Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require('babel-polyfill')
  2.  
  3. const Metaverse = require("metaversejs");
  4. const Blockchain = require("mvs-blockchain");
  5.  
  6. const TransportHID = require('@ledgerhq/hw-transport-node-hid').default;
  7. const BtcApp = require('./ledgerjs-hw-app-btc').default;
  8.  
  9. const getDevice = async () => {
  10.     let transport = await TransportHID.create();
  11.     let ledger = new BtcApp(transport);
  12.     ledger.close = () => transport.close();
  13.     console.log(ledger)
  14.     return ledger;
  15. };
  16.  
  17. const blockchain = Blockchain({
  18.     url: "https://explorer.mvs.org/api/"
  19. });  
  20. const path = "44'/2302'/0'/0/0";
  21.  
  22. const buildTx = async address => {
  23.  
  24.     let target = {
  25.         ETP: 100 //100 million units = 1 ETP
  26.     };
  27.  
  28.     let recipient_address = "MT7By3irp1nzTvha3zGe7t8h61HtUSzTAn";
  29.     let height = await blockchain.height();
  30.     let txs = await blockchain.addresses.txs([address]);
  31.     let utxos = await Metaverse.output.calculateUtxo(txs.transactions, [
  32.        address
  33.     ]);
  34.  
  35.     console.log("MTV utxo", utxos);
  36.     let result = await Metaverse.output.findUtxo(utxos, target, height);
  37.     let tx = await Metaverse.transaction_builder.send(
  38.         result.utxo,
  39.         recipient_address,
  40.         undefined,
  41.         target,
  42.         result.utxo[0].address,
  43.         result.change
  44.     );
  45.     // let tx = new Metaverse.transaction()
  46.     // tx.addInput(result.utxos.inputs.address, result.utxos.inputs.previous_output.hash, 1)
  47.     // tx.addOutput(recipient_address, "DNA", 0.1234)
  48.     console.log("MTV tx", tx.inputs);
  49.  
  50.     return await tx;
  51.  
  52. };
  53.  
  54.  
  55. async function transferMST(amount,MSTSymbol) {
  56.  
  57.     amount = parseInt(amount * 10**4)
  58.     recipient_address = "MNWVbJuNxq9FQNL1bWXx5sXiFRKjcFuuj9"
  59.     from_address = "MT7By3irp1nzTvha3zGe7t8h61HtUSzTAn"
  60.  
  61.     let target = {};
  62.     target[MSTSymbol] = amount
  63.     console.log(target)
  64.  
  65.     change_address = from_address
  66.     let height = await blockchain.height()
  67.     let txs = await blockchain.addresses.txs([from_address])
  68.     let utxos = await Metaverse.output.calculateUtxo(txs.transactions, [from_address]) //Get all utxo
  69.     let result = await Metaverse.output.findUtxo(utxos, target, height) //Collect utxo for given target
  70.     let tx = await Metaverse.transaction_builder.send(result.utxo, recipient_address, undefined, target, change_address, result.change)
  71.     // tx = await wallet.sign(tx)
  72.     // tx = await tx.encode()
  73.     // tx = await blockchain.transaction.broadcast(tx.toString('hex'))
  74.     console.log(tx)
  75.  
  76.     return(tx)
  77. }
  78.  
  79. const createDummyTransaction = async function() {
  80.  
  81.     const ledger = await getDevice();
  82.     let inputs = [
  83.         [
  84.             {
  85.                 "version":Buffer.from([4,0,0,0]),
  86.                 "inputs":[
  87.                     {
  88.                         "prevout":Buffer.from([9,115,169,139,185,54,170,83,201,180,124,233,89,209,154,133,66,98,212,77,95,143,31,92,151,163,187,250,50,11,77,219,0,0,0,0]),
  89.                         "vout":0,
  90.                         "script":Buffer.from([72,48,69,2,33,0,163,246,111,140,197,161,139,250,253,127,241,75,234,100,207,147,82,25,16,145,7,122,94,99,106,125,39,224,112,67,76,196,2,32,42,143,248,234,124,148,89,195,0,185,135,87,115,229,109,57,161,174,163,57,122,51,125,138,252,119,123,170,8,52,117,150,1,33,2,158,152,48,13,133,142,233,12,195,56,152,76,120,79,165,195,36,24,22,91,226,215,7,35,8,172,125,95,51,136,67,176]),
  91.                         "sequence":Buffer.from([255,255,255,255])
  92.                     },
  93.                     {
  94.                         "prevout":Buffer.from([17,234,213,88,231,166,158,103,219,61,246,10,13,47,178,145,216,14,138,107,204,59,128,205,35,22,41,185,40,207,238,32,0,0,0,0]),
  95.                         "vout":0,
  96.                         "script":Buffer.from([71,48,68,2,32,4,106,99,102,171,243,12,137,110,97,44,27,243,41,241,185,34,96,184,216,88,9,108,217,32,253,59,7,92,235,71,2,2,32,73,236,146,199,226,7,246,119,62,21,66,5,118,187,2,71,236,151,153,122,178,3,203,107,107,77,23,3,36,178,23,238,1,33,2,158,152,48,13,133,142,233,12,195,56,152,76,120,79,165,195,36,24,22,91,226,215,7,35,8,172,125,95,51,136,67,176]),
  97.                         "sequence":Buffer.from([255,255,255,255])
  98.                     }
  99.                 ],
  100.                 "outputs":[
  101.                     {
  102.                         "amount":Buffer.from([0,0,0,0,0,0,0,0]),
  103.                         "script":Buffer.from([118,169,20,160,64,123,4,164,230,92,38,158,114,84,186,26,184,74,255,189,173,110,236,136,172]),
  104.                         "postfix":"01000000020000000200000003444e411027000000000000"
  105.                     },
  106.                     {
  107.                         "amount":Buffer.from([0,0,0,0,0,0,0,0]),
  108.                         "script":Buffer.from([118,169,20,210,176,212,93,253,186,80,251,210,39,249,104,199,154,41,236,224,81,68,251,136,172]),
  109.                         "postfix":"01000000020000000200000003444e41204e000000000000"
  110.                     },
  111.                     {
  112.                         "amount":Buffer.from([208,108,4,0,0,0,0,0]),
  113.                         "script":Buffer.from([118,169,20,210,176,212,93,253,186,80,251,210,39,249,104,199,154,41,236,224,81,68,251,136,172]),
  114.                         "postfix":"0100000000000000"
  115.                     }
  116.                 ],
  117.                 "locktime":Buffer.from([0,0,0,0]),
  118.                 "timestamp":Buffer.from([]),
  119.                 "nVersionGroupId":Buffer.from([]),
  120.                 "nExpiryHeight":Buffer.from([]),
  121.                 "extraData":Buffer.from([])
  122.             },
  123.             0,
  124.             null,
  125.             4294967295
  126.         ],
  127.         [
  128.             {
  129.                 "version":Buffer.from([4,0,0,0]),
  130.                 "inputs":[{"prevout":Buffer.from([101,186,93,104,40,49,94,170,216,189,241,144,71,230,115,250,143,110,221,57,110,14,175,16,167,179,27,212,230,49,100,20,1,0,0,0]),"vout":1,"script":Buffer.from([72,48,69,2,33,0,147,188,143,70,156,123,170,51,242,232,154,183,60,179,219,1,170,242,255,125,75,51,100,155,144,174,175,83,223,60,33,67,2,32,87,231,194,30,7,72,249,170,3,115,86,40,201,87,89,173,142,22,50,122,221,111,13,163,182,128,115,103,234,239,28,215,1,33,3,184,167,137,47,91,51,56,129,162,97,70,89,56,234,65,112,179,56,122,173,237,200,219,11,84,137,235,169,48,213,18,189]),"sequence":Buffer.from([255,255,255,255])}],
  131.                 "outputs":[{"amount":Buffer.from([224,147,4,0,0,0,0,0]),"script":Buffer.from([118,169,20,210,176,212,93,253,186,80,251,210,39,249,104,199,154,41,236,224,81,68,251,136,172]),"postfix":"0100000000000000"},{"amount":Buffer.from([192,237,101,0,0,0,0,0]),"script":Buffer.from([118,169,20,160,64,123,4,164,230,92,38,158,114,84,186,26,184,74,255,189,173,110,236,136,172]),"postfix":"0100000000000000"}],
  132.                 "locktime":Buffer.from([0,0,0,0]),
  133.                 "timestamp":Buffer.from([]),
  134.                 "nVersionGroupId":Buffer.from([]),
  135.                 "nExpiryHeight":Buffer.from([]),
  136.                 "extraData":Buffer.from([])
  137.             },
  138.             1,
  139.             null,
  140.             4294967295
  141.         ]
  142.     ];
  143.  
  144.     let associatedKeysets = ["44'/2302'/0'/0/0", "44'/2302'/0'/0/0"];
  145.     let changePath = "44'/2302'/0'/0/0";
  146.     let outputsScript = "030300000000000000001976a914d2b0d45dfdba50fbd227f968c79a29ece05144fb88ac01000000020000000200000003444e41d20400000000000000000000000000001976a914a0407b04a4e65c269e7254ba1ab84affbdad6eec88ac01000000020000000200000003444e413e22000000000000b0c66500000000001976a914a0407b04a4e65c269e7254ba1ab84affbdad6eec88ac0100000000000000";
  147.     let locktime = undefined;
  148.     let sigHash = 1;
  149.     let segwit = false;
  150.     let initialTimestamp = undefined;
  151.     let additionals = [];
  152.     let expiryHeight = undefined;
  153.     let options = {
  154.         version: 4,
  155.         outputsPrefix: "040400",
  156.         outputScriptChunks: [
  157.             Buffer.from("0300000000000000001976a914d2b0d45dfdba50fbd227f968c79a29ece05144fb88ac01000000020000000200000003444e41d204000000000000", 'hex'),
  158.             Buffer.from("00000000000000001976a914a0407b04a4e65c269e7254ba1ab84affbdad6eec88ac01000000020000000200000003444e413e22000000000000", 'hex'),
  159.             Buffer.from("b0c66500000000001976a914a0407b04a4e65c269e7254ba1ab84affbdad6eec88ac0100000000000000", 'hex'),
  160.         ],
  161.     };
  162.  
  163.     const transaction = await ledger.createPaymentTransactionNew(
  164.         inputs,
  165.         associatedKeysets,
  166.         changePath,
  167.         outputsScript,
  168.         locktime,
  169.         sigHash,
  170.         segwit,
  171.         initialTimestamp,
  172.         additionals,
  173.         expiryHeight,
  174.         options
  175.     );
  176.  
  177.     await ledger.close();
  178.     console.log('Signed transaction', transaction);
  179.     return transaction;
  180. }
  181.  
  182.  
  183. const createLedgerTransaction = async function() {
  184.     const ledger = await getDevice();
  185.     let walletInfo = await ledger.getWalletPublicKey(path);
  186.  
  187.     // Transfer ETP
  188.     // let tx = await buildTx(await walletInfo.bitcoinAddress);
  189.  
  190.     // Transfer MST
  191.     let tx = await transferMST(0.1234, "DNA");
  192.    
  193.     let split_tx = await ledger.splitTransaction(await tx.encode().toString("hex"));
  194.  
  195.     let inputs = []
  196.     let outputs = []
  197.     let outputScriptChunks = []
  198.     await tx.inputs.forEach((tx_input) => {
  199.         console.log(tx_input);    
  200.         const input = {
  201.             "prevout": Buffer.from(tx_input.previous_output.hash, 'hex').reverse(),
  202.             "vout": tx_input.previous_output.index,
  203.             "script": Metaverse.script.fromASM(tx_input.previous_output.script).buffer,
  204.             "sequence": Buffer.from([255,255,255,255]) // disabled
  205.         }
  206.         inputs.push(input);
  207.     });
  208.     await tx.outputs.forEach((tx_output) => {
  209.         let amount_buffer = Buffer.alloc(8)
  210.         amount_buffer.writeInt32LE(tx_output.value)
  211.         const output = {
  212.             "amount": {"type":"Buffer","data": amount_buffer},
  213.             "script": Metaverse.encoder.outputScriptAsBuffer(tx_output).slice(1), // don't pass the first byte (length)
  214.             "postfix": Metaverse.encoder.attachmentAsBuffer(tx_output).toString("hex")
  215.         }
  216.         outputs.push(output);
  217.     });
  218.     await split_tx.outputs.forEach((tx_output) => {
  219.         const chunk = Buffer.from(tx_output.amount.toString("hex")+ tx_output.script.length.toString(16).padStart(2,0) + tx_output.script.toString("hex") , 'hex');
  220.         outputScriptChunks.push(chunk);
  221.     });
  222.  
  223.     let associatedKeysets = ["44'/2302'/0'/0/0", "44'/2302'/0'/0/0"];
  224.     let changePath = "44'/2302'/0'/0/0";
  225.     let outputsScript = split_tx.outputs.length.toString(16).padStart(2, '0') + ledger.serializeTransactionOutputs(split_tx).toString('hex');
  226.     let locktime = undefined;
  227.     let sigHash = 1;
  228.     let segwit = false;
  229.     let initialTimestamp = undefined;
  230.     let additionals = [];
  231.     let expiryHeight = undefined;
  232.     let options = {
  233.         version: 4,
  234.         outputsPrefix: "0808",
  235.         outputScriptChunks: outputScriptChunks,
  236.     };
  237.  
  238.     let ledger_inputs = [
  239.         [
  240.             {
  241.                 "version": Buffer.from([4,0,0,0]),
  242.                 "inputs": inputs,
  243.                 "outputs": outputs,
  244.                 "locktime": Buffer.from([0,0,0,0]),
  245.                 "timestamp": Buffer.from([]),
  246.                 "nVersionGroupId": Buffer.from([]),
  247.                 "nExpiryHeight": Buffer.from([]),
  248.                 "extraData": Buffer.from([])
  249.             },
  250.             0,
  251.             null,
  252.             4294967295
  253.         ]
  254.     ];
  255.  
  256.     const transaction = await ledger.createPaymentTransactionNew(
  257.         ledger_inputs,
  258.         associatedKeysets,
  259.         changePath,
  260.         outputsScript,
  261.         locktime,
  262.         sigHash,
  263.         segwit,
  264.         initialTimestamp,
  265.         additionals,
  266.         expiryHeight,
  267.         options
  268.     );
  269.  
  270.     await ledger.close();
  271.  
  272.     console.log('Signed transaction', transaction);
  273.  
  274.     return transaction;
  275. };
  276.  
  277. // createDummyTransaction();
  278. createLedgerTransaction();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement