Bitcoin Forum
May 07, 2024, 10:47:13 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Bitcoin Digital Signatures in TypeScript  (Read 91 times)
bitcoinNoob21 (OP)
Newbie
*
Offline Offline

Activity: 1
Merit: 3


View Profile
September 18, 2020, 09:13:04 AM
Merited by ABCbits (2), TryNinja (1)
 #1

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.
TalkImg was created especially for hosting images on bitcointalk.org: try it next time you want to post an image
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715078833
Hero Member
*
Offline Offline

Posts: 1715078833

View Profile Personal Message (Offline)

Ignore
1715078833
Reply with quote  #2

1715078833
Report to moderator
1715078833
Hero Member
*
Offline Offline

Posts: 1715078833

View Profile Personal Message (Offline)

Ignore
1715078833
Reply with quote  #2

1715078833
Report to moderator
1715078833
Hero Member
*
Offline Offline

Posts: 1715078833

View Profile Personal Message (Offline)

Ignore
1715078833
Reply with quote  #2

1715078833
Report to moderator
BASE16
Member
**
Offline Offline

Activity: 180
Merit: 38


View Profile
September 18, 2020, 09:32:29 AM
 #2

Code:
pubKey = keyPair.publicKey;

https://npmdoc.github.io/node-npmdoc-bitcoinjs-lib/build/apidoc.html

A lot of things change now and then so depending on the build you are using you can always check the master.

https://github.com/bitcoinjs/bitcoinjs-lib/tree/master/test
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!