Skip to content

Commit 7d12beb

Browse files
holgerd77jochem-brouwer
authored andcommitted
tx -> EIP-2930: added generic sign(), getSenderAddress(), getSenderPublicKey(), verifySignature() tests
1 parent ceeedc6 commit 7d12beb

File tree

2 files changed

+61
-46
lines changed

2 files changed

+61
-46
lines changed

packages/tx/test/api.spec.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import tape from 'tape'
22
import { Buffer } from 'buffer'
3-
import { BN, rlp, privateToPublic, toBuffer, bufferToHex, unpadBuffer } from 'ethereumjs-util'
3+
import { BN, rlp, toBuffer, bufferToHex, unpadBuffer } from 'ethereumjs-util'
44
import Common from '@ethereumjs/common'
55
import { LegacyTransaction, TxData } from '../src'
66
import { TxsJsonEntry, VitaliksTestsDataEntry } from './types'
@@ -80,51 +80,6 @@ tape('[Transaction]', function (t) {
8080
st.end()
8181
})
8282

83-
t.test('sign()', function (st) {
84-
transactions.forEach(function (tx, i) {
85-
const { privateKey } = txFixtures[i]
86-
if (privateKey) {
87-
st.ok(tx.sign(Buffer.from(privateKey, 'hex')), 'should sign tx')
88-
}
89-
})
90-
st.end()
91-
})
92-
93-
t.test("should get sender's address after signing it", function (st) {
94-
transactions.forEach(function (tx, i) {
95-
const { privateKey, sendersAddress } = txFixtures[i]
96-
if (privateKey) {
97-
const signedTx = tx.sign(Buffer.from(privateKey, 'hex'))
98-
st.equals(signedTx.getSenderAddress().toString(), '0x' + sendersAddress)
99-
}
100-
})
101-
st.end()
102-
})
103-
104-
t.test("should get sender's public key after signing it", function (st) {
105-
transactions.forEach(function (tx, i) {
106-
const { privateKey } = txFixtures[i]
107-
if (privateKey) {
108-
const signedTx = tx.sign(Buffer.from(privateKey, 'hex'))
109-
const txPubKey = signedTx.getSenderPublicKey()
110-
const pubKeyFromPriv = privateToPublic(Buffer.from(privateKey, 'hex'))
111-
st.ok(txPubKey.equals(pubKeyFromPriv))
112-
}
113-
})
114-
st.end()
115-
})
116-
117-
t.test('should verify signing it', function (st) {
118-
transactions.forEach(function (tx, i) {
119-
const { privateKey } = txFixtures[i]
120-
if (privateKey) {
121-
const signedTx = tx.sign(Buffer.from(privateKey, 'hex'))
122-
st.ok(signedTx.verifySignature())
123-
}
124-
})
125-
st.end()
126-
})
127-
12883
t.test('should validate with string option', function (st) {
12984
transactions.forEach(function (tx) {
13085
st.ok(typeof tx.validate(true)[0] === 'string')

packages/tx/test/base.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Common from '@ethereumjs/common'
33
import { LegacyTransaction, EIP2930Transaction } from '../src'
44
import { TxsJsonEntry } from './types'
55
import { BaseTransaction } from '../src/baseTransaction'
6+
import { privateToPublic } from 'ethereumjs-util'
67

78
tape('[BaseTransaction]', function (t) {
89
const legacyFixtures: TxsJsonEntry[] = require('./json/txs.json')
@@ -144,7 +145,66 @@ tape('[BaseTransaction]', function (t) {
144145
st.notOk(tx.validate(), `${txType.name}: should not validate correctly`)
145146
})
146147
}
148+
st.end()
149+
})
150+
151+
t.test('sign()', function (st) {
152+
for (const txType of txTypes) {
153+
txType.txs.forEach(function (tx: any, i: number) {
154+
const { privateKey } = txType.fixtures[i]
155+
if (privateKey) {
156+
st.ok(tx.sign(Buffer.from(privateKey, 'hex')), `${txType.name}: should sign tx`)
157+
}
158+
})
159+
}
160+
st.end()
161+
})
162+
163+
t.test('getSenderAddress()', function (st) {
164+
for (const txType of txTypes) {
165+
txType.txs.forEach(function (tx: any, i: number) {
166+
const { privateKey, sendersAddress } = txType.fixtures[i]
167+
if (privateKey) {
168+
const signedTx = tx.sign(Buffer.from(privateKey, 'hex'))
169+
st.equals(
170+
signedTx.getSenderAddress().toString(),
171+
`0x${sendersAddress}`,
172+
`${txType.name}: should get sender's address after signing it`
173+
)
174+
}
175+
})
176+
}
177+
st.end()
178+
})
179+
180+
t.test('getSenderPublicKey()', function (st) {
181+
for (const txType of txTypes) {
182+
txType.txs.forEach(function (tx: any, i: number) {
183+
const { privateKey } = txType.fixtures[i]
184+
if (privateKey) {
185+
const signedTx = tx.sign(Buffer.from(privateKey, 'hex'))
186+
const txPubKey = signedTx.getSenderPublicKey()
187+
const pubKeyFromPriv = privateToPublic(Buffer.from(privateKey, 'hex'))
188+
st.ok(
189+
txPubKey.equals(pubKeyFromPriv),
190+
`${txType.name}: should get sender's public key after signing it`
191+
)
192+
}
193+
})
194+
}
195+
st.end()
196+
})
147197

198+
t.test('verifySignature()', function (st) {
199+
for (const txType of txTypes) {
200+
txType.txs.forEach(function (tx: any, i: number) {
201+
const { privateKey } = txType.fixtures[i]
202+
if (privateKey) {
203+
const signedTx = tx.sign(Buffer.from(privateKey, 'hex'))
204+
st.ok(signedTx.verifySignature(), `${txType.name}: should verify signing it`)
205+
}
206+
})
207+
}
148208
st.end()
149209
})
150210
})

0 commit comments

Comments
 (0)