Bitcoin Forum
May 04, 2024, 11:49:03 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
  Home Help Search Login Register More  
  Show Posts
Pages: [1]
1  Bitcoin / Development & Technical Discussion / Bitcoin Digital Signatures in TypeScript on: September 18, 2020, 09:13:04 AM
Hi all. I am new to development with bitcoin. I was trying to use ECDSA signatures for a transaction generated on JavaScript. But I am thoroughly confused to say the least.

First of all I would be really grateful if anyone of you could point me towards an example of using digital signatures to sign a transaction in JS/TS.

This is my implementation of what I have done, with using bitcoinjs-lib.

Code:
const bitcoin = require('bitcoinjs-lib');
const txb = new bitcoin.transaction.TransactionBuilder(bitcoin.networks.testnet);

txb.setVersion(1);

txb.addInput('33e516d808e631774d80b9d6f2fd611d96088f6d34bf82ed83d440ecf512d694' /* txid */, 0 /* vout */);
txb.addOutput('tb1q8vmdmdxe7a05qyz9pvj639f2vujesxlmzzacfg', 2e4 /* in Hex */);

const unsignedHash = txb.buildIncomplete();
const digitalSignature = myDigitalSignatureGenerator(unsignedHash); /* Assuming my digital signature generator is correct */
Now I fail to understand what to do next.
Can I replace digitalSignature with the private key?

I tried to use txb.sign()

but it requires a keyPair which I don't think is necessary if I am using a digitalSignature? Correct me if I am wrong?

But still I tried to do something like

Code:
const keyPair = bitcoin.ECPair.makeRandom({ network: bitcoin.networks.testnet });
keyPair.publicKey = Buffer.from('039b219ff489f9f5d3c674602e2280cc803f068f07db7295c7d2a2f9d51844cc45' /* the actual public key */, 'hex');
keyPair.privateKey = Buffer.from(digitalSignature /* this is the digitalSignature I generated */, 'hex');


.
.
.
txb.sign(0, {
publicKey: Buffer.from('039b219ff489f9f5d3c674602e2280cc803f068f07db7295c7d2a2f9d51844cc45', 'hex'),
network: bitcoin.networks.testnet,
keyPair,
});
console.log(txb.build().toHex());

Now don't get me wrong I know this is wrong, but I am feeling pretty helpless in this situation. I am not sure how to proceed. How to use my digital signature for the unsigned transaction I generated. Is there anyway to sign the unsigned transaction bytes I generated? Should I be using any other library. Any help, literally any help would be much much appreciated.
Pages: [1]
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!