@solana/web3.js 1.56.3 → 1.57.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.56.3",
3
+ "version": "1.57.1",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -0,0 +1,79 @@
1
+ import {LoadedAddresses} from '../connection';
2
+ import {PublicKey} from '../publickey';
3
+ import {TransactionInstruction} from '../transaction';
4
+ import {MessageCompiledInstruction} from './index';
5
+
6
+ export type AccountKeysFromLookups = LoadedAddresses;
7
+
8
+ export class MessageAccountKeys {
9
+ staticAccountKeys: Array<PublicKey>;
10
+ accountKeysFromLookups?: AccountKeysFromLookups;
11
+
12
+ constructor(
13
+ staticAccountKeys: Array<PublicKey>,
14
+ accountKeysFromLookups?: AccountKeysFromLookups,
15
+ ) {
16
+ this.staticAccountKeys = staticAccountKeys;
17
+ this.accountKeysFromLookups = accountKeysFromLookups;
18
+ }
19
+
20
+ keySegments(): Array<Array<PublicKey>> {
21
+ const keySegments = [this.staticAccountKeys];
22
+ if (this.accountKeysFromLookups) {
23
+ keySegments.push(this.accountKeysFromLookups.writable);
24
+ keySegments.push(this.accountKeysFromLookups.readonly);
25
+ }
26
+ return keySegments;
27
+ }
28
+
29
+ get(index: number): PublicKey | undefined {
30
+ for (const keySegment of this.keySegments()) {
31
+ if (index < keySegment.length) {
32
+ return keySegment[index];
33
+ } else {
34
+ index -= keySegment.length;
35
+ }
36
+ }
37
+ return;
38
+ }
39
+
40
+ get length(): number {
41
+ return this.keySegments().flat().length;
42
+ }
43
+
44
+ compileInstructions(
45
+ instructions: Array<TransactionInstruction>,
46
+ ): Array<MessageCompiledInstruction> {
47
+ // Bail early if any account indexes would overflow a u8
48
+ const U8_MAX = 255;
49
+ if (this.length > U8_MAX + 1) {
50
+ throw new Error('Account index overflow encountered during compilation');
51
+ }
52
+
53
+ const keyIndexMap = new Map();
54
+ this.keySegments()
55
+ .flat()
56
+ .forEach((key, index) => {
57
+ keyIndexMap.set(key.toBase58(), index);
58
+ });
59
+
60
+ const findKeyIndex = (key: PublicKey) => {
61
+ const keyIndex = keyIndexMap.get(key.toBase58());
62
+ if (keyIndex === undefined)
63
+ throw new Error(
64
+ 'Encountered an unknown instruction account key during compilation',
65
+ );
66
+ return keyIndex;
67
+ };
68
+
69
+ return instructions.map((instruction): MessageCompiledInstruction => {
70
+ return {
71
+ programIdIndex: findKeyIndex(instruction.programId),
72
+ accountKeyIndexes: instruction.keys.map(meta =>
73
+ findKeyIndex(meta.pubkey),
74
+ ),
75
+ data: instruction.data,
76
+ };
77
+ });
78
+ }
79
+ }
@@ -0,0 +1,165 @@
1
+ import {MessageHeader, MessageAddressTableLookup} from './index';
2
+ import {AccountKeysFromLookups} from './account-keys';
3
+ import {AddressLookupTableAccount} from '../programs';
4
+ import {TransactionInstruction} from '../transaction';
5
+ import assert from '../utils/assert';
6
+ import {PublicKey} from '../publickey';
7
+
8
+ export type CompiledKeyMeta = {
9
+ isSigner: boolean;
10
+ isWritable: boolean;
11
+ isInvoked: boolean;
12
+ };
13
+
14
+ type KeyMetaMap = Map<string, CompiledKeyMeta>;
15
+
16
+ export class CompiledKeys {
17
+ payer: PublicKey;
18
+ keyMetaMap: KeyMetaMap;
19
+
20
+ constructor(payer: PublicKey, keyMetaMap: KeyMetaMap) {
21
+ this.payer = payer;
22
+ this.keyMetaMap = keyMetaMap;
23
+ }
24
+
25
+ static compile(
26
+ instructions: Array<TransactionInstruction>,
27
+ payer: PublicKey,
28
+ ): CompiledKeys {
29
+ const keyMetaMap: KeyMetaMap = new Map();
30
+ const getOrInsertDefault = (pubkey: PublicKey): CompiledKeyMeta => {
31
+ const address = pubkey.toBase58();
32
+ let keyMeta = keyMetaMap.get(address);
33
+ if (keyMeta === undefined) {
34
+ keyMeta = {
35
+ isSigner: false,
36
+ isWritable: false,
37
+ isInvoked: false,
38
+ };
39
+ keyMetaMap.set(address, keyMeta);
40
+ }
41
+ return keyMeta;
42
+ };
43
+
44
+ const payerKeyMeta = getOrInsertDefault(payer);
45
+ payerKeyMeta.isSigner = true;
46
+ payerKeyMeta.isWritable = true;
47
+
48
+ for (const ix of instructions) {
49
+ getOrInsertDefault(ix.programId).isInvoked = true;
50
+ for (const accountMeta of ix.keys) {
51
+ const keyMeta = getOrInsertDefault(accountMeta.pubkey);
52
+ keyMeta.isSigner ||= accountMeta.isSigner;
53
+ keyMeta.isWritable ||= accountMeta.isWritable;
54
+ }
55
+ }
56
+
57
+ return new CompiledKeys(payer, keyMetaMap);
58
+ }
59
+
60
+ getMessageComponents(): [MessageHeader, Array<PublicKey>] {
61
+ const mapEntries = [...this.keyMetaMap.entries()];
62
+ assert(mapEntries.length <= 256, 'Max static account keys length exceeded');
63
+
64
+ const writableSigners = mapEntries.filter(
65
+ ([, meta]) => meta.isSigner && meta.isWritable,
66
+ );
67
+ const readonlySigners = mapEntries.filter(
68
+ ([, meta]) => meta.isSigner && !meta.isWritable,
69
+ );
70
+ const writableNonSigners = mapEntries.filter(
71
+ ([, meta]) => !meta.isSigner && meta.isWritable,
72
+ );
73
+ const readonlyNonSigners = mapEntries.filter(
74
+ ([, meta]) => !meta.isSigner && !meta.isWritable,
75
+ );
76
+
77
+ const header: MessageHeader = {
78
+ numRequiredSignatures: writableSigners.length + readonlySigners.length,
79
+ numReadonlySignedAccounts: readonlySigners.length,
80
+ numReadonlyUnsignedAccounts: readonlyNonSigners.length,
81
+ };
82
+
83
+ // sanity checks
84
+ {
85
+ assert(
86
+ writableSigners.length > 0,
87
+ 'Expected at least one writable signer key',
88
+ );
89
+ const [payerAddress] = writableSigners[0];
90
+ assert(
91
+ payerAddress === this.payer.toBase58(),
92
+ 'Expected first writable signer key to be the fee payer',
93
+ );
94
+ }
95
+
96
+ const staticAccountKeys = [
97
+ ...writableSigners.map(([address]) => new PublicKey(address)),
98
+ ...readonlySigners.map(([address]) => new PublicKey(address)),
99
+ ...writableNonSigners.map(([address]) => new PublicKey(address)),
100
+ ...readonlyNonSigners.map(([address]) => new PublicKey(address)),
101
+ ];
102
+
103
+ return [header, staticAccountKeys];
104
+ }
105
+
106
+ extractTableLookup(
107
+ lookupTable: AddressLookupTableAccount,
108
+ ): [MessageAddressTableLookup, AccountKeysFromLookups] | undefined {
109
+ const [writableIndexes, drainedWritableKeys] =
110
+ this.drainKeysFoundInLookupTable(
111
+ lookupTable.state.addresses,
112
+ keyMeta =>
113
+ !keyMeta.isSigner && !keyMeta.isInvoked && keyMeta.isWritable,
114
+ );
115
+ const [readonlyIndexes, drainedReadonlyKeys] =
116
+ this.drainKeysFoundInLookupTable(
117
+ lookupTable.state.addresses,
118
+ keyMeta =>
119
+ !keyMeta.isSigner && !keyMeta.isInvoked && !keyMeta.isWritable,
120
+ );
121
+
122
+ // Don't extract lookup if no keys were found
123
+ if (writableIndexes.length === 0 && readonlyIndexes.length === 0) {
124
+ return;
125
+ }
126
+
127
+ return [
128
+ {
129
+ accountKey: lookupTable.key,
130
+ writableIndexes,
131
+ readonlyIndexes,
132
+ },
133
+ {
134
+ writable: drainedWritableKeys,
135
+ readonly: drainedReadonlyKeys,
136
+ },
137
+ ];
138
+ }
139
+
140
+ /** @internal */
141
+ private drainKeysFoundInLookupTable(
142
+ lookupTableEntries: Array<PublicKey>,
143
+ keyMetaFilter: (keyMeta: CompiledKeyMeta) => boolean,
144
+ ): [Array<number>, Array<PublicKey>] {
145
+ const lookupTableIndexes = new Array();
146
+ const drainedKeys = new Array();
147
+
148
+ for (const [address, keyMeta] of this.keyMetaMap.entries()) {
149
+ if (keyMetaFilter(keyMeta)) {
150
+ const key = new PublicKey(address);
151
+ const lookupTableIndex = lookupTableEntries.findIndex(entry =>
152
+ entry.equals(key),
153
+ );
154
+ if (lookupTableIndex >= 0) {
155
+ assert(lookupTableIndex < 256, 'Max lookup table index exceeded');
156
+ lookupTableIndexes.push(lookupTableIndex);
157
+ drainedKeys.push(key);
158
+ this.keyMetaMap.delete(address);
159
+ }
160
+ }
161
+ }
162
+
163
+ return [lookupTableIndexes, drainedKeys];
164
+ }
165
+ }
@@ -1,5 +1,7 @@
1
1
  import {PublicKey} from '../publickey';
2
2
 
3
+ export * from './account-keys';
4
+ // note: compiled-keys is internal and doesn't need to be exported
3
5
  export * from './legacy';
4
6
  export * from './versioned';
5
7
  export * from './v0';
package/src/message/v0.ts CHANGED
@@ -12,6 +12,10 @@ import {PublicKey, PUBLIC_KEY_LENGTH} from '../publickey';
12
12
  import * as shortvec from '../utils/shortvec-encoding';
13
13
  import assert from '../utils/assert';
14
14
  import {PACKET_DATA_SIZE, VERSION_PREFIX_MASK} from '../transaction/constants';
15
+ import {TransactionInstruction} from '../transaction';
16
+ import {AddressLookupTableAccount} from '../programs';
17
+ import {CompiledKeys} from './compiled-keys';
18
+ import {AccountKeysFromLookups, MessageAccountKeys} from './account-keys';
15
19
  import {guardedShift, guardedSplice} from '../utils/guarded-array-utils';
16
20
 
17
21
  /**
@@ -30,6 +34,13 @@ export type MessageV0Args = {
30
34
  addressTableLookups: MessageAddressTableLookup[];
31
35
  };
32
36
 
37
+ export type CompileV0Args = {
38
+ payerKey: PublicKey;
39
+ instructions: Array<TransactionInstruction>;
40
+ recentBlockhash: Blockhash;
41
+ addressLookupTableAccounts?: Array<AddressLookupTableAccount>;
42
+ };
43
+
33
44
  export class MessageV0 {
34
45
  header: MessageHeader;
35
46
  staticAccountKeys: Array<PublicKey>;
@@ -49,6 +60,42 @@ export class MessageV0 {
49
60
  return 0;
50
61
  }
51
62
 
63
+ static compile(args: CompileV0Args): MessageV0 {
64
+ const compiledKeys = CompiledKeys.compile(args.instructions, args.payerKey);
65
+
66
+ const addressTableLookups = new Array<MessageAddressTableLookup>();
67
+ const accountKeysFromLookups: AccountKeysFromLookups = {
68
+ writable: new Array(),
69
+ readonly: new Array(),
70
+ };
71
+ const lookupTableAccounts = args.addressLookupTableAccounts || [];
72
+ for (const lookupTable of lookupTableAccounts) {
73
+ const extractResult = compiledKeys.extractTableLookup(lookupTable);
74
+ if (extractResult !== undefined) {
75
+ const [addressTableLookup, {writable, readonly}] = extractResult;
76
+ addressTableLookups.push(addressTableLookup);
77
+ accountKeysFromLookups.writable.push(...writable);
78
+ accountKeysFromLookups.readonly.push(...readonly);
79
+ }
80
+ }
81
+
82
+ const [header, staticAccountKeys] = compiledKeys.getMessageComponents();
83
+ const accountKeys = new MessageAccountKeys(
84
+ staticAccountKeys,
85
+ accountKeysFromLookups,
86
+ );
87
+ const compiledInstructions = accountKeys.compileInstructions(
88
+ args.instructions,
89
+ );
90
+ return new MessageV0({
91
+ header,
92
+ staticAccountKeys,
93
+ recentBlockhash: args.recentBlockhash,
94
+ compiledInstructions,
95
+ addressTableLookups,
96
+ });
97
+ }
98
+
52
99
  serialize(): Uint8Array {
53
100
  const encodedStaticAccountKeysLength = Array<number>();
54
101
  shortvec.encodeLength(
package/src/publickey.ts CHANGED
@@ -40,6 +40,9 @@ function isPublicKeyData(value: PublicKeyInitData): value is PublicKeyData {
40
40
  return (value as PublicKeyData)._bn !== undefined;
41
41
  }
42
42
 
43
+ // local counter used by PublicKey.unique()
44
+ let uniquePublicKeyCounter = 1;
45
+
43
46
  /**
44
47
  * A public key
45
48
  */
@@ -73,6 +76,15 @@ export class PublicKey extends Struct {
73
76
  }
74
77
  }
75
78
 
79
+ /**
80
+ * Returns a unique PublicKey for tests and benchmarks using acounter
81
+ */
82
+ static unique(): PublicKey {
83
+ const key = new PublicKey(uniquePublicKeyCounter);
84
+ uniquePublicKeyCounter += 1;
85
+ return new PublicKey(key.toBuffer());
86
+ }
87
+
76
88
  /**
77
89
  * Default public key value. (All zeros)
78
90
  */