Skip to content

feat: decouple ecpair from tiny-secp256k1 (allow injecting ecc lib) #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 15, 2021
Merged
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ A library for managing SECP256k1 keypairs written in TypeScript with transpiled
TypeScript

``` typescript
import { Signer, SignerAsync, ECPairInterface, ECPair } from 'ecpair';
import { Signer, SignerAsync, ECPairInterface, ECPairFactory, ECPairAPI, TinySecp256k1Interface } from 'ecpair';
import * as crypto from 'crypto';

// You need to provide the ECC library. The ECC library must implement
// all the methods of the `TinySecp256k1Interface` interface.
const tinysecp: TinySecp256k1Interface = require('tiny-secp256k1');
const ECPair: ECPairAPI = ECPairFactory(tinysecp);

// You don't need to explicitly write ECPairInterface, but just to show
// that ECPair implements the interface this example includes it.
// that the keyPair implements the interface this example includes it.

// From WIF
const keyPair1: ECPairInterface = ECPair.fromWIF('KynD8ZKdViVo5W82oyxvE18BbG6nZPVQ8Td8hYbwU94RmyUALUik');
Expand Down
91 changes: 15 additions & 76 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ecpair",
"version": "1.0.1",
"version": "2.0.0",
"description": "Client-side Bitcoin JavaScript library ECPair",
"main": "./src/ecpair.js",
"types": "./src/ecpair.d.ts",
Expand Down Expand Up @@ -47,10 +47,9 @@
"src"
],
"dependencies": {
"randombytes": "^2.0.1",
"tiny-secp256k1": "^1.1.6",
"typeforce": "^1.11.3",
"wif": "^2.0.1"
"randombytes": "^2.1.0",
"typeforce": "^1.18.0",
"wif": "^2.0.6"
},
"devDependencies": {
"@types/mocha": "^5.2.7",
Expand All @@ -72,6 +71,7 @@
"prettier": "^2.4.1",
"proxyquire": "^2.0.1",
"rimraf": "^2.6.3",
"tiny-secp256k1": "^2.1.2",
"ts-node": "^8.3.0",
"tslint": "^6.1.3",
"typescript": "^4.4.4"
Expand Down
36 changes: 19 additions & 17 deletions src/ecpair.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ export interface ECPairInterface extends Signer {
privateKey?: Buffer;
toWIF(): string;
verify(hash: Buffer, signature: Buffer): boolean;
verifySchnorr(hash: Buffer, signature: Buffer): boolean;
signSchnorr(hash: Buffer): Buffer;
}
export declare class ECPair implements ECPairInterface {
private __D?;
private __Q?;
static isPoint(maybePoint: any): boolean;
static fromPrivateKey(buffer: Buffer, options?: ECPairOptions): ECPair;
static fromPublicKey(buffer: Buffer, options?: ECPairOptions): ECPair;
static fromWIF(wifString: string, network?: Network | Network[]): ECPair;
static makeRandom(options?: ECPairOptions): ECPair;
compressed: boolean;
network: Network;
lowR: boolean;
protected constructor(__D?: Buffer | undefined, __Q?: Buffer | undefined, options?: ECPairOptions);
get privateKey(): Buffer | undefined;
get publicKey(): Buffer;
toWIF(): string;
sign(hash: Buffer, lowR?: boolean): Buffer;
verify(hash: Buffer, signature: Buffer): boolean;
export interface ECPairAPI {
isPoint(maybePoint: any): boolean;
fromPrivateKey(buffer: Buffer, options?: ECPairOptions): ECPairInterface;
fromPublicKey(buffer: Buffer, options?: ECPairOptions): ECPairInterface;
fromWIF(wifString: string, network?: Network | Network[]): ECPairInterface;
makeRandom(options?: ECPairOptions): ECPairInterface;
}
export interface TinySecp256k1Interface {
isPoint(p: Uint8Array): boolean;
pointCompress(p: Uint8Array, compressed?: boolean): Uint8Array;
isPrivate(d: Uint8Array): boolean;
pointFromScalar(d?: Uint8Array, compressed?: boolean): Uint8Array;
sign(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array;
signSchnorr?(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array;
verify(h: Uint8Array, Q: Uint8Array, signature: Uint8Array, strict?: boolean): boolean;
verifySchnorr?(h: Uint8Array, Q: Uint8Array, signature: Uint8Array): boolean;
}
export declare function ECPairFactory(ecc: TinySecp256k1Interface): ECPairAPI;
Loading