-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
132 lines (106 loc) · 3.88 KB
/
test.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
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
const signer = require('./')
const crypto = require('crypto')
const { rlp } = require('eth-serde')
const test = require('tape')
const vectors = require('./vectors.json')
test('sign', t => {
var tx = {
nonce: 'ca1',
gasPrice: Buffer.from('0009184e72a000', 'hex'),
gasLimit: Buffer.from('002710', 'hex'),
to: Buffer.from('0000000000000000000000000000000000000001', 'hex'),
value: Buffer.from('00', 'hex'),
data: Buffer.from('000000000000000000000000000000000000000000000000000000600057', 'hex')
}
var privKey = crypto.randomBytes(32)
const signed = signer.sign(tx, privKey)
t.ok(signer.verify(signed.tx))
t.end()
})
test('sign: string input', t => {
var tx = {
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}
var privKey = crypto.randomBytes(32)
const signed = signer.sign(tx, privKey)
t.ok(signer.verify(signed.tx))
t.end()
})
test('verify: vectors', t => {
var raw = Buffer.from('f86c81dc8501984ab39182520894361c7a56cb86ac9c3fe3f47504ac1da63fc6137f872516ff52ce08008026a0e74da2d5c587083586fa877627c47b5925c1e60453bf83601a55f9775415c142a0187e9e8a3b36677961669841bb1f3005cd6578af0e0f7fc7b164b0bd06e6382d', 'hex')
var tx = format(raw)
var chainId = tx.v[0] > 30 ? 1 : null
t.ok(signer.verify(tx, chainId))
for (const v of vectors.map(a => Buffer.from(a.slice(2), 'hex'))) {
tx = format(v)
t.ok(signer.verify(tx))
}
t.end()
})
test('verify: vectors', t => {
var raw = Buffer.from('f86c81dc8501984ab39182520894361c7a56cb86ac9c3fe3f47504ac1da63fc6137f872516ff52ce08008026a0e74da2d5c587083586fa877627c47b5925c1e60453bf83601a55f9775415c142a0187e9e8a3b36677961669841bb1f3005cd6578af0e0f7fc7b164b0bd06e6382d', 'hex')
var tx = format(raw, true, true)
var chainId = tx.v[0] > 30 ? 1 : null
t.ok(signer.verify(tx, chainId))
for (const v of vectors.map(a => Buffer.from(a.slice(2), 'hex'))) {
tx = format(v, true, true)
t.ok(signer.verify(tx))
}
t.end()
})
test('verify: vectors raw buffer', t => {
var raw = Buffer.from('f86c81dc8501984ab39182520894361c7a56cb86ac9c3fe3f47504ac1da63fc6137f872516ff52ce08008026a0e74da2d5c587083586fa877627c47b5925c1e60453bf83601a55f9775415c142a0187e9e8a3b36677961669841bb1f3005cd6578af0e0f7fc7b164b0bd06e6382d', 'hex')
t.ok(signer.verify(raw))
for (const v of vectors.map(a => Buffer.from(a.slice(2), 'hex'))) {
t.ok(signer.verify(v))
}
t.end()
})
test("verify: vectors raw string with '0x'", t => {
var raw = Buffer.from('f86c81dc8501984ab39182520894361c7a56cb86ac9c3fe3f47504ac1da63fc6137f872516ff52ce08008026a0e74da2d5c587083586fa877627c47b5925c1e60453bf83601a55f9775415c142a0187e9e8a3b36677961669841bb1f3005cd6578af0e0f7fc7b164b0bd06e6382d', 'hex')
t.ok(signer.verify(raw))
for (const v of vectors) {
t.ok(signer.verify(v))
}
t.end()
})
test('sign: vectors', t => {
for (const v of vectors.map(a => Buffer.from(a.slice(2), 'hex'))) {
var key = crypto.randomBytes(32)
var tx = format(v, false)
var chainId = format.v > 30 ? 1 : null
const signed = signer.sign(tx, key, chainId)
t.ok(signer.verify(signed.tx, chainId))
}
t.end()
})
function format (str, toVerify = true, string = false) {
var items = rlp.decode(str)
if (string) {
items = items.map(a =>
typeof a === 'number'
? '0x' + a.toString(16)
: '0x' + a.toString('hex'))
}
var obj = {}
obj.nonce = Buffer.from([items[0]])
obj.gasPrice = items[1]
obj.gasLimit = items[2]
obj.to = items[3]
obj.value = items[4]
obj.data = items[5]
if (toVerify) {
obj.v = Buffer.from([items[6]])
obj.r = items[7]
obj.s = items[8]
}
if (string) obj.nonce = '0x' + obj.nonce.toString('hex')
if (string && obj.v) obj.v = '0x' + obj.v.toString('hex')
format.v = items[6]
return obj
}