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

Commit

Permalink
Use secp256k1 lib instead of shims.
Browse files Browse the repository at this point in the history
  • Loading branch information
nebojsa94 committed Jun 19, 2020
1 parent 1d72012 commit eda4d21
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
publicKeyCreate,
publicKeyVerify,
publicKeyConvert,
} = require('ethereum-cryptography/shims/hdkey-secp256k1v3')
} = require('ethereum-cryptography/secp256k1')
import * as assert from 'assert'
import * as BN from 'bn.js'
import { zeros, bufferToHex, toBuffer } from './bytes'
Expand Down Expand Up @@ -156,7 +156,7 @@ export const isValidPublic = function(publicKey: Buffer, sanitize: boolean = fal
export const pubToAddress = function(pubKey: Buffer, sanitize: boolean = false): Buffer {
assertIsBuffer(pubKey)
if (sanitize && pubKey.length !== 64) {
pubKey = toBuffer(publicKeyConvert(pubKey, false).slice(1))
pubKey = Buffer.from(publicKeyConvert(pubKey, false).slice(1))
}
assert(pubKey.length === 64)
// Only take the lower 160bits of the hash
Expand All @@ -179,7 +179,7 @@ export const privateToAddress = function(privateKey: Buffer): Buffer {
export const privateToPublic = function(privateKey: Buffer): Buffer {
assertIsBuffer(privateKey)
// skip the type flag and use the X, Y points
return toBuffer(publicKeyCreate(privateKey, false).slice(1))
return Buffer.from(publicKeyCreate(privateKey, false)).slice(1)
}

/**
Expand All @@ -188,7 +188,7 @@ export const privateToPublic = function(privateKey: Buffer): Buffer {
export const importPublic = function(publicKey: Buffer): Buffer {
assertIsBuffer(publicKey)
if (publicKey.length !== 64) {
publicKey = toBuffer(publicKeyConvert(publicKey, false).slice(1))
publicKey = Buffer.from(publicKeyConvert(publicKey, false).slice(1))
}
return publicKey
}
13 changes: 6 additions & 7 deletions src/signature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { sign, publicKeyConvert } = require('ethereum-cryptography/shims/hdkey-secp256k1v3')
const { ecdsaRecover } = require('ethereum-cryptography/secp256k1')
const { ecdsaSign, ecdsaRecover, publicKeyConvert } = require('ethereum-cryptography/secp256k1')
import * as BN from 'bn.js'
import { toBuffer, setLengthLeft, bufferToHex } from './bytes'
import { keccak } from './hash'
Expand All @@ -19,12 +18,12 @@ export const ecsign = function(
privateKey: Buffer,
chainId?: number,
): ECDSASignature {
const sig = sign(msgHash, privateKey)
const recovery: number = sig.recovery
const sig = ecdsaSign(msgHash, privateKey)
const recovery: number = sig.recid

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

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

/**
Expand Down

0 comments on commit eda4d21

Please sign in to comment.