|
| 1 | +const { v4 } = require('uuid'); |
| 2 | +const { MixinApi, buildMultiSigsTransaction, sleep, encodeScript } = require('..'); |
| 3 | +const keystore = require('../keystore.json'); |
| 4 | + |
| 5 | +keystore.user_id = keystore.client_id; |
| 6 | +const client = MixinApi({ |
| 7 | + requestConfig: { |
| 8 | + responseCallback: err => { |
| 9 | + console.log(err); |
| 10 | + }, |
| 11 | + }, |
| 12 | + keystore, |
| 13 | +}); |
| 14 | + |
| 15 | +const readOutput = async (hash, members, threshold, offset = '') => { |
| 16 | + let new_offset = offset; |
| 17 | + const outputs = await client.multisig.outputs({ |
| 18 | + members, |
| 19 | + threshold, |
| 20 | + offset, |
| 21 | + limit: 10, |
| 22 | + }); |
| 23 | + |
| 24 | + // eslint-disable-next-line no-restricted-syntax |
| 25 | + for (const output of outputs) { |
| 26 | + new_offset = output.updated_at; |
| 27 | + if (output.transaction_hash === hash) return output; |
| 28 | + } |
| 29 | + |
| 30 | + await sleep(1000 * 5); |
| 31 | + return readOutput(hash, members, threshold, new_offset); |
| 32 | +}; |
| 33 | + |
| 34 | +const main = async () => { |
| 35 | + const bot = await client.user.profile(); |
| 36 | + const asset_id = '965e5c6e-434c-3fa9-b780-c50f43cd955c'; |
| 37 | + const amount = '0.0001'; |
| 38 | + const members = [bot.app.creator_id, keystore.user_id]; |
| 39 | + const threshold = 1; |
| 40 | + |
| 41 | + // 1. send to multisig account |
| 42 | + // should have balance in your bot |
| 43 | + const sendTxReceipt = await client.transfer.toAddress(keystore.pin, { |
| 44 | + asset_id, |
| 45 | + amount, |
| 46 | + trace_id: v4(), |
| 47 | + memo: 'send to multisig', |
| 48 | + opponent_multisig: { |
| 49 | + threshold, |
| 50 | + receivers: members, |
| 51 | + }, |
| 52 | + }); |
| 53 | + console.log('send to multi-signature account'); |
| 54 | + console.log('transaction hash:', sendTxReceipt.transaction_hash); |
| 55 | + |
| 56 | + // 2. wait tx complete |
| 57 | + console.log('read transaction output...'); |
| 58 | + const utxo = await readOutput(sendTxReceipt.transaction_hash, members, threshold, ''); |
| 59 | + console.log(utxo); |
| 60 | + |
| 61 | + // 3. refund |
| 62 | + console.log('refund to bot:'); |
| 63 | + const asset = await client.asset.fetch(asset_id); |
| 64 | + const receivers = await client.transfer.outputs([ |
| 65 | + { |
| 66 | + receivers: [keystore.user_id], |
| 67 | + index: 0, |
| 68 | + }, |
| 69 | + ]); |
| 70 | + console.log('generate raw transaction'); |
| 71 | + const raw = buildMultiSigsTransaction({ |
| 72 | + version: 2, |
| 73 | + asset: asset.mixin_id, |
| 74 | + inputs: [ |
| 75 | + { |
| 76 | + hash: utxo.transaction_hash, |
| 77 | + index: utxo.output_index, |
| 78 | + }, |
| 79 | + ], |
| 80 | + outputs: [ |
| 81 | + { |
| 82 | + amount, |
| 83 | + mask: receivers[0].mask, |
| 84 | + keys: receivers[0].keys, |
| 85 | + script: encodeScript(threshold), |
| 86 | + }, |
| 87 | + ], |
| 88 | + extra: 'refund', |
| 89 | + }); |
| 90 | + |
| 91 | + // Generate a multi-signature |
| 92 | + console.log('generate a multi-signature request...'); |
| 93 | + const multisig = await client.multisig.create('sign', raw); |
| 94 | + |
| 95 | + // Sign a multi-signature |
| 96 | + console.log('sign...'); |
| 97 | + const signed = await client.multisig.sign(keystore.pin, multisig.request_id); |
| 98 | + console.log(signed); |
| 99 | + |
| 100 | + // Send signed tx to mainnet |
| 101 | + console.log('send to mainnet...'); |
| 102 | + const res = await client.external.proxy({ |
| 103 | + method: 'sendrawtransaction', |
| 104 | + params: [signed.raw_transaction], |
| 105 | + }); |
| 106 | + console.log(res); |
| 107 | +}; |
| 108 | + |
| 109 | +main(); |
0 commit comments