forked from tgrospic/rnode-client-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eth-sign.js
44 lines (36 loc) · 1.59 KB
/
eth-sign.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
import { ec } from 'elliptic'
import * as ethUtil from 'ethereumjs-util'
import { decodeAscii } from '../lib.js'
import { deployDataProtobufSerialize } from '../rnode-sign.js'
export const recoverPublicKeyEth = (data, sigHex) => {
// Ethereum lib to recover public key from massage and signature
const hashed = ethUtil.hashPersonalMessage(ethUtil.toBuffer([...data]))
const sigBytes = ethUtil.toBuffer(sigHex)
const {v, r, s} = ethUtil.fromRpcSig(sigBytes)
// Public key without prefix
const pubkeyRecover = ethUtil.ecrecover(hashed, v, r, s)
return ethUtil.bufferToHex([4, ...pubkeyRecover])
}
export const verifyDeployEth = deploySigned => {
const {
term, timestamp, phloPrice, phloLimit, validAfterBlockNumber, shardId,
deployer, sig, // : Array[Byte]
} = deploySigned
// Serialize deploy data for signing
const deploySerialized = deployDataProtobufSerialize({
term, timestamp, phloPrice, phloLimit, validAfterBlockNumber, shardId,
})
// Create a hash of message with prefix
// https://github.com/ethereumjs/ethereumjs-util/blob/4a8001c/src/signature.ts#L136
const deployLen = deploySerialized.length
const msgPrefix = `\x19Ethereum Signed Message:\n${deployLen}`
const prefixBin = decodeAscii(msgPrefix)
const msg = ethUtil.toBuffer([...prefixBin, ...deploySerialized])
const hashed = ethUtil.keccak256(msg)
// Check deployer's signature
const crypt = new ec('secp256k1')
const key = crypt.keyFromPublic(deployer)
const sigRS = { r: sig.slice(0, 32), s: sig.slice(32, 64) }
const isValid = key.verify(hashed, sigRS)
return isValid
}