ECDSA Signature Verification
Description
Using Truebit To Execute ECDSA Verification Example
The Code
Source
/*
Signee address recoverer and validator
======================================
This task is an example on how to compute a signature verification using the ECDSA algorithm, the one used in EVM blockchains like Ethereum, Polygon, Hyperledger Besu, Avalanche C-Chain and others. It takes several paramaters as an input, all of them contained in an input.txt file provided as example:
Input parameters (input.txt)
----------------------------
A json-formatted file. See the example below:
{
"messageHash": "0x13260446136602c31107aa5e08571c276026b60c5156c1a564e268f3ab08492c",
"signature": {
"r": "0x7097b9a0810d13e60182f261708c2c260e86ea09853cd48b184a5a4b4ea08c02",
"s": "0x087bd86b8cdee3aec88ac365ec533c9a111e5b5586c4941a790ca46970c3f380",
"v": 28
},
"address": "0x63f9a92d8d61b48a9fff8d58080425a3012d05c8"
}
Fields:
messageHash: "0x" + hex-encoded data (ASCII string, 66 chars).
signature: ECDSA signature, divided in its 'r', 's' and 'v'
components, as specified below.
r: "0x" + hex-encoded data (ASCII string, 64 chars).
s: "0x" + hex-encoded data (ASCII string, 64 chars).
v: numeric parameter.
address: "0x" + hex-encoded data (ASCII string, 42 chars).
Output values (output.txt)
--------------------------
A JSON-formatted file. See the example below:
{
"declaredAddress": "0x63f9a92d8d61b48a9fff8d58080425a3012d05c8",
"derivedAddress": "0x63f9a92d8d61b48a9fff8d58080425a3012d05c8",
"match": true
}
*/
const fs = require('fs');
const elliptic = require('elliptic');
const keccak256 = require('js-sha3').keccak256;
const bn = require('bn.js');
function checkSignature(messageHash, signature, address) {
const ec = new elliptic.ec('secp256k1');
const bnHash = new bn(messageHash, 16);
const recoveryByteV = signature.v - 27;
const pubKeyRecovered = ec.recoverPubKey(bnHash, signature, recoveryByteV).encode('hex');
const pubKeyHash = keccak256(Buffer.from(pubKeyRecovered.slice(2), 'hex'));
const derivedAddress = pubKeyHash.slice(-40);
return {
declaredAddress: '0x' + address,
derivedAddress: '0x' + derivedAddress,
match: address === derivedAddress
}
}
function fixHash(input, key) {
if (typeof input[key] !== 'string') {
throw (`invalid type on input key '${key}', not a string.`);
}
input[key] = input[key].replace('0x', '').toLowerCase();
}
const main = () => {
const inputJson = fs.readFileSync('input.txt', 'utf-8');
let inputObj = JSON.parse(inputJson);
fixHash(inputObj, 'messageHash');
fixHash(inputObj.signature, 'r');
fixHash(inputObj.signature, 's');
fixHash(inputObj, 'address');
const outputObj = checkSignature(inputObj.messageHash, inputObj.signature, inputObj.address);
const outputJson = JSON.stringify(outputObj, null, 4);
fs.writeFileSync('output.txt', outputJson);
console.log(outputJson);
}
main();In/Out Parameters
Try Out
Step 1: Create The Ecdsa-Verify Source Code
Step 2: Build The Source Code
Output
Step 3: Try The Code
Output
Step 4: Deploy The Task
Output
Last updated
Was this helpful?