Open junderw opened 4 years ago
Hi , I am willing to develop a wallet example project and have an experience of using bitcoinjs lib except psbt functions which are recently added.. Tell me the next steps.
@tarun1475
Sure.
As for advice:
Once you get an issue up and running you can ask some more questions there.
Thanks.
Sounds great.
I have few more questions i will ask in the issue then. Thanks.
Regarding "Async Hardware Wallet library", I'm interested in implementing it for the Ledger HW wallet since I'm already using ledger nano with psbt in my project. The main problem is that LedgerJS's sign function needs the whole transaction for every input being signed (not just for non witness inputs) so atm it's impossible to sign a segwit input only with data contained in pbst transaction.
(I asked them if they planned to support bip174 https://github.com/LedgerHQ/ledgerjs/issues/399)
I added support for nonWitnessUtxo with segwit. (it just ignores the extra info, since the witnessUtxo is contained within the nonWitnessUtxo)
So while not exactly the intended spec, you can use nonWitnessUtxo for all inputs in this library, and the segwit detection is done by matching scripts.
Most other PSBT implementations don't do this though. So they should eventually support it.
Great, even if it is just a patch waiting for ledgerjs implementing it. Another thing I need is a way to extract the unsigned transaction hex like I did with
txb.buildIncomplete().toHex()
since ledger.signP2SH needs serialized transaction outputs: atm i'm doing as follow:
const outshex = btc.serializeTransactionOutputs(btc.splitTransaction((txb as any).__CACHE.__TX.toHex(), true)).toString('hex');
which is not a clean way. Maybe could be useful an additional extractTransactionIncomplete which ignores non-finalized inputs?
hmmm.... tbh, I think the whole point of PSBT is that we have a format to encode Partially Signed Bitcoin Transactions...
So having an extract incomplete method seems antithetical to the idea of PSBT.
Your hack is OK for now. Let's wait until Ledger supports PSBT to fix it. :-D
yea, I agree with you, let's wait :) I post here the code I'm using to sign p2wsh and p2sh-p2wsh with the ledger, maybe could be useful for other users while waiting:
TransportU2F.create().then(transport => {
const btc = new Btc(transport);
/* Download utxo transaction raw */
this.rawTransactions(options.utxos.map(utxo => utxo.tx)).then(transactions => {
/* Create inputs and serialized outputs */
const inputs = options.utxos.map(utxo => [
btc.splitTransaction(transactions[utxo.tx], bitcoinjs.Transaction.fromHex(transactions[utxo.tx]).hasWitnesses()),
utxo.n,
walletScripts.p2wsh.redeem.output.toString('hex'),
]);
const paths = inputs.map(i => this.signPath);
const txb = bitcoinjs.Psbt.fromHex(txhex, { network: this.config.network });
const outshex = btc.serializeTransactionOutputs(btc.splitTransaction((txb as any).__CACHE.__TX.toHex(), true)).toString('hex');
btc.signP2SHTransaction(inputs, paths, outshex, 0/*tx.locktime*/, 1 /*SIGHASH_ALL*/, segwit, 2).then(signatures => {
/* Inject signatures */
for (let j = 0; j < txb.inputCount; j++) {
txb.signInput(j, {
network: this.config.network,
publicKey: toByteArray(publickey),
sign: (hash) => bitcoinjs.script.signature.decode(toByteArray(signatures[j])).signature
} as bitcoinjs.ECPairInterface);
}
/* Build the transaction */
resolve(txb.toHex());
});
});
});
And this is the code for retrieving the pubkey:
private getPublicKeyFromPath(path: string) {
return new Promise((resolve, reject) => {
TransportU2F.create().then(transport => {
const btc = new Btc(transport);
btc.getWalletPublicKey(path, { verify: false, format: 'legacy' }).then(result => {
const comppk = compressPublicKey(result.publicKey);
resolve(comppk);
}).catch(err => {
reject(err);
});
}).catch(err => {
reject(err);
});
});
};
Very cool.
I would like to see the bigger project. Not sure where publickey is coming from etc.
@junderw I put the getpubkey function in the previous message; anyway for further details the frontend wallet part of our platform is opensource: https://github.com/helperbit/helperbit-wallet
@junderw maybe we can merge this with #1326
Any other ideas for future projects are welcome.