@solana/web3.js 1.54.0 → 1.56.0

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.
@@ -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(privateKey, false).slice(1); // throw away leading byte
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 {signature, recid: recoveryId} = ecdsaSign(messageHash, privateKey);
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 nacl from 'tweetnacl';
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 hash = sha256(new Uint8Array(buffer)).slice(2);
144
- return new PublicKey(Buffer.from(hash, 'hex'));
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
- let hash = sha256(new Uint8Array(buffer)).slice(2);
168
- let publicKeyBytes = new BN(hash, 16).toArray(undefined, 32);
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 is_on_curve(pubkey.toBytes()) == 1;
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 = nacl.sign.detached(signData, signer.secretKey);
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] = nacl.sign.detached(
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;
@@ -1,4 +1,5 @@
1
1
  export * from './borsh-schema';
2
2
  export * from './cluster';
3
+ export type {Ed25519Keypair} from './ed25519';
3
4
  export * from './send-and-confirm-raw-transaction';
4
5
  export * from './send-and-confirm-transaction';
@@ -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;