@solana/web3.js 1.54.0 → 1.54.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.browser.cjs.js +117 -1798
- package/lib/index.browser.cjs.js.map +1 -1
- package/lib/index.browser.esm.js +117 -1798
- package/lib/index.browser.esm.js.map +1 -1
- package/lib/index.cjs.js +117 -1819
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +15 -6
- package/lib/index.esm.js +117 -1819
- package/lib/index.esm.js.map +1 -1
- package/lib/index.iife.js +19245 -26059
- package/lib/index.iife.js.map +1 -1
- package/lib/index.iife.min.js +8 -5
- package/lib/index.iife.min.js.map +1 -1
- package/lib/index.native.js +117 -1798
- package/lib/index.native.js.map +1 -1
- package/package.json +5 -6
- package/src/account.ts +18 -9
- package/src/keypair.ts +19 -24
- package/src/programs/ed25519.ts +2 -2
- package/src/programs/secp256k1.ts +6 -5
- package/src/publickey.ts +7 -71
- package/src/transaction/legacy.ts +3 -5
- package/src/transaction/versioned.ts +2 -5
- package/src/utils/ed25519.ts +46 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/secp256k1.ts +18 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/web3.js",
|
|
3
|
-
"version": "1.54.
|
|
3
|
+
"version": "1.54.1",
|
|
4
4
|
"description": "Solana Javascript API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -58,7 +58,9 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@babel/runtime": "^7.12.5",
|
|
61
|
-
"@
|
|
61
|
+
"@noble/ed25519": "^1.7.0",
|
|
62
|
+
"@noble/hashes": "^1.1.2",
|
|
63
|
+
"@noble/secp256k1": "^1.6.3",
|
|
62
64
|
"@solana/buffer-layout": "^4.0.0",
|
|
63
65
|
"bigint-buffer": "^1.1.5",
|
|
64
66
|
"bn.js": "^5.0.0",
|
|
@@ -70,9 +72,7 @@
|
|
|
70
72
|
"js-sha3": "^0.8.0",
|
|
71
73
|
"node-fetch": "2",
|
|
72
74
|
"rpc-websockets": "^7.5.0",
|
|
73
|
-
"
|
|
74
|
-
"superstruct": "^0.14.2",
|
|
75
|
-
"tweetnacl": "^1.0.3"
|
|
75
|
+
"superstruct": "^0.14.2"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"@babel/core": "^7.12.13",
|
|
@@ -100,7 +100,6 @@
|
|
|
100
100
|
"@types/mz": "^2.7.3",
|
|
101
101
|
"@types/node": "^17.0.24",
|
|
102
102
|
"@types/node-fetch": "2",
|
|
103
|
-
"@types/secp256k1": "^4.0.1",
|
|
104
103
|
"@types/sinon": "^10.0.0",
|
|
105
104
|
"@types/sinon-chai": "^3.2.8",
|
|
106
105
|
"@typescript-eslint/eslint-plugin": "^4.14.2",
|
package/src/account.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {SignKeyPair as KeyPair} from 'tweetnacl';
|
|
3
|
-
import type {Buffer} from 'buffer';
|
|
1
|
+
import {Buffer} from 'buffer';
|
|
4
2
|
|
|
3
|
+
import {generatePrivateKey, getPublicKey} from './utils/ed25519';
|
|
5
4
|
import {toBuffer} from './utils/to-buffer';
|
|
6
5
|
import {PublicKey} from './publickey';
|
|
7
6
|
|
|
@@ -12,7 +11,9 @@ import {PublicKey} from './publickey';
|
|
|
12
11
|
*/
|
|
13
12
|
export class Account {
|
|
14
13
|
/** @internal */
|
|
15
|
-
|
|
14
|
+
private _publicKey: Buffer;
|
|
15
|
+
/** @internal */
|
|
16
|
+
private _secretKey: Buffer;
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Create a new Account object
|
|
@@ -24,9 +25,15 @@ export class Account {
|
|
|
24
25
|
*/
|
|
25
26
|
constructor(secretKey?: Buffer | Uint8Array | Array<number>) {
|
|
26
27
|
if (secretKey) {
|
|
27
|
-
|
|
28
|
+
const secretKeyBuffer = toBuffer(secretKey);
|
|
29
|
+
if (secretKey.length !== 64) {
|
|
30
|
+
throw new Error('bad secret key size');
|
|
31
|
+
}
|
|
32
|
+
this._publicKey = secretKeyBuffer.slice(32, 64);
|
|
33
|
+
this._secretKey = secretKeyBuffer.slice(0, 32);
|
|
28
34
|
} else {
|
|
29
|
-
this.
|
|
35
|
+
this._secretKey = toBuffer(generatePrivateKey());
|
|
36
|
+
this._publicKey = toBuffer(getPublicKey(this._secretKey));
|
|
30
37
|
}
|
|
31
38
|
}
|
|
32
39
|
|
|
@@ -34,13 +41,15 @@ export class Account {
|
|
|
34
41
|
* The public key for this account
|
|
35
42
|
*/
|
|
36
43
|
get publicKey(): PublicKey {
|
|
37
|
-
return new PublicKey(this.
|
|
44
|
+
return new PublicKey(this._publicKey);
|
|
38
45
|
}
|
|
39
46
|
|
|
40
47
|
/**
|
|
41
|
-
* The **unencrypted** secret key for this account
|
|
48
|
+
* The **unencrypted** secret key for this account. The first 32 bytes
|
|
49
|
+
* is the private scalar and the last 32 bytes is the public key.
|
|
50
|
+
* Read more: https://blog.mozilla.org/warner/2011/11/29/ed25519-keys/
|
|
42
51
|
*/
|
|
43
52
|
get secretKey(): Buffer {
|
|
44
|
-
return
|
|
53
|
+
return Buffer.concat([this._secretKey, this._publicKey], 64);
|
|
45
54
|
}
|
|
46
55
|
}
|
package/src/keypair.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import {generateKeypair, getPublicKey, Ed25519Keypair} from './utils/ed25519';
|
|
3
2
|
import {PublicKey} from './publickey';
|
|
4
3
|
|
|
5
4
|
/**
|
|
@@ -10,14 +9,6 @@ export interface Signer {
|
|
|
10
9
|
secretKey: Uint8Array;
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
/**
|
|
14
|
-
* Ed25519 Keypair
|
|
15
|
-
*/
|
|
16
|
-
export interface Ed25519Keypair {
|
|
17
|
-
publicKey: Uint8Array;
|
|
18
|
-
secretKey: Uint8Array;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
12
|
/**
|
|
22
13
|
* An account keypair used for signing transactions.
|
|
23
14
|
*/
|
|
@@ -31,18 +22,14 @@ export class Keypair {
|
|
|
31
22
|
* @param keypair ed25519 keypair
|
|
32
23
|
*/
|
|
33
24
|
constructor(keypair?: Ed25519Keypair) {
|
|
34
|
-
|
|
35
|
-
this._keypair = keypair;
|
|
36
|
-
} else {
|
|
37
|
-
this._keypair = nacl.sign.keyPair();
|
|
38
|
-
}
|
|
25
|
+
this._keypair = keypair ?? generateKeypair();
|
|
39
26
|
}
|
|
40
27
|
|
|
41
28
|
/**
|
|
42
29
|
* Generate a new random keypair
|
|
43
30
|
*/
|
|
44
31
|
static generate(): Keypair {
|
|
45
|
-
return new Keypair(
|
|
32
|
+
return new Keypair(generateKeypair());
|
|
46
33
|
}
|
|
47
34
|
|
|
48
35
|
/**
|
|
@@ -61,16 +48,20 @@ export class Keypair {
|
|
|
61
48
|
secretKey: Uint8Array,
|
|
62
49
|
options?: {skipValidation?: boolean},
|
|
63
50
|
): Keypair {
|
|
64
|
-
|
|
51
|
+
if (secretKey.byteLength !== 64) {
|
|
52
|
+
throw new Error('bad secret key size');
|
|
53
|
+
}
|
|
54
|
+
const publicKey = secretKey.slice(32, 64);
|
|
65
55
|
if (!options || !options.skipValidation) {
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
56
|
+
const privateScalar = secretKey.slice(0, 32);
|
|
57
|
+
const computedPublicKey = getPublicKey(privateScalar);
|
|
58
|
+
for (let ii = 0; ii < 32; ii++) {
|
|
59
|
+
if (publicKey[ii] !== computedPublicKey[ii]) {
|
|
60
|
+
throw new Error('provided secretKey is invalid');
|
|
61
|
+
}
|
|
71
62
|
}
|
|
72
63
|
}
|
|
73
|
-
return new Keypair(
|
|
64
|
+
return new Keypair({publicKey, secretKey});
|
|
74
65
|
}
|
|
75
66
|
|
|
76
67
|
/**
|
|
@@ -79,7 +70,11 @@ export class Keypair {
|
|
|
79
70
|
* @param seed seed byte array
|
|
80
71
|
*/
|
|
81
72
|
static fromSeed(seed: Uint8Array): Keypair {
|
|
82
|
-
|
|
73
|
+
const publicKey = getPublicKey(seed);
|
|
74
|
+
const secretKey = new Uint8Array(64);
|
|
75
|
+
secretKey.set(seed);
|
|
76
|
+
secretKey.set(publicKey, 32);
|
|
77
|
+
return new Keypair({publicKey, secretKey});
|
|
83
78
|
}
|
|
84
79
|
|
|
85
80
|
/**
|
package/src/programs/ed25519.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {Buffer} from 'buffer';
|
|
2
2
|
import * as BufferLayout from '@solana/buffer-layout';
|
|
3
|
-
import nacl from 'tweetnacl';
|
|
4
3
|
|
|
5
4
|
import {Keypair} from '../keypair';
|
|
6
5
|
import {PublicKey} from '../publickey';
|
|
7
6
|
import {TransactionInstruction} from '../transaction';
|
|
8
7
|
import assert from '../utils/assert';
|
|
8
|
+
import {sign} from '../utils/ed25519';
|
|
9
9
|
|
|
10
10
|
const PRIVATE_KEY_BYTES = 64;
|
|
11
11
|
const PUBLIC_KEY_BYTES = 32;
|
|
@@ -142,7 +142,7 @@ export class Ed25519Program {
|
|
|
142
142
|
try {
|
|
143
143
|
const keypair = Keypair.fromSecretKey(privateKey);
|
|
144
144
|
const publicKey = keypair.publicKey.toBytes();
|
|
145
|
-
const signature =
|
|
145
|
+
const signature = sign(message, keypair.secretKey);
|
|
146
146
|
|
|
147
147
|
return this.createInstructionWithPublicKey({
|
|
148
148
|
publicKey,
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import {Buffer} from 'buffer';
|
|
2
2
|
import * as BufferLayout from '@solana/buffer-layout';
|
|
3
|
-
import secp256k1 from 'secp256k1';
|
|
4
3
|
import sha3 from 'js-sha3';
|
|
5
4
|
|
|
6
5
|
import {PublicKey} from '../publickey';
|
|
7
6
|
import {TransactionInstruction} from '../transaction';
|
|
8
7
|
import assert from '../utils/assert';
|
|
8
|
+
import {publicKeyCreate, ecdsaSign} from '../utils/secp256k1';
|
|
9
9
|
import {toBuffer} from '../utils/to-buffer';
|
|
10
10
|
|
|
11
|
-
const {publicKeyCreate, ecdsaSign} = secp256k1;
|
|
12
|
-
|
|
13
11
|
const PRIVATE_KEY_BYTES = 32;
|
|
14
12
|
const ETHEREUM_ADDRESS_BYTES = 20;
|
|
15
13
|
const PUBLIC_KEY_BYTES = 64;
|
|
@@ -209,11 +207,14 @@ export class Secp256k1Program {
|
|
|
209
207
|
|
|
210
208
|
try {
|
|
211
209
|
const privateKey = toBuffer(pkey);
|
|
212
|
-
const publicKey = publicKeyCreate(
|
|
210
|
+
const publicKey = publicKeyCreate(
|
|
211
|
+
privateKey,
|
|
212
|
+
false /* isCompressed */,
|
|
213
|
+
).slice(1); // throw away leading byte
|
|
213
214
|
const messageHash = Buffer.from(
|
|
214
215
|
sha3.keccak_256.update(toBuffer(message)).digest(),
|
|
215
216
|
);
|
|
216
|
-
const
|
|
217
|
+
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
|
|
217
218
|
|
|
218
219
|
return this.createInstructionWithPublicKey({
|
|
219
220
|
publicKey,
|
package/src/publickey.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import BN from 'bn.js';
|
|
2
2
|
import bs58 from 'bs58';
|
|
3
3
|
import {Buffer} from 'buffer';
|
|
4
|
-
import
|
|
5
|
-
import {sha256} from '@ethersproject/sha2';
|
|
4
|
+
import {sha256} from '@noble/hashes/sha256';
|
|
6
5
|
|
|
6
|
+
import {isOnCurve} from './utils/ed25519';
|
|
7
7
|
import {Struct, SOLANA_SCHEMA} from './utils/borsh-schema';
|
|
8
8
|
import {toBuffer} from './utils/to-buffer';
|
|
9
9
|
|
|
@@ -140,8 +140,8 @@ export class PublicKey extends Struct {
|
|
|
140
140
|
Buffer.from(seed),
|
|
141
141
|
programId.toBuffer(),
|
|
142
142
|
]);
|
|
143
|
-
const
|
|
144
|
-
return new PublicKey(
|
|
143
|
+
const publicKeyBytes = sha256(buffer);
|
|
144
|
+
return new PublicKey(publicKeyBytes);
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
/**
|
|
@@ -164,9 +164,8 @@ export class PublicKey extends Struct {
|
|
|
164
164
|
programId.toBuffer(),
|
|
165
165
|
Buffer.from('ProgramDerivedAddress'),
|
|
166
166
|
]);
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (is_on_curve(publicKeyBytes)) {
|
|
167
|
+
const publicKeyBytes = sha256(buffer);
|
|
168
|
+
if (isOnCurve(publicKeyBytes)) {
|
|
170
169
|
throw new Error(`Invalid seeds, address must fall off the curve`);
|
|
171
170
|
}
|
|
172
171
|
return new PublicKey(publicKeyBytes);
|
|
@@ -229,7 +228,7 @@ export class PublicKey extends Struct {
|
|
|
229
228
|
*/
|
|
230
229
|
static isOnCurve(pubkeyData: PublicKeyInitData): boolean {
|
|
231
230
|
const pubkey = new PublicKey(pubkeyData);
|
|
232
|
-
return
|
|
231
|
+
return isOnCurve(pubkey.toBytes());
|
|
233
232
|
}
|
|
234
233
|
}
|
|
235
234
|
|
|
@@ -237,66 +236,3 @@ SOLANA_SCHEMA.set(PublicKey, {
|
|
|
237
236
|
kind: 'struct',
|
|
238
237
|
fields: [['_bn', 'u256']],
|
|
239
238
|
});
|
|
240
|
-
|
|
241
|
-
// @ts-ignore
|
|
242
|
-
let naclLowLevel = nacl.lowlevel;
|
|
243
|
-
|
|
244
|
-
// Check that a pubkey is on the curve.
|
|
245
|
-
// This function and its dependents were sourced from:
|
|
246
|
-
// https://github.com/dchest/tweetnacl-js/blob/f1ec050ceae0861f34280e62498b1d3ed9c350c6/nacl.js#L792
|
|
247
|
-
function is_on_curve(p: any) {
|
|
248
|
-
var r = [
|
|
249
|
-
naclLowLevel.gf(),
|
|
250
|
-
naclLowLevel.gf(),
|
|
251
|
-
naclLowLevel.gf(),
|
|
252
|
-
naclLowLevel.gf(),
|
|
253
|
-
];
|
|
254
|
-
|
|
255
|
-
var t = naclLowLevel.gf(),
|
|
256
|
-
chk = naclLowLevel.gf(),
|
|
257
|
-
num = naclLowLevel.gf(),
|
|
258
|
-
den = naclLowLevel.gf(),
|
|
259
|
-
den2 = naclLowLevel.gf(),
|
|
260
|
-
den4 = naclLowLevel.gf(),
|
|
261
|
-
den6 = naclLowLevel.gf();
|
|
262
|
-
|
|
263
|
-
naclLowLevel.set25519(r[2], gf1);
|
|
264
|
-
naclLowLevel.unpack25519(r[1], p);
|
|
265
|
-
naclLowLevel.S(num, r[1]);
|
|
266
|
-
naclLowLevel.M(den, num, naclLowLevel.D);
|
|
267
|
-
naclLowLevel.Z(num, num, r[2]);
|
|
268
|
-
naclLowLevel.A(den, r[2], den);
|
|
269
|
-
|
|
270
|
-
naclLowLevel.S(den2, den);
|
|
271
|
-
naclLowLevel.S(den4, den2);
|
|
272
|
-
naclLowLevel.M(den6, den4, den2);
|
|
273
|
-
naclLowLevel.M(t, den6, num);
|
|
274
|
-
naclLowLevel.M(t, t, den);
|
|
275
|
-
|
|
276
|
-
naclLowLevel.pow2523(t, t);
|
|
277
|
-
naclLowLevel.M(t, t, num);
|
|
278
|
-
naclLowLevel.M(t, t, den);
|
|
279
|
-
naclLowLevel.M(t, t, den);
|
|
280
|
-
naclLowLevel.M(r[0], t, den);
|
|
281
|
-
|
|
282
|
-
naclLowLevel.S(chk, r[0]);
|
|
283
|
-
naclLowLevel.M(chk, chk, den);
|
|
284
|
-
if (neq25519(chk, num)) naclLowLevel.M(r[0], r[0], I);
|
|
285
|
-
|
|
286
|
-
naclLowLevel.S(chk, r[0]);
|
|
287
|
-
naclLowLevel.M(chk, chk, den);
|
|
288
|
-
if (neq25519(chk, num)) return 0;
|
|
289
|
-
return 1;
|
|
290
|
-
}
|
|
291
|
-
let gf1 = naclLowLevel.gf([1]);
|
|
292
|
-
let I = naclLowLevel.gf([
|
|
293
|
-
0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7,
|
|
294
|
-
0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83,
|
|
295
|
-
]);
|
|
296
|
-
function neq25519(a: any, b: any) {
|
|
297
|
-
var c = new Uint8Array(32),
|
|
298
|
-
d = new Uint8Array(32);
|
|
299
|
-
naclLowLevel.pack25519(c, a);
|
|
300
|
-
naclLowLevel.pack25519(d, b);
|
|
301
|
-
return naclLowLevel.crypto_verify_32(c, 0, d, 0);
|
|
302
|
-
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import nacl from 'tweetnacl';
|
|
2
1
|
import bs58 from 'bs58';
|
|
3
2
|
import {Buffer} from 'buffer';
|
|
4
3
|
|
|
@@ -12,6 +11,7 @@ import invariant from '../utils/assert';
|
|
|
12
11
|
import type {Signer} from '../keypair';
|
|
13
12
|
import type {Blockhash} from '../blockhash';
|
|
14
13
|
import type {CompiledInstruction} from '../message';
|
|
14
|
+
import {sign, verify} from '../utils/ed25519';
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Transaction signature as base-58 encoded string
|
|
@@ -658,7 +658,7 @@ export class Transaction {
|
|
|
658
658
|
_partialSign(message: Message, ...signers: Array<Signer>) {
|
|
659
659
|
const signData = message.serialize();
|
|
660
660
|
signers.forEach(signer => {
|
|
661
|
-
const signature =
|
|
661
|
+
const signature = sign(signData, signer.secretKey);
|
|
662
662
|
this._addSignature(signer.publicKey, toBuffer(signature));
|
|
663
663
|
});
|
|
664
664
|
}
|
|
@@ -706,9 +706,7 @@ export class Transaction {
|
|
|
706
706
|
return false;
|
|
707
707
|
}
|
|
708
708
|
} else {
|
|
709
|
-
if (
|
|
710
|
-
!nacl.sign.detached.verify(signData, signature, publicKey.toBuffer())
|
|
711
|
-
) {
|
|
709
|
+
if (!verify(signature, signData, publicKey.toBuffer())) {
|
|
712
710
|
return false;
|
|
713
711
|
}
|
|
714
712
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import nacl from 'tweetnacl';
|
|
2
1
|
import * as BufferLayout from '@solana/buffer-layout';
|
|
3
2
|
|
|
4
3
|
import {Signer} from '../keypair';
|
|
@@ -7,6 +6,7 @@ import {VersionedMessage} from '../message/versioned';
|
|
|
7
6
|
import {SIGNATURE_LENGTH_IN_BYTES} from './constants';
|
|
8
7
|
import * as shortvec from '../utils/shortvec-encoding';
|
|
9
8
|
import * as Layout from '../layout';
|
|
9
|
+
import {sign} from '../utils/ed25519';
|
|
10
10
|
|
|
11
11
|
export type TransactionVersion = 'legacy' | 0;
|
|
12
12
|
|
|
@@ -99,10 +99,7 @@ export class VersionedTransaction {
|
|
|
99
99
|
signerIndex >= 0,
|
|
100
100
|
`Cannot sign with non signer key ${signer.publicKey.toBase58()}`,
|
|
101
101
|
);
|
|
102
|
-
this.signatures[signerIndex] =
|
|
103
|
-
messageData,
|
|
104
|
-
signer.secretKey,
|
|
105
|
-
);
|
|
102
|
+
this.signatures[signerIndex] = sign(messageData, signer.secretKey);
|
|
106
103
|
}
|
|
107
104
|
}
|
|
108
105
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {sha512} from '@noble/hashes/sha512';
|
|
2
|
+
import * as ed25519 from '@noble/ed25519';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A 64 byte secret key, the first 32 bytes of which is the
|
|
6
|
+
* private scalar and the last 32 bytes is the public key.
|
|
7
|
+
* Read more: https://blog.mozilla.org/warner/2011/11/29/ed25519-keys/
|
|
8
|
+
*/
|
|
9
|
+
type Ed25519SecretKey = Uint8Array;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Ed25519 Keypair
|
|
13
|
+
*/
|
|
14
|
+
export interface Ed25519Keypair {
|
|
15
|
+
publicKey: Uint8Array;
|
|
16
|
+
secretKey: Ed25519SecretKey;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
ed25519.utils.sha512Sync = (...m) => sha512(ed25519.utils.concatBytes(...m));
|
|
20
|
+
|
|
21
|
+
export const generatePrivateKey = ed25519.utils.randomPrivateKey;
|
|
22
|
+
export const generateKeypair = (): Ed25519Keypair => {
|
|
23
|
+
const privateScalar = ed25519.utils.randomPrivateKey();
|
|
24
|
+
const publicKey = getPublicKey(privateScalar);
|
|
25
|
+
const secretKey = new Uint8Array(64);
|
|
26
|
+
secretKey.set(privateScalar);
|
|
27
|
+
secretKey.set(publicKey, 32);
|
|
28
|
+
return {
|
|
29
|
+
publicKey,
|
|
30
|
+
secretKey,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export const getPublicKey = ed25519.sync.getPublicKey;
|
|
34
|
+
export function isOnCurve(publicKey: Uint8Array): boolean {
|
|
35
|
+
try {
|
|
36
|
+
ed25519.Point.fromHex(publicKey, true /* strict */);
|
|
37
|
+
return true;
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export const sign = (
|
|
43
|
+
message: Parameters<typeof ed25519.sync.sign>[0],
|
|
44
|
+
secretKey: Ed25519SecretKey,
|
|
45
|
+
) => ed25519.sync.sign(message, secretKey.slice(0, 32));
|
|
46
|
+
export const verify = ed25519.sync.verify;
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {hmac} from '@noble/hashes/hmac';
|
|
2
|
+
import {sha256} from '@noble/hashes/sha256';
|
|
3
|
+
import * as secp256k1 from '@noble/secp256k1';
|
|
4
|
+
|
|
5
|
+
// Supply a synchronous hashing algorithm to make this
|
|
6
|
+
// library interoperable with the synchronous APIs in web3.js.
|
|
7
|
+
secp256k1.utils.hmacSha256Sync = (key: Uint8Array, ...msgs: Uint8Array[]) => {
|
|
8
|
+
const h = hmac.create(sha256, key);
|
|
9
|
+
msgs.forEach(msg => h.update(msg));
|
|
10
|
+
return h.digest();
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const ecdsaSign = (
|
|
14
|
+
msgHash: Parameters<typeof secp256k1.signSync>[0],
|
|
15
|
+
privKey: Parameters<typeof secp256k1.signSync>[1],
|
|
16
|
+
) => secp256k1.signSync(msgHash, privKey, {der: false, recovered: true});
|
|
17
|
+
export const isValidPrivateKey = secp256k1.utils.isValidPrivateKey;
|
|
18
|
+
export const publicKeyCreate = secp256k1.getPublicKey;
|