forked from ethereumjs/ethereumjs-util
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsignature.ts
More file actions
152 lines (131 loc) · 4.19 KB
/
signature.ts
File metadata and controls
152 lines (131 loc) · 4.19 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { ecdsaSign, ecdsaRecover, publicKeyConvert } = require('ethereum-cryptography/secp256k1')
import * as BN from 'bn.js'
import { toBuffer, setLengthLeft, bufferToHex } from './bytes'
import { keccak } from './hash'
import { assertIsBuffer } from './helpers'
export interface ECDSASignature {
v: number
r: Buffer
s: Buffer
}
/**
* Returns the ECDSA signature of a message hash.
*/
export const ecsign = function(
msgHash: Buffer,
privateKey: Buffer,
chainId?: number,
): ECDSASignature {
const sig = ecdsaSign(msgHash, privateKey)
const recovery: number = sig.recid
const ret = {
r: Buffer.from(sig.signature.slice(0, 32)),
s: Buffer.from(sig.signature.slice(32, 64)),
v: chainId ? recovery + (chainId * 2 + 35) : recovery + 27,
}
return ret
}
/**
* ECDSA public key recovery from signature.
* @returns Recovered public key
*/
export const ecrecover = function(
msgHash: Buffer,
v: number,
r: Buffer,
s: Buffer,
chainId?: number,
): Buffer {
const signature = Buffer.concat([setLengthLeft(r, 32), setLengthLeft(s, 32)], 64)
const recovery = calculateSigRecovery(v, chainId)
if (!isValidSigRecovery(recovery)) {
throw new Error('Invalid signature v value')
}
const senderPubKey = ecdsaRecover(signature, recovery, msgHash)
return Buffer.from(publicKeyConvert(senderPubKey, false).slice(1))
}
/**
* Convert signature parameters into the format of `eth_sign` RPC method.
* @returns Signature
*/
export const toRpcSig = function(v: number, r: Buffer, s: Buffer, chainId?: number): string {
const recovery = calculateSigRecovery(v, chainId)
if (!isValidSigRecovery(recovery)) {
throw new Error('Invalid signature v value')
}
// geth (and the RPC eth_sign method) uses the 65 byte format used by Bitcoin
return bufferToHex(Buffer.concat([setLengthLeft(r, 32), setLengthLeft(s, 32), toBuffer(v)]))
}
/**
* Convert signature format of the `eth_sign` RPC method to signature parameters
* NOTE: all because of a bug in geth: https://github.com/ethereum/go-ethereum/issues/2053
*/
export const fromRpcSig = function(sig: string): ECDSASignature {
const buf: Buffer = toBuffer(sig)
// NOTE: with potential introduction of chainId this might need to be updated
if (buf.length !== 65) {
throw new Error('Invalid signature length')
}
let v = buf[64]
// support both versions of `eth_sign` responses
if (v < 27) {
v += 27
}
return {
v: v,
r: buf.slice(0, 32),
s: buf.slice(32, 64),
}
}
/**
* Validate a ECDSA signature.
* @param homesteadOrLater Indicates whether this is being used on either the homestead hardfork or a later one
*/
export const isValidSignature = function(
v: number,
r: Buffer,
s: Buffer,
homesteadOrLater: boolean = true,
chainId?: number,
): boolean {
const SECP256K1_N_DIV_2 = new BN(
'7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0',
16,
)
const SECP256K1_N = new BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16)
if (r.length !== 32 || s.length !== 32) {
return false
}
if (!isValidSigRecovery(calculateSigRecovery(v, chainId))) {
return false
}
const rBN: BN = new BN(r)
const sBN: BN = new BN(s)
if (rBN.isZero() || rBN.gt(SECP256K1_N) || sBN.isZero() || sBN.gt(SECP256K1_N)) {
return false
}
if (homesteadOrLater && sBN.cmp(SECP256K1_N_DIV_2) === 1) {
return false
}
return true
}
/**
* Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call.
* The output of this function can be fed into `ecsign` to produce the same signature as the `eth_sign`
* call for a given `message`, or fed to `ecrecover` along with a signature to recover the public key
* used to produce the signature.
*/
export const hashPersonalMessage = function(message: Buffer): Buffer {
assertIsBuffer(message)
const prefix = Buffer.from(
`\u0019Ethereum Signed Message:\n${message.length.toString()}`,
'utf-8',
)
return keccak(Buffer.concat([prefix, message]))
}
function calculateSigRecovery(v: number, chainId?: number): number {
return chainId ? v - (2 * chainId + 35) : v - 27
}
function isValidSigRecovery(recovery: number): boolean {
return recovery === 0 || recovery === 1
}