Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
support newer versons of ethereumjs/tx with immutability
Browse files Browse the repository at this point in the history
  • Loading branch information
brad-decker committed Mar 24, 2021
1 parent 7b3825f commit fb6e9df
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 7 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ class SimpleKeyring extends EventEmitter {
// tx is an instance of the ethereumjs-transaction class.
signTransaction (address, tx, opts = {}) {
const privKey = this.getPrivateKeyFor(address, opts)
tx.sign(privKey)
return Promise.resolve(tx)
const signedTx = tx.sign(privKey)
// Newer versions of Ethereumjs-tx are immutable and return a new tx object
return Promise.resolve(signedTx === undefined ? tx : signedTx)
}

// For eth_sign, we need to sign arbitrary data:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"events": "^1.1.1"
},
"devDependencies": {
"@ethereumjs/tx": "^3.1.1",
"@metamask/eslint-config": "^3.2.0",
"chai": "^4.1.2",
"eslint": "^7.2.0",
Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert')
const ethUtil = require('ethereumjs-util')
const sigUtil = require('eth-sig-util')
const { Transaction: EthereumTx } = require('ethereumjs-tx')
const { TransactionFactory } = require('@ethereumjs/tx')
const { expect } = require('chai')
const SimpleKeyring = require('..')

Expand Down Expand Up @@ -82,6 +83,23 @@ describe('simple-keyring', () => {
const signed = await keyring.signTransaction(address, tx)
assert.ok(signed.raw, 'has a raw signature')
})

it('returns a signed tx object when using newer versions of ethereumjs/tx', async () => {
await keyring.deserialize([privateKey])

const txParams = {
from: address,
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: address,
value: '0x1000',
}
const tx = TransactionFactory.fromTxData(txParams)

const signed = await keyring.signTransaction(address, tx)
assert.ok(signed.raw, 'has a raw signature')
})
})

describe('#signMessage', () => {
Expand Down
Loading

0 comments on commit fb6e9df

Please sign in to comment.