Skip to content
This repository was archived by the owner on Jun 17, 2021. It is now read-only.

Commit da4827c

Browse files
committed
Remove keccak native dependency.
1 parent 58c2476 commit da4827c

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@
9292
"@types/bn.js": "^4.11.3",
9393
"bn.js": "^5.1.2",
9494
"create-hash": "^1.1.2",
95+
"ethereum-cryptography": "^0.1.3",
9596
"ethjs-util": "0.1.6",
96-
"keccak": "^3.0.0",
9797
"rlp": "^2.2.4",
9898
"secp256k1": "^4.0.1"
9999
},

src/account.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const ethjsUtil = require('ethjs-util')
2+
const {
3+
privateKeyVerify,
4+
publicKeyCreate,
5+
publicKeyVerify,
6+
publicKeyConvert,
7+
} = require('ethereum-cryptography/shims/hdkey-secp256k1v3')
28
import * as assert from 'assert'
3-
import * as secp256k1 from 'secp256k1'
49
import * as BN from 'bn.js'
510
import { zeros, bufferToHex, toBuffer } from './bytes'
611
import { keccak, keccak256, keccakFromString, rlphash } from './hash'
@@ -119,7 +124,7 @@ export const generateAddress2 = function(from: Buffer, salt: Buffer, initCode: B
119124
* Checks if the private key satisfies the rules of the curve secp256k1.
120125
*/
121126
export const isValidPrivate = function(privateKey: Buffer): boolean {
122-
return secp256k1.privateKeyVerify(privateKey)
127+
return privateKeyVerify(privateKey)
123128
}
124129

125130
/**
@@ -132,14 +137,14 @@ export const isValidPublic = function(publicKey: Buffer, sanitize: boolean = fal
132137
assertIsBuffer(publicKey)
133138
if (publicKey.length === 64) {
134139
// Convert to SEC1 for secp256k1
135-
return secp256k1.publicKeyVerify(Buffer.concat([Buffer.from([4]), publicKey]))
140+
return publicKeyVerify(Buffer.concat([Buffer.from([4]), publicKey]))
136141
}
137142

138143
if (!sanitize) {
139144
return false
140145
}
141146

142-
return secp256k1.publicKeyVerify(publicKey)
147+
return publicKeyVerify(publicKey)
143148
}
144149

145150
/**
@@ -151,7 +156,7 @@ export const isValidPublic = function(publicKey: Buffer, sanitize: boolean = fal
151156
export const pubToAddress = function(pubKey: Buffer, sanitize: boolean = false): Buffer {
152157
assertIsBuffer(pubKey)
153158
if (sanitize && pubKey.length !== 64) {
154-
pubKey = toBuffer(secp256k1.publicKeyConvert(pubKey, false).slice(1))
159+
pubKey = toBuffer(publicKeyConvert(pubKey, false).slice(1))
155160
}
156161
assert(pubKey.length === 64)
157162
// Only take the lower 160bits of the hash
@@ -174,7 +179,7 @@ export const privateToAddress = function(privateKey: Buffer): Buffer {
174179
export const privateToPublic = function(privateKey: Buffer): Buffer {
175180
assertIsBuffer(privateKey)
176181
// skip the type flag and use the X, Y points
177-
return toBuffer(secp256k1.publicKeyCreate(privateKey, false).slice(1))
182+
return toBuffer(publicKeyCreate(privateKey, false).slice(1))
178183
}
179184

180185
/**
@@ -183,7 +188,7 @@ export const privateToPublic = function(privateKey: Buffer): Buffer {
183188
export const importPublic = function(publicKey: Buffer): Buffer {
184189
assertIsBuffer(publicKey)
185190
if (publicKey.length !== 64) {
186-
publicKey = toBuffer(secp256k1.publicKeyConvert(publicKey, false).slice(1))
191+
publicKey = toBuffer(publicKeyConvert(publicKey, false).slice(1))
187192
}
188193
return publicKey
189194
}

src/hash.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const createKeccakHash = require('keccak')
1+
const { keccak224, keccak384, keccak256: k256, keccak512 } = require('ethereum-cryptography/keccak')
22
const createHash = require('create-hash')
33
const ethjsUtil = require('ethjs-util')
44
import * as rlp from 'rlp'
@@ -12,9 +12,23 @@ import { assertIsString, assertIsBuffer, assertIsArray, assertIsHexString } from
1212
*/
1313
export const keccak = function(a: Buffer, bits: number = 256): Buffer {
1414
assertIsBuffer(a)
15-
return createKeccakHash(`keccak${bits}`)
16-
.update(a)
17-
.digest()
15+
switch (bits) {
16+
case 224: {
17+
return keccak224(a)
18+
}
19+
case 256: {
20+
return k256(a)
21+
}
22+
case 384: {
23+
return keccak384(a)
24+
}
25+
case 512: {
26+
return keccak512(a)
27+
}
28+
default: {
29+
throw new Error(`Invald algorithm: keccak${bits}`)
30+
}
31+
}
1832
}
1933

2034
/**

src/signature.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { recover, sign, publicKeyConvert } = require('ethereum-cryptography/shims/hdkey-secp256k1v3')
12
import * as secp256k1 from 'secp256k1'
23
import * as BN from 'bn.js'
34
import { toBuffer, setLengthLeft, bufferToHex } from './bytes'
@@ -18,8 +19,8 @@ export const ecsign = function(
1819
privateKey: Buffer,
1920
chainId?: number,
2021
): ECDSASignature {
21-
const sig = secp256k1.ecdsaSign(msgHash, privateKey)
22-
const recovery: number = sig.recid
22+
const sig = sign(msgHash, privateKey)
23+
const recovery: number = sig.recovery
2324

2425
const ret = {
2526
r: toBuffer(sig.signature.slice(0, 32)),
@@ -47,7 +48,7 @@ export const ecrecover = function(
4748
throw new Error('Invalid signature v value')
4849
}
4950
const senderPubKey = secp256k1.ecdsaRecover(signature, recovery, msgHash)
50-
return toBuffer(secp256k1.publicKeyConvert(senderPubKey, false).slice(1))
51+
return toBuffer(publicKeyConvert(senderPubKey, false).slice(1))
5152
}
5253

5354
/**

test/externals.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as assert from 'assert'
22

33
import * as BN_export from 'bn.js'
44
import * as rlp_export from 'rlp'
5-
import * as secp256k1_export from 'secp256k1'
65

76
import * as src from '../src'
87

0 commit comments

Comments
 (0)