bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.84k stars 2.08k forks source link

How to apply a signature to a Transaction? (bitcore-lib-cash) #2884

Open kss-espeo opened 4 years ago

kss-espeo commented 4 years ago

Transaction's interface has a applySignature method that references Input's addSignature method. Therefore, calling Transaction.applySignature results in a following error:

(node:17220) UnhandledPromiseRejectionWarning: Abstract Method Invocation: Input#addSignature
Error: 
    at new NodeError (/home/kspisacki/coding/wallets/node_modules/bitcore-lib-cash/lib/errors/index.js:20:41)
    at Input.addSignature (/home/kspisacki/coding/wallets/node_modules/bitcore-lib-cash/lib/transaction/input/input.js:179:9)
    at Transaction.applySignature (/home/kspisacki/coding/wallets/node_modules/bitcore-lib-cash/lib/transaction/transaction.js:1123:37)
    at signatures.forEach (/home/kspisacki/coding/wallets/src/blockchainProviders/bitcoinCash/bitcoinCashLedgerAdaptor.ts:61:25)
    at Array.forEach (<anonymous>)
    at BitcoinCashLedgerAdaptor.<anonymous> (/home/kspisacki/coding/wallets/src/blockchainProviders/bitcoinCash/bitcoinCashLedgerAdaptor.ts:48:20)
    at Generator.next (<anonymous>)
    at /home/kspisacki/coding/wallets/src/blockchainProviders/bitcoinCash/bitcoinCashLedgerAdaptor.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/home/kspisacki/coding/wallets/src/blockchainProviders/bitcoinCash/bitcoinCashLedgerAdaptor.ts:4:12)
    at BitcoinCashLedgerAdaptor.applySignaturesToTransaction (/home/kspisacki/coding/wallets/src/blockchainProviders/bitcoinCash/bitcoinCashLedgerAdaptor.ts:53:16)
    at Cli.<anonymous> (/home/kspisacki/coding/wallets/src/cli/cli.ts:273:19)
    at Generator.next (<anonymous>)
    at fulfilled (/home/kspisacki/coding/wallets/src/cli/cli.ts:5:58)

And not surprisingly, since Input was implemented to do exactly that: https://github.com/bitpay/bitcore/blob/master/packages/bitcore-lib-cash/lib/transaction/input/input.js#L179 .

Is this a correct method to apply an existing signature to a Transaction ? If not - how do I do this?

Please note that I am not after signing a transaction. I have no access to a private key, I only have an existing signature.

kss-espeo commented 4 years ago

It seems that the only way of instantiating a Transaction that correctly classifies concrete implementation of Inputs is Transaction.prototype._fromNonP2SH

All other methods, like passing hex to a constructor will only create abstract Inputs , therefore some Transaction's features (like Transaction.applySignature) will not be available. This could probably be fixed by addressing a TODO in Input.fromBufferReader .

Please, confirm that I'm right and there is no workaround for that.

tharun-p commented 3 years ago

Facing same issue while adding Signatures using addSignatures from Inputs.