forked from ethereumjs/ethereumjs-tx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdsaOps.js
67 lines (58 loc) · 1.64 KB
/
ecdsaOps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const ecdsa = require('secp256k1');
const BN = require('bn.js');
const utils = require('ethereumjs-util');
/**
* @method verifySignature
* @return {Boolean}
*/
exports.txVerifySignature = function() {
var msgHash = this.hash(false);
var sig = Buffer.concat([utils.pad(this.r, 32), utils.pad(this.s, 32)], 64);
this._senderPubKey = ecdsa.recoverCompact(msgHash, sig, utils.bufferToInt(this.v) - 27);
if (this._senderPubKey && this._senderPubKey.toString('hex') !== '') {
return true;
} else {
return false;
}
};
/**
* sign a transaction given a private key
* @method sign
* @param {Buffer} privateKey
*/
exports.txSign = function(privateKey) {
var msgHash = this.hash(false),
sig = ecdsa.signCompact(privateKey, msgHash);
this.r = sig.r;
this.s = sig.s;
this.v = sig.recoveryId + 27;
};
/**
* gets the senders public key
* @method getSenderPublicKey
* @return {Buffer}
*/
exports.txGetSenderPublicKey = function() {
if (!this._senderPubKey || !this._senderPubKey.length) {
this.verifySignature();
}
return this._senderPubKey;
};
/**
* ecrecover
* @param {Buffer} msgHash [description]
* @param {Buffer} v [description]
* @param {Buffer} r [description]
* @param {Buffer} s [description]
* @return {Buffer} public key otherwise null
*/
exports.ecrecover = function(msgHash, v, r, s) {
var sig = Buffer.concat([utils.pad(r, 32), utils.pad(s, 32)], 64);
var recid = utils.bufferToInt(v) - 27;
var senderPubKey = ecdsa.recoverCompact(msgHash, sig, recid);
if (senderPubKey && senderPubKey.toString('hex') !== '') {
return senderPubKey;
} else {
return null;
}
};