|
| 1 | +const { |
| 2 | + MixinApi, |
| 3 | + getED25519KeyPair, |
| 4 | + getTipPinUpdateMsg, |
| 5 | + base64RawURLDecode, |
| 6 | + encodeSafeTransaction, |
| 7 | + buildSafeTransactionRecipient, |
| 8 | + buildSafeTransaction, |
| 9 | + signSafeTransaction, |
| 10 | +} = require('..'); |
| 11 | +const { v4 } = require('uuid'); |
| 12 | +const keystore = require('../keystore.json'); // keystore from your bot |
| 13 | + |
| 14 | +const main = async () => { |
| 15 | + const client = MixinApi({ keystore }); |
| 16 | + let bot = await client.user.profile(); |
| 17 | + |
| 18 | + // private key for safe registration |
| 19 | + let privateKey = ''; |
| 20 | + // upgrade to tip pin if haven't |
| 21 | + if (!bot.tip_key_base64) { |
| 22 | + const keys = getED25519KeyPair(); |
| 23 | + const pub = base64RawURLDecode(keys.publicKey); |
| 24 | + const priv = base64RawURLDecode(keys.privateKey); |
| 25 | + const tipPin = priv.toString('hex'); |
| 26 | + privateKey = tipPin; |
| 27 | + |
| 28 | + const b = getTipPinUpdateMsg(pub, bot.tip_counter + 1); |
| 29 | + await client.pin.update(keystore.pin, b); |
| 30 | + bot = await client.pin.verifyTipPin(tipPin); |
| 31 | + keystore.pin = tipPin; // should update pin in your keystore file too |
| 32 | + console.log('new tip pin', tipPin); |
| 33 | + } |
| 34 | + |
| 35 | + // register to safe if haven't |
| 36 | + // it's convinient to use the same private key as above tipPin |
| 37 | + if (!bot.has_safe) { |
| 38 | + const resp = await client.safe.register(keystore.client_id, keystore.pin, privateKey); |
| 39 | + console.log(resp); |
| 40 | + } |
| 41 | + |
| 42 | + // destination |
| 43 | + const members = ['7766b24c-1a03-4c3a-83a3-b4358266875d']; |
| 44 | + const threshold = 1; |
| 45 | + const recipients = [buildSafeTransactionRecipient(members, threshold, '1')]; |
| 46 | + |
| 47 | + // get ghost key to send tx to uuid multisigs |
| 48 | + // For Mixin Kernel Address start with 'XIN', get ghost key with getMainnetAddressGhostKey |
| 49 | + const ghosts = await client.utxo.ghostKey( |
| 50 | + recipients.map((r, i) => ({ |
| 51 | + hint: v4(), |
| 52 | + receivers: r.members, |
| 53 | + index: i, |
| 54 | + })), |
| 55 | + ); |
| 56 | + |
| 57 | + // get unspent utxos |
| 58 | + const outputs = await client.utxo.safeOutputs({ |
| 59 | + members: [keystore.client_id], |
| 60 | + threshold: 1, |
| 61 | + asset: 'edb4fe8b-8f05-32e3-a0e0-5d2096e7a7ac', |
| 62 | + state: 'unspent', |
| 63 | + }); |
| 64 | + console.log(outputs); |
| 65 | + const balance = await client.utxo.safeAssetBalance({ |
| 66 | + members: [keystore.client_id], |
| 67 | + threshold: 1, |
| 68 | + asset: 'edb4fe8b-8f05-32e3-a0e0-5d2096e7a7ac', |
| 69 | + state: 'unspent', |
| 70 | + }); |
| 71 | + console.log(balance); |
| 72 | + |
| 73 | + // build safe transaction raw |
| 74 | + const tx = buildSafeTransaction(outputs, recipients, ghosts, 'test-memo'); |
| 75 | + console.log(tx); |
| 76 | + const raw = encodeSafeTransaction(tx); |
| 77 | + console.log(raw); |
| 78 | + |
| 79 | + // verify safe transaction |
| 80 | + const request_id = v4(); |
| 81 | + const verifiedTx = await client.utxo.verifyTransaction([ |
| 82 | + { |
| 83 | + raw, |
| 84 | + request_id, |
| 85 | + }, |
| 86 | + ]); |
| 87 | + console.log(verifiedTx); |
| 88 | + |
| 89 | + // sign safe transaction with the private key registerd to safe |
| 90 | + const signedRaw = await signSafeTransaction(tx, verifiedTx[0].views, privateKey); |
| 91 | + console.log(signedRaw); |
| 92 | + const sendedTx = await client.utxo.sendTransactions([ |
| 93 | + { |
| 94 | + raw: signedRaw, |
| 95 | + request_id, |
| 96 | + }, |
| 97 | + ]); |
| 98 | + console.log(sendedTx); |
| 99 | +}; |
| 100 | + |
| 101 | +main(); |
0 commit comments