Bitcoin: After signing a message, how to verify the signature with JavaScript/TypeScript?

Verification of signatures on Bitcoin nodes with JavaScript / Typescript

In order to check the signing of the Auser message on a Bitcoin node, we must make sure that the signature is the corrector and comes. In this article, we will give discussing how to achieve it using JavaScript / Typescript.

Configuration of the environment

Before you start, do yours, you have a node.js installed on your system. We also go to the “Crypto” module of the Caisse de la Caisse a shaping SHA-256 of Theer’s message.

`Javascript

Const Crypto = require ('crypto');

'

Signing the message with Bitcoin-Js

Bitcoin-JS is a JavaScript library which allows us to interact without Bitcoin. We can sign from the message use this library:

Javascript

Signmessage asynchronous function (knot, message) {

Const PrivateKey = wait node.getPrivateKey ();

Const Signature = Await PrivateKey.sign (message);

return signature;

}

'

Verification of the signature on a Bitcoin node

The one we signed the message with Bitcoin-JS, we must check it on a bitcoin. We will use the Bitcoinjs-Lib library for this purpose.

`Javascript

constant bitcoinjslib = require ('bitcoinjs-lib');

ASYNC FUNCTIONALITURE (knot, signature, message) {

Const publickey = wait node.getpublickey ();

Const isvalid = Wait Bitcoinjslib.VerifySignature (signature, publickey, message);

return isvalid;

}

'

Example of use

Here is an example of the house to you the functions:

Javascript

Const Bitcoinode = '

Const PrivateKey = Waits BitcoinNode.GetPrivateKey ();

// Sign the message with Bitcoin-Js

Signmessage (bitcoinnode,Sign this message to prove that you

If (Err) Console.error (Err);

other {

Const publickey = wait for BitcoinNode.getPublicKey ();

Const isvalid = verifysignature (bitcoinod, signature, signs the message to prove that you, this character);

console.log (isvalid); // should be true

}

});

`'

Error management

It is essential to properly manage the errors that Wen uses Bitcoin-JS. We can do by grabbing all the exceptions that can be

Javascript

Signmessage (bitcoinodus, sign this message to prove that you

If (Err) Console.error (Err);

});

` ‘

By following the and examples, we can check the signing of the Auser message on a Bitcoin node Use javaScript / Typescript / Typescript / Typescript.

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *