Skip to content

Commit db0e00b

Browse files
committed
readme: ECDSA recovery public key example
1 parent 71e4e8e commit db0e00b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var ec = new EC('secp256k1');
5555
// Generate keys
5656
var key = ec.genKeyPair();
5757

58-
// Sign the message's hash (input must be an array, or a hex-string)
58+
// Sign the message's hash (input must be a byte array, or a hex-string)
5959
var msgHash = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
6060
var signature = key.sign(msgHash);
6161

@@ -95,6 +95,18 @@ var signature = { r: 'b1fc...', s: '9c42...' }; // case 3
9595

9696
// Verify signature
9797
console.log(key.verify(msgHash, signature));
98+
99+
// Recover public key from signature
100+
const BN = require("bn.js")
101+
102+
function hexToDec(hex) {
103+
return new BN(hex, 16).toString(10)
104+
}
105+
106+
var recoveryId = ec.getKeyRecoveryParam(msgHash, signature, key.getPublic());
107+
108+
// msgHash must be decimal number
109+
var recoveredPubKey = ec.recoverPubKey(hexToDec(msgHash), signature, recoveryId);
98110
```
99111

100112
### EdDSA

0 commit comments

Comments
 (0)