-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add apis for tip pin * Prepare data to register safe * Format * Fix encoding * Reorganize safe funcs * Format * Add safe transaction encode * Add unfinished safe tx example * Format * Fix type * Sign safe transaction with views in resp * Slight fixes * Slight fix * Fix blake3 lib * Add safeAssetBalance * Improve params * Add safe transaction signature test * Add getMainnetAddressGhostKey * Improve safe example * Format * Ignore noble curves type error * Improve hash funcs * Slight improves * Improve func name
- Loading branch information
1 parent
6d449cb
commit 4c6b378
Showing
30 changed files
with
806 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const { | ||
MixinApi, | ||
getED25519KeyPair, | ||
getTipPinUpdateMsg, | ||
base64RawURLDecode, | ||
encodeSafeTransaction, | ||
buildSafeTransactionRecipient, | ||
buildSafeTransaction, | ||
signSafeTransaction, | ||
} = require('..'); | ||
const { v4 } = require('uuid'); | ||
const keystore = require('../keystore.json'); // keystore from your bot | ||
|
||
const main = async () => { | ||
const client = MixinApi({ keystore }); | ||
let bot = await client.user.profile(); | ||
|
||
// private key for safe registration | ||
let privateKey = ''; | ||
// upgrade to tip pin if haven't | ||
if (!bot.tip_key_base64) { | ||
const keys = getED25519KeyPair(); | ||
const pub = base64RawURLDecode(keys.publicKey); | ||
const priv = base64RawURLDecode(keys.privateKey); | ||
const tipPin = priv.toString('hex'); | ||
privateKey = tipPin; | ||
|
||
const b = getTipPinUpdateMsg(pub, bot.tip_counter + 1); | ||
await client.pin.update(keystore.pin, b); | ||
bot = await client.pin.verifyTipPin(tipPin); | ||
keystore.pin = tipPin; // should update pin in your keystore file too | ||
console.log('new tip pin', tipPin); | ||
} | ||
|
||
// register to safe if haven't | ||
// it's convinient to use the same private key as above tipPin | ||
if (!bot.has_safe) { | ||
const resp = await client.safe.register(keystore.client_id, keystore.pin, privateKey); | ||
console.log(resp); | ||
} | ||
|
||
// destination | ||
const members = ['7766b24c-1a03-4c3a-83a3-b4358266875d']; | ||
const threshold = 1; | ||
const recipients = [buildSafeTransactionRecipient(members, threshold, '1')]; | ||
|
||
// get ghost key to send tx to uuid multisigs | ||
// For Mixin Kernel Address start with 'XIN', get ghost key with getMainnetAddressGhostKey | ||
const ghosts = await client.utxo.ghostKey( | ||
recipients.map((r, i) => ({ | ||
hint: v4(), | ||
receivers: r.members, | ||
index: i, | ||
})), | ||
); | ||
|
||
// get unspent utxos | ||
const outputs = await client.utxo.safeOutputs({ | ||
members: [keystore.client_id], | ||
threshold: 1, | ||
asset: 'edb4fe8b-8f05-32e3-a0e0-5d2096e7a7ac', | ||
state: 'unspent', | ||
}); | ||
console.log(outputs); | ||
const balance = await client.utxo.safeAssetBalance({ | ||
members: [keystore.client_id], | ||
threshold: 1, | ||
asset: 'edb4fe8b-8f05-32e3-a0e0-5d2096e7a7ac', | ||
state: 'unspent', | ||
}); | ||
console.log(balance); | ||
|
||
// build safe transaction raw | ||
const tx = buildSafeTransaction(outputs, recipients, ghosts, 'test-memo'); | ||
console.log(tx); | ||
const raw = encodeSafeTransaction(tx); | ||
console.log(raw); | ||
|
||
// verify safe transaction | ||
const request_id = v4(); | ||
const verifiedTx = await client.utxo.verifyTransaction([ | ||
{ | ||
raw, | ||
request_id, | ||
}, | ||
]); | ||
console.log(verifiedTx); | ||
|
||
// sign safe transaction with the private key registerd to safe | ||
const signedRaw = await signSafeTransaction(tx, verifiedTx[0].views, privateKey); | ||
console.log(signedRaw); | ||
const sendedTx = await client.utxo.sendTransactions([ | ||
{ | ||
raw: signedRaw, | ||
request_id, | ||
}, | ||
]); | ||
console.log(sendedTx); | ||
}; | ||
|
||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.