无法签署交易并使用BlockCypher发送交易

时间:2019-07-25 22:48:25

标签: node.js blockcypher bitcoinjs-lib

您好,我正在尝试使用Blockcypher创建和发送交易

从外观上看,他们的文档确实是最新的。

我想在这里实现的是。

  • 创建2个地址(成功)

  • 将使用水龙头的sa徒添加到第一个(成功)

  • 将sa子从第一个转移到第二个(完全失败)

这是我正在尝试实现的功能。


async transaction(from, to, ammount) {
  const newTransaction = {
    inputs: [ { addresses: [ from.address ] } ]
    outputs: [ { addresses: [ to.address ], value: ammount } ]
  };
  const { data: newTx } = await post(`${ this.url }/txs/new`, newTransaction, { query: { token: this.token } });
  if(checkError(newTx)) {
    return false;
  }

  // This is where things split from official documentation
  // Official docs say but this throws not-a-constructor error
  // new bitcoin.ECPair(bigi.fromHex(my_hex_private_key));

  const keys = new bitcoin.ECPair.fromPrivateKey(Buffer.from(from.private, 'hex'));
  newTx.pubkeys = [];
  newTx.signatures = newTx.tosign.map((tosign) => {
    newTx.pubkeys.push(keys.publicKey.toString('hex'));

    // here is the point of failure
    // error says toDER is not a function. 
    return keys.sign(new Buffer(tosign, 'hex')).toDER().toString('hex');
  });

  // If I remove .toDER() call this will fail with 400 error indicating that transaction is not signed properly
  await post(`${ this.url }/txs/send`, newTx, { query: { token: this.token } });
  return true;
}

那么,我该如何完成这项交易?

0 个答案:

没有答案