Skip to content

Commit eda4d21

Browse files
committed
Use secp256k1 lib instead of shims.
1 parent 1d72012 commit eda4d21

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/account.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
publicKeyCreate,
55
publicKeyVerify,
66
publicKeyConvert,
7-
} = require('ethereum-cryptography/shims/hdkey-secp256k1v3')
7+
} = require('ethereum-cryptography/secp256k1')
88
import * as assert from 'assert'
99
import * as BN from 'bn.js'
1010
import { zeros, bufferToHex, toBuffer } from './bytes'
@@ -156,7 +156,7 @@ export const isValidPublic = function(publicKey: Buffer, sanitize: boolean = fal
156156
export const pubToAddress = function(pubKey: Buffer, sanitize: boolean = false): Buffer {
157157
assertIsBuffer(pubKey)
158158
if (sanitize && pubKey.length !== 64) {
159-
pubKey = toBuffer(publicKeyConvert(pubKey, false).slice(1))
159+
pubKey = Buffer.from(publicKeyConvert(pubKey, false).slice(1))
160160
}
161161
assert(pubKey.length === 64)
162162
// Only take the lower 160bits of the hash
@@ -179,7 +179,7 @@ export const privateToAddress = function(privateKey: Buffer): Buffer {
179179
export const privateToPublic = function(privateKey: Buffer): Buffer {
180180
assertIsBuffer(privateKey)
181181
// skip the type flag and use the X, Y points
182-
return toBuffer(publicKeyCreate(privateKey, false).slice(1))
182+
return Buffer.from(publicKeyCreate(privateKey, false)).slice(1)
183183
}
184184

185185
/**
@@ -188,7 +188,7 @@ export const privateToPublic = function(privateKey: Buffer): Buffer {
188188
export const importPublic = function(publicKey: Buffer): Buffer {
189189
assertIsBuffer(publicKey)
190190
if (publicKey.length !== 64) {
191-
publicKey = toBuffer(publicKeyConvert(publicKey, false).slice(1))
191+
publicKey = Buffer.from(publicKeyConvert(publicKey, false).slice(1))
192192
}
193193
return publicKey
194194
}

src/signature.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const { sign, publicKeyConvert } = require('ethereum-cryptography/shims/hdkey-secp256k1v3')
2-
const { ecdsaRecover } = require('ethereum-cryptography/secp256k1')
1+
const { ecdsaSign, ecdsaRecover, publicKeyConvert } = require('ethereum-cryptography/secp256k1')
32
import * as BN from 'bn.js'
43
import { toBuffer, setLengthLeft, bufferToHex } from './bytes'
54
import { keccak } from './hash'
@@ -19,12 +18,12 @@ export const ecsign = function(
1918
privateKey: Buffer,
2019
chainId?: number,
2120
): ECDSASignature {
22-
const sig = sign(msgHash, privateKey)
23-
const recovery: number = sig.recovery
21+
const sig = ecdsaSign(msgHash, privateKey)
22+
const recovery: number = sig.recid
2423

2524
const ret = {
26-
r: toBuffer(sig.signature.slice(0, 32)),
27-
s: toBuffer(sig.signature.slice(32, 64)),
25+
r: Buffer.from(sig.signature.slice(0, 32)),
26+
s: Buffer.from(sig.signature.slice(32, 64)),
2827
v: chainId ? recovery + (chainId * 2 + 35) : recovery + 27,
2928
}
3029

@@ -48,7 +47,7 @@ export const ecrecover = function(
4847
throw new Error('Invalid signature v value')
4948
}
5049
const senderPubKey = ecdsaRecover(signature, recovery, msgHash)
51-
return toBuffer(publicKeyConvert(senderPubKey, false).slice(1))
50+
return Buffer.from(publicKeyConvert(senderPubKey, false).slice(1))
5251
}
5352

5453
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy