This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 931
/
Copy pathkeypair.ts
102 lines (94 loc) · 2.63 KB
/
keypair.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
import {generateKeypair, getPublicKey, Ed25519Keypair} from './utils/ed25519';
import {PublicKey} from './publickey';
/**
* Keypair signer interface
*/
export interface Signer {
publicKey: PublicKey;
secretKey: Uint8Array;
}
/**
* An account keypair used for signing transactions.
*/
export class Keypair {
private _keypair: Ed25519Keypair;
/**
* Create a new keypair instance.
* Generate random keypair if no {@link Ed25519Keypair} is provided.
*
* @param {Ed25519Keypair} keypair ed25519 keypair
*/
constructor(keypair?: Ed25519Keypair) {
this._keypair = keypair ?? generateKeypair();
}
/**
* Generate a new random keypair
*
* @returns {Keypair} Keypair
*/
static generate(): Keypair {
return new Keypair(generateKeypair());
}
/**
* Create a keypair from a raw secret key byte array.
*
* This method should only be used to recreate a keypair from a previously
* generated secret key. Generating keypairs from a random seed should be done
* with the {@link Keypair.fromSeed} method.
*
* @throws error if the provided secret key is invalid and validation is not skipped.
*
* @param secretKey secret key byte array
* @param options skip secret key validation
*
* @returns {Keypair} Keypair
*/
static fromSecretKey(
secretKey: Uint8Array,
options?: {skipValidation?: boolean},
): Keypair {
if (secretKey.byteLength !== 64) {
throw new Error('bad secret key size');
}
const publicKey = secretKey.slice(32, 64);
if (!options || !options.skipValidation) {
const privateScalar = secretKey.slice(0, 32);
const computedPublicKey = getPublicKey(privateScalar);
for (let ii = 0; ii < 32; ii++) {
if (publicKey[ii] !== computedPublicKey[ii]) {
throw new Error('provided secretKey is invalid');
}
}
}
return new Keypair({publicKey, secretKey});
}
/**
* Generate a keypair from a 32 byte seed.
*
* @param seed seed byte array
*
* @returns {Keypair} Keypair
*/
static fromSeed(seed: Uint8Array): Keypair {
const publicKey = getPublicKey(seed);
const secretKey = new Uint8Array(64);
secretKey.set(seed);
secretKey.set(publicKey, 32);
return new Keypair({publicKey, secretKey});
}
/**
* The public key for this keypair
*
* @returns {PublicKey} PublicKey
*/
get publicKey(): PublicKey {
return new PublicKey(this._keypair.publicKey);
}
/**
* The raw secret key for this keypair
* @returns {Uint8Array} Secret key in an array of Uint8 bytes
*/
get secretKey(): Uint8Array {
return new Uint8Array(this._keypair.secretKey);
}
}