@solana/web3.js 1.11.1 → 1.12.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.d.ts CHANGED
@@ -1,18 +1,37 @@
1
1
  /// <reference types="node" />
2
2
  declare module '@solana/web3.js' {
3
+ export class Struct {
4
+ constructor(properties: any);
5
+ encode(): Buffer;
6
+ static decode(data: Buffer): any;
7
+ }
8
+ export class Enum extends Struct {
9
+ enum: string;
10
+ constructor(properties: any);
11
+ }
12
+ export const SOLANA_SCHEMA: Map<Function, any>;
13
+
3
14
  /**
4
15
  * Maximum length of derived pubkey seed
5
16
  */
6
17
  export const MAX_SEED_LENGTH = 32;
18
+ export type PublicKeyInitData =
19
+ | number
20
+ | string
21
+ | Buffer
22
+ | Uint8Array
23
+ | Array<number>
24
+ | PublicKeyData;
25
+ export type PublicKeyData = {};
7
26
  /**
8
27
  * A public key
9
28
  */
10
- export class PublicKey {
29
+ export class PublicKey extends Struct {
11
30
  /**
12
31
  * Create a new PublicKey object
13
32
  * @param value ed25519 public key as buffer or base-58 encoded string
14
33
  */
15
- constructor(value: number | string | Buffer | Uint8Array | Array<number>);
34
+ constructor(value: PublicKeyInitData);
16
35
  /**
17
36
  * Default public key value. (All zeros)
18
37
  */
package/lib/index.esm.js CHANGED
@@ -5,6 +5,7 @@ import _defineProperty from '@babel/runtime/helpers/defineProperty';
5
5
  import BN from 'bn.js';
6
6
  import bs58 from 'bs58';
7
7
  import { sha256 } from 'crypto-hash';
8
+ import { serialize, deserialize } from 'borsh';
8
9
  import * as BufferLayout from 'buffer-layout';
9
10
  import invariant from 'assert';
10
11
  import { parse, format } from 'url';
@@ -27,16 +28,55 @@ const toBuffer = arr => {
27
28
  }
28
29
  };
29
30
 
31
+ class Struct {
32
+ constructor(properties) {
33
+ Object.assign(this, properties);
34
+ }
35
+
36
+ encode() {
37
+ return Buffer.from(serialize(SOLANA_SCHEMA, this));
38
+ }
39
+
40
+ static decode(data) {
41
+ return deserialize(SOLANA_SCHEMA, this, data);
42
+ }
43
+
44
+ } // Class representing a Rust-compatible enum, since enums are only strings or
45
+ // numbers in pure JS
46
+
47
+ class Enum extends Struct {
48
+ constructor(properties) {
49
+ super(properties);
50
+
51
+ _defineProperty(this, "enum", '');
52
+
53
+ if (Object.keys(properties).length !== 1) {
54
+ throw new Error('Enum can only take single value');
55
+ }
56
+
57
+ Object.keys(properties).map(key => {
58
+ this.enum = key;
59
+ });
60
+ }
61
+
62
+ }
63
+ const SOLANA_SCHEMA = new Map();
64
+
30
65
  /**
31
66
  * Maximum length of derived pubkey seed
32
67
  */
33
68
 
34
69
  const MAX_SEED_LENGTH = 32;
70
+
71
+ function isPublicKeyData(value) {
72
+ return value._bn !== undefined;
73
+ }
35
74
  /**
36
75
  * A public key
37
76
  */
38
77
 
39
- class PublicKey {
78
+
79
+ class PublicKey extends Struct {
40
80
  /** @internal */
41
81
 
42
82
  /**
@@ -44,21 +84,27 @@ class PublicKey {
44
84
  * @param value ed25519 public key as buffer or base-58 encoded string
45
85
  */
46
86
  constructor(value) {
47
- if (typeof value === 'string') {
48
- // assume base 58 encoding by default
49
- const decoded = bs58.decode(value);
50
-
51
- if (decoded.length != 32) {
52
- throw new Error("Invalid public key input");
53
- }
87
+ super({});
54
88
 
55
- this._bn = new BN(decoded);
89
+ if (isPublicKeyData(value)) {
90
+ this._bn = value._bn;
56
91
  } else {
57
- this._bn = new BN(value);
58
- }
92
+ if (typeof value === 'string') {
93
+ // assume base 58 encoding by default
94
+ const decoded = bs58.decode(value);
95
+
96
+ if (decoded.length != 32) {
97
+ throw new Error("Invalid public key input");
98
+ }
99
+
100
+ this._bn = new BN(decoded);
101
+ } else {
102
+ this._bn = new BN(value);
103
+ }
59
104
 
60
- if (this._bn.byteLength() > 32) {
61
- throw new Error("Invalid public key input");
105
+ if (this._bn.byteLength() > 32) {
106
+ throw new Error("Invalid public key input");
107
+ }
62
108
  }
63
109
  }
64
110
  /**
@@ -186,10 +232,15 @@ class PublicKey {
186
232
  return is_on_curve(pubkey) == 1;
187
233
  }
188
234
 
189
- } // @ts-ignore
235
+ }
190
236
 
191
237
  _defineProperty(PublicKey, "default", new PublicKey('11111111111111111111111111111111'));
192
238
 
239
+ SOLANA_SCHEMA.set(PublicKey, {
240
+ kind: 'struct',
241
+ fields: [['_bn', 'u256']]
242
+ }); // @ts-ignore
243
+
193
244
  let naclLowLevel = nacl__default.lowlevel; // Check that a pubkey is on the curve.
194
245
  // This function and its dependents were sourced from:
195
246
  // https://github.com/dchest/tweetnacl-js/blob/f1ec050ceae0861f34280e62498b1d3ed9c350c6/nacl.js#L792
@@ -6297,5 +6348,5 @@ function clusterApiUrl(cluster, tls) {
6297
6348
 
6298
6349
  const LAMPORTS_PER_SOL = 1000000000;
6299
6350
 
6300
- export { Account, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, Connection, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, Loader, Lockup, MAX_SEED_LENGTH, Message, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PublicKey, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, StakeAuthorizationLayout, StakeInstruction, StakeProgram, SystemInstruction, SystemProgram, Transaction, TransactionInstruction, VALIDATOR_INFO_KEY, VOTE_PROGRAM_ID, ValidatorInfo, VoteAccount, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
6351
+ export { Account, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, Connection, Enum, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, Loader, Lockup, MAX_SEED_LENGTH, Message, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PublicKey, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionInstruction, VALIDATOR_INFO_KEY, VOTE_PROGRAM_ID, ValidatorInfo, VoteAccount, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
6301
6352
  //# sourceMappingURL=index.esm.js.map