-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
index.d.ts
135 lines (135 loc) · 4.67 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* secp256k1 curve parameters. Equation is x³ + ax + b.
* Gx and Gy are generator coordinates. p is field order, n is group order.
*/
declare const CURVE: {
p: bigint;
n: bigint;
a: bigint;
b: bigint;
Gx: bigint;
Gy: bigint;
};
/** Alias to Uint8Array. */
export type Bytes = Uint8Array;
/** Hex-encoded string or Uint8Array. */
export type Hex = Bytes | string;
/** Hex-encoded string, Uint8Array or bigint. */
export type PrivKey = Hex | bigint;
/** Point in 2d xy affine coordinates. */
export interface AffinePoint {
x: bigint;
y: bigint;
}
/** Point in 3d xyz projective coordinates. */
declare class Point {
readonly px: bigint;
readonly py: bigint;
readonly pz: bigint;
constructor(px: bigint, py: bigint, pz: bigint);
static readonly BASE: Point;
static readonly ZERO: Point;
static fromAffine(p: AffinePoint): Point;
static fromHex(hex: Hex): Point;
static fromPrivateKey(k: PrivKey): Point;
get x(): bigint;
get y(): bigint;
equals(other: Point): boolean;
negate(): Point;
double(): Point;
add(other: Point): Point;
mul(n: bigint, safe?: boolean): Point;
mulAddQUns(R: Point, u1: bigint, u2: bigint): Point;
toAffine(): AffinePoint;
assertValidity(): Point;
multiply(n: bigint): Point;
aff(): AffinePoint;
ok(): Point;
toHex(isCompressed?: boolean): string;
toRawBytes(isCompressed?: boolean): Bytes;
}
/** Create public key from private. Output is compressed 33b or uncompressed 65b. */
declare const getPublicKey: (privKey: PrivKey, isCompressed?: boolean) => Bytes;
/** Signature which allows recovering pubkey from it. */
export type SignatureWithRecovery = Signature & {
recovery: number;
};
/** ECDSA Signature class. Supports only compact 64-byte representation, not DER. */
declare class Signature {
readonly r: bigint;
readonly s: bigint;
readonly recovery?: number | undefined;
constructor(r: bigint, s: bigint, recovery?: number | undefined);
static fromCompact(hex: Hex): Signature;
assertValidity(): Signature;
addRecoveryBit(rec: number): SignatureWithRecovery;
hasHighS(): boolean;
normalizeS(): Signature;
recoverPublicKey(msgh: Hex): Point;
toCompactRawBytes(): Bytes;
toCompactHex(): string;
}
type HmacFnSync = undefined | ((key: Bytes, ...msgs: Bytes[]) => Bytes);
type OptS = {
lowS?: boolean;
extraEntropy?: boolean | Hex;
};
type OptV = {
lowS?: boolean;
};
/** ECDSA signature generation. via secg.org/sec1-v2.pdf 4.1.2 + RFC6979 deterministic k. */
/**
* Sign a msg hash using secp256k1. Async.
* @param msgh - message HASH, not message itself e.g. sha256(message)
* @param priv - private key
*/
declare const signAsync: (msgh: Hex, priv: PrivKey, opts?: OptS) => Promise<SignatureWithRecovery>;
/**
* Sign a msg hash using secp256k1.
* @param msgh - message HASH, not message itself e.g. sha256(message)
* @param priv - private key
*/
declare const sign: (msgh: Hex, priv: PrivKey, opts?: OptS) => SignatureWithRecovery;
type SigLike = {
r: bigint;
s: bigint;
};
/**
* Verify a signature using secp256k1.
* @param sig - signature, 64-byte or Signature instance
* @param msgh - message HASH, not message itself e.g. sha256(message)
* @param pub - public key
*/
declare const verify: (sig: Hex | SigLike, msgh: Hex, pub: Hex, opts?: OptV) => boolean;
/**
* Elliptic Curve Diffie-Hellman (ECDH) on secp256k1.
* Result is **NOT hashed**. Use hash on it if you need.
* @param privA private key A
* @param pubB public key B
* @param isCompressed 33-byte or 65-byte output
* @returns public key C
*/
declare const getSharedSecret: (privA: Hex, pubB: Hex, isCompressed?: boolean) => Bytes;
/** Math, hex, byte helpers. Not in `utils` because utils share API with noble-curves. */
declare const etc: {
hexToBytes: (hex: string) => Bytes;
bytesToHex: (bytes: Bytes) => string;
concatBytes: (...arrs: Bytes[]) => Bytes;
bytesToNumberBE: (a: Bytes) => bigint;
numberToBytesBE: (n: bigint) => Bytes;
mod: (a: bigint, md?: bigint) => bigint;
invert: (num: bigint, md?: bigint) => bigint;
hmacSha256Async: (key: Bytes, ...msgs: Bytes[]) => Promise<Bytes>;
hmacSha256Sync: HmacFnSync;
hashToPrivateKey: (hash: Hex) => Bytes;
randomBytes: (len?: number) => Bytes;
};
/** Curve-specific utilities for private keys. */
declare const utils: {
normPrivateKeyToScalar: (p: PrivKey) => bigint;
isValidPrivateKey: (key: Hex) => boolean;
randomPrivateKey: () => Bytes;
precompute: (w?: number, p?: Point) => Point;
};
export { getPublicKey, sign, signAsync, verify, CURVE, // Remove the export to easily use in REPL
getSharedSecret, etc, utils, Point as ProjectivePoint, Signature };