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

Commit

Permalink
Remove ethereumjs-util dependency (#96)
Browse files Browse the repository at this point in the history
The dependency `ethereumjs-util` has been removed. It was only for a
few trivial things that were easy to replace.
  • Loading branch information
Gudahtt authored Jun 28, 2021
1 parent 445b74e commit c7f7e2e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { EventEmitter } = require('events')
const ethUtil = require('ethereumjs-util')
const bip39 = require('bip39')
const ObservableStore = require('obs-store')
const encryptor = require('browser-passworder')
Expand All @@ -13,6 +12,18 @@ const keyringTypes = [
HdKeyring,
]

/**
* Strip the hex prefix from an address, if present
* @param {string} address - The address that might be hex prefixed.
* @returns {string} The address without a hex prefix.
*/
function stripHexPrefix (address) {
if (address.startsWith('0x')) {
return address.slice(2)
}
return address
}

class KeyringController extends EventEmitter {

//
Expand Down Expand Up @@ -243,7 +254,7 @@ class KeyringController extends EventEmitter {
accounts.find(
(key) => (
key === newAccountArray[0] ||
key === ethUtil.stripHexPrefix(newAccountArray[0])),
key === stripHexPrefix(newAccountArray[0])),
),
)
return isIncluded
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"eth-hd-keyring": "^3.6.0",
"eth-sig-util": "^3.0.1",
"eth-simple-keyring": "^4.2.0",
"ethereumjs-util": "^7.0.9",
"obs-store": "^4.0.3"
},
"files": [
Expand Down
3 changes: 1 addition & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { strict: assert } = require('assert')
const ethUtil = require('ethereumjs-util')
const sigUtil = require('eth-sig-util')

const normalizeAddress = sigUtil.normalize
Expand Down Expand Up @@ -269,7 +268,7 @@ describe('KeyringController', function () {

const privateAppKey = await keyringController.exportAppKeyForAddress(address, 'someapp.origin.io')

const wallet = Wallet.fromPrivateKey(ethUtil.toBuffer(`0x${privateAppKey}`))
const wallet = Wallet.fromPrivateKey(Buffer.from(privateAppKey, 'hex'))
const recoveredAddress = `0x${wallet.getAddress().toString('hex')}`

assert.equal(recoveredAddress, appKeyAddress, 'Exported the appropriate private key')
Expand Down

0 comments on commit c7f7e2e

Please sign in to comment.