@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/module.flow.js CHANGED
@@ -5,22 +5,39 @@
5
5
  */
6
6
 
7
7
  declare module "@solana/web3.js" {
8
+ declare export class Struct {
9
+ constructor(properties: any): this;
10
+ encode(): Buffer;
11
+ static decode(data: Buffer): any;
12
+ }
13
+ declare export class Enum mixins Struct {
14
+ enum: string;
15
+ constructor(properties: any): this;
16
+ }
17
+ declare export var SOLANA_SCHEMA: Map<Function, any>;
18
+
8
19
  /**
9
20
  * Maximum length of derived pubkey seed
10
21
  */
11
22
  declare export var MAX_SEED_LENGTH: 32;
23
+ declare export type PublicKeyInitData =
24
+ | number
25
+ | string
26
+ | Buffer
27
+ | Uint8Array
28
+ | Array<number>
29
+ | PublicKeyData;
30
+ declare export type PublicKeyData = { ... };
12
31
 
13
32
  /**
14
33
  * A public key
15
34
  */
16
- declare export class PublicKey {
35
+ declare export class PublicKey mixins Struct {
17
36
  /**
18
37
  * Create a new PublicKey object
19
38
  * @param value ed25519 public key as buffer or base-58 encoded string
20
39
  */
21
- constructor(
22
- value: number | string | Buffer | Uint8Array | Array<number>
23
- ): this;
40
+ constructor(value: PublicKeyInitData): this;
24
41
 
25
42
  /**
26
43
  * Default public key value. (All zeros)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.11.1",
3
+ "version": "1.12.1",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -39,6 +39,7 @@
39
39
  "dependencies": {
40
40
  "@babel/runtime": "^7.12.5",
41
41
  "bn.js": "^5.0.0",
42
+ "borsh": "^0.4.0",
42
43
  "bs58": "^4.0.1",
43
44
  "buffer": "6.0.1",
44
45
  "buffer-layout": "^1.2.0",
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from './transaction';
16
16
  export * from './validator-info';
17
17
  export * from './vote-account';
18
18
  export * from './sysvar';
19
+ export * from './util/borsh-schema';
19
20
  export * from './util/send-and-confirm-transaction';
20
21
  export * from './util/send-and-confirm-raw-transaction';
21
22
  export * from './util/cluster';
package/src/publickey.ts CHANGED
@@ -4,6 +4,7 @@ import nacl from 'tweetnacl';
4
4
  import {sha256} from 'crypto-hash';
5
5
  import {Buffer} from 'buffer';
6
6
 
7
+ import {Struct, SOLANA_SCHEMA} from './util/borsh-schema';
7
8
  import {toBuffer} from './util/to-buffer';
8
9
 
9
10
  /**
@@ -11,10 +12,27 @@ import {toBuffer} from './util/to-buffer';
11
12
  */
12
13
  export const MAX_SEED_LENGTH = 32;
13
14
 
15
+ type PublicKeyInitData =
16
+ | number
17
+ | string
18
+ | Buffer
19
+ | Uint8Array
20
+ | Array<number>
21
+ | PublicKeyData;
22
+
23
+ type PublicKeyData = {
24
+ /** @internal */
25
+ _bn: BN;
26
+ };
27
+
28
+ function isPublicKeyData(value: PublicKeyInitData): value is PublicKeyData {
29
+ return (value as PublicKeyData)._bn !== undefined;
30
+ }
31
+
14
32
  /**
15
33
  * A public key
16
34
  */
17
- export class PublicKey {
35
+ export class PublicKey extends Struct {
18
36
  /** @internal */
19
37
  _bn: BN;
20
38
 
@@ -22,20 +40,25 @@ export class PublicKey {
22
40
  * Create a new PublicKey object
23
41
  * @param value ed25519 public key as buffer or base-58 encoded string
24
42
  */
25
- constructor(value: number | string | Buffer | Uint8Array | Array<number>) {
26
- if (typeof value === 'string') {
27
- // assume base 58 encoding by default
28
- const decoded = bs58.decode(value);
29
- if (decoded.length != 32) {
30
- throw new Error(`Invalid public key input`);
31
- }
32
- this._bn = new BN(decoded);
43
+ constructor(value: PublicKeyInitData) {
44
+ super({});
45
+ if (isPublicKeyData(value)) {
46
+ this._bn = value._bn;
33
47
  } else {
34
- this._bn = new BN(value);
35
- }
48
+ if (typeof value === 'string') {
49
+ // assume base 58 encoding by default
50
+ const decoded = bs58.decode(value);
51
+ if (decoded.length != 32) {
52
+ throw new Error(`Invalid public key input`);
53
+ }
54
+ this._bn = new BN(decoded);
55
+ } else {
56
+ this._bn = new BN(value);
57
+ }
36
58
 
37
- if (this._bn.byteLength() > 32) {
38
- throw new Error(`Invalid public key input`);
59
+ if (this._bn.byteLength() > 32) {
60
+ throw new Error(`Invalid public key input`);
61
+ }
39
62
  }
40
63
  }
41
64
 
@@ -167,6 +190,11 @@ export class PublicKey {
167
190
  }
168
191
  }
169
192
 
193
+ SOLANA_SCHEMA.set(PublicKey, {
194
+ kind: 'struct',
195
+ fields: [['_bn', 'u256']],
196
+ });
197
+
170
198
  // @ts-ignore
171
199
  let naclLowLevel = nacl.lowlevel;
172
200
 
@@ -0,0 +1,34 @@
1
+ import {Buffer} from 'buffer';
2
+ import {serialize, deserialize} from 'borsh';
3
+
4
+ // Class wrapping a plain object
5
+ export class Struct {
6
+ constructor(properties: any) {
7
+ Object.assign(this, properties);
8
+ }
9
+
10
+ encode(): Buffer {
11
+ return Buffer.from(serialize(SOLANA_SCHEMA, this));
12
+ }
13
+
14
+ static decode(data: Buffer): any {
15
+ return deserialize(SOLANA_SCHEMA, this, data);
16
+ }
17
+ }
18
+
19
+ // Class representing a Rust-compatible enum, since enums are only strings or
20
+ // numbers in pure JS
21
+ export class Enum extends Struct {
22
+ enum: string = '';
23
+ constructor(properties: any) {
24
+ super(properties);
25
+ if (Object.keys(properties).length !== 1) {
26
+ throw new Error('Enum can only take single value');
27
+ }
28
+ Object.keys(properties).map(key => {
29
+ this.enum = key;
30
+ });
31
+ }
32
+ }
33
+
34
+ export const SOLANA_SCHEMA: Map<Function, any> = new Map();