@exodus/solana-lib 1.6.6 → 1.6.8

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,20 +1,16 @@
1
- // @flow
2
1
  // https://github.com/solana-labs/solana-web3.js/blob/master/src/transaction.js
3
2
 
4
3
  import invariant from 'assert'
5
4
  import nacl from 'tweetnacl'
6
5
  import bs58 from 'bs58'
7
6
 
8
- import type { Blockhash } from './utils/blockhash'
9
- import type { CompiledInstruction } from './message'
10
7
  import { Message } from './message'
11
- import { PublicKey, Account } from './index'
8
+ import { PublicKey } from './index'
12
9
  import * as shortvec from './utils/shortvec-encoding'
13
10
 
14
11
  /**
15
12
  * @typedef {string} TransactionSignature
16
13
  */
17
- export type TransactionSignature = string
18
14
 
19
15
  /**
20
16
  * Default (empty) signature
@@ -42,11 +38,6 @@ const SIGNATURE_LENGTH = 64
42
38
  * @property {boolean} isSigner True if an instruction requires a transaction signature matching `pubkey`
43
39
  * @property {boolean} isWritable True if the `pubkey` can be loaded as a read-write account.
44
40
  */
45
- export type AccountMeta = {
46
- pubkey: PublicKey,
47
- isSigner: boolean,
48
- isWritable: boolean,
49
- }
50
41
 
51
42
  /**
52
43
  * List of TransactionInstruction object fields that may be initialized at construction
@@ -56,11 +47,6 @@ export type AccountMeta = {
56
47
  * @property {?PublicKey} programId
57
48
  * @property {?Buffer} data
58
49
  */
59
- export type TransactionInstructionCtorFields = {|
60
- keys?: Array<AccountMeta>,
61
- programId?: PublicKey,
62
- data?: Buffer,
63
- |}
64
50
 
65
51
  /**
66
52
  * Configuration object for Transaction.serialize()
@@ -69,10 +55,6 @@ export type TransactionInstructionCtorFields = {|
69
55
  * @property {boolean|undefined} requireAllSignatures Require all transaction signatures be present (default: true)
70
56
  * @property {boolean|undefined} verifySignatures Verify provided signatures (default: true)
71
57
  */
72
- export type SerializeConfig = {
73
- requireAllSignatures?: boolean,
74
- verifySignatures?: boolean,
75
- }
76
58
 
77
59
  /**
78
60
  * Transaction Instruction class
@@ -82,19 +64,19 @@ export class TransactionInstruction {
82
64
  * Public keys to include in this transaction
83
65
  * Boolean represents whether this pubkey needs to sign the transaction
84
66
  */
85
- keys: Array<AccountMeta> = []
67
+ keys = []
86
68
 
87
69
  /**
88
70
  * Program Id to execute
89
71
  */
90
- programId: PublicKey
72
+ programId
91
73
 
92
74
  /**
93
75
  * Program input
94
76
  */
95
- data: Buffer = Buffer.alloc(0)
77
+ data = Buffer.alloc(0)
96
78
 
97
- constructor(opts?: TransactionInstructionCtorFields) {
79
+ constructor(opts?) {
98
80
  opts && Object.assign(this, opts)
99
81
  }
100
82
  }
@@ -102,10 +84,6 @@ export class TransactionInstruction {
102
84
  /**
103
85
  * @private
104
86
  */
105
- type SignaturePubkeyPair = {|
106
- signature: Buffer | null,
107
- publicKey: PublicKey,
108
- |}
109
87
 
110
88
  /**
111
89
  * NonceInformation to be used to build a Transaction.
@@ -114,10 +92,6 @@ type SignaturePubkeyPair = {|
114
92
  * @property {Blockhash} nonce The current Nonce blockhash
115
93
  * @property {TransactionInstruction} nonceInstruction AdvanceNonceAccount Instruction
116
94
  */
117
- type NonceInformation = {|
118
- nonce: Blockhash,
119
- nonceInstruction: TransactionInstruction,
120
- |}
121
95
 
122
96
  /**
123
97
  * List of Transaction object fields that may be initialized at construction
@@ -127,11 +101,6 @@ type NonceInformation = {|
127
101
  * @property {?Array<SignaturePubkeyPair>} signatures One or more signatures
128
102
  *
129
103
  */
130
- type TransactionCtorFields = {|
131
- recentBlockhash?: Blockhash | null,
132
- nonceInfo?: NonceInformation | null,
133
- signatures?: Array<SignaturePubkeyPair>,
134
- |}
135
104
 
136
105
  /**
137
106
  * Transaction class
@@ -141,12 +110,12 @@ export class Transaction {
141
110
  * Signatures for the transaction. Typically created by invoking the
142
111
  * `sign()` method
143
112
  */
144
- signatures: Array<SignaturePubkeyPair> = []
113
+ signatures = []
145
114
 
146
115
  /**
147
116
  * The first (payer) Transaction signature
148
117
  */
149
- get signature(): Buffer | null {
118
+ get signature() {
150
119
  if (this.signatures.length > 0) {
151
120
  return this.signatures[0].signature
152
121
  }
@@ -156,12 +125,12 @@ export class Transaction {
156
125
  /**
157
126
  * The transaction fee payer
158
127
  */
159
- feePayer: PublicKey
128
+ feePayer
160
129
 
161
130
  /**
162
131
  * The transaction fee payer (first signer)
163
132
  */
164
- getFeePayer(): PublicKey | null {
133
+ getFeePayer() {
165
134
  if (this.signatures.length > 0) {
166
135
  return this.signatures[0].publicKey
167
136
  }
@@ -171,36 +140,34 @@ export class Transaction {
171
140
  /**
172
141
  * The instructions to atomically execute
173
142
  */
174
- instructions: Array<TransactionInstruction> = []
143
+ instructions = []
175
144
 
176
145
  /**
177
146
  * A recent transaction id. Must be populated by the caller
178
147
  */
179
- recentBlockhash: Blockhash | null
148
+ recentBlockhash
180
149
 
181
150
  /**
182
151
  * Optional Nonce information. If populated, transaction will use a durable
183
152
  * Nonce hash instead of a recentBlockhash. Must be populated by the caller
184
153
  */
185
- nonceInfo: NonceInformation | null
154
+ nonceInfo
186
155
 
187
156
  /**
188
157
  * Construct an empty Transaction
189
158
  */
190
- constructor(opts?: TransactionCtorFields) {
159
+ constructor(opts?) {
191
160
  opts && Object.assign(this, opts)
192
161
  }
193
162
  /**
194
163
  * Add one or more instructions to this Transaction
195
164
  */
196
- add(
197
- ...items: Array<Transaction | TransactionInstruction | TransactionInstructionCtorFields>
198
- ): Transaction {
165
+ add(...items) {
199
166
  if (items.length === 0) {
200
167
  throw new Error('No instructions')
201
168
  }
202
169
 
203
- items.forEach((item: any) => {
170
+ items.forEach((item) => {
204
171
  if ('instructions' in item) {
205
172
  this.instructions = this.instructions.concat(item.instructions)
206
173
  } else if ('data' in item && 'programId' in item && 'keys' in item) {
@@ -214,7 +181,7 @@ export class Transaction {
214
181
  /**
215
182
  * Compile transaction data
216
183
  */
217
- compileMessage(): Message {
184
+ compileMessage() {
218
185
  const { nonceInfo } = this
219
186
  if (nonceInfo && this.instructions[0] !== nonceInfo.nonceInstruction) {
220
187
  this.recentBlockhash = nonceInfo.nonce
@@ -229,7 +196,7 @@ export class Transaction {
229
196
  throw new Error('No instructions provided')
230
197
  }
231
198
 
232
- let feePayer: PublicKey
199
+ let feePayer
233
200
  if (this.feePayer) {
234
201
  feePayer = this.feePayer
235
202
  } else if (this.signatures.length > 0 && this.signatures[0].publicKey) {
@@ -239,8 +206,8 @@ export class Transaction {
239
206
  throw new Error('Transaction fee payer required')
240
207
  }
241
208
 
242
- const programIds: string[] = []
243
- const accountMetas: AccountMeta[] = []
209
+ const programIds = []
210
+ const accountMetas = []
244
211
  this.instructions.forEach((instruction) => {
245
212
  instruction.keys.forEach((accountMeta) => {
246
213
  accountMetas.push({ ...accountMeta })
@@ -278,7 +245,7 @@ export class Transaction {
278
245
  })
279
246
 
280
247
  // Cull duplicate account metas
281
- const uniqueMetas: AccountMeta[] = []
248
+ const uniqueMetas = []
282
249
  accountMetas.forEach((accountMeta) => {
283
250
  const pubkeyString = accountMeta.pubkey.toString()
284
251
  const uniqueIndex = uniqueMetas.findIndex((x) => {
@@ -339,8 +306,8 @@ export class Transaction {
339
306
  let numReadonlySignedAccounts = 0
340
307
  let numReadonlyUnsignedAccounts = 0
341
308
  // Split out signing from non-signing keys and count header values
342
- const signedKeys: string[] = []
343
- const unsignedKeys: string[] = []
309
+ const signedKeys = []
310
+ const unsignedKeys = []
344
311
  uniqueMetas.forEach(({ pubkey, isSigner, isWritable }) => {
345
312
  if (isSigner) {
346
313
  signedKeys.push(pubkey.toString())
@@ -357,7 +324,7 @@ export class Transaction {
357
324
  })
358
325
 
359
326
  const accountKeys = signedKeys.concat(unsignedKeys)
360
- const instructions: CompiledInstruction[] = this.instructions.map((instruction) => {
327
+ const instructions = this.instructions.map((instruction) => {
361
328
  const { data, programId } = instruction
362
329
  return {
363
330
  programIdIndex: accountKeys.indexOf(programId.toString()),
@@ -385,7 +352,7 @@ export class Transaction {
385
352
  /**
386
353
  * Get a buffer of the Transaction data that need to be covered by signatures
387
354
  */
388
- serializeMessage(): Buffer {
355
+ serializeMessage() {
389
356
  return this.compileMessage().serialize()
390
357
  }
391
358
  /**
@@ -394,7 +361,7 @@ export class Transaction {
394
361
  *
395
362
  * Signatures can be added with either `partialSign` or `addSignature`
396
363
  */
397
- setSigners(...signers: Array<PublicKey>) {
364
+ setSigners(...signers) {
398
365
  if (signers.length === 0) {
399
366
  throw new Error('No signers')
400
367
  }
@@ -424,7 +391,7 @@ export class Transaction {
424
391
  *
425
392
  * The Transaction must be assigned a valid `recentBlockhash` before invoking this method
426
393
  */
427
- sign(...signers: Array<Account>) {
394
+ sign(...signers) {
428
395
  if (signers.length === 0) {
429
396
  throw new Error('No signers')
430
397
  }
@@ -453,7 +420,7 @@ export class Transaction {
453
420
  *
454
421
  * All the caveats from the `sign` method apply to `partialSign`
455
422
  */
456
- partialSign(...signers: Array<Account>) {
423
+ partialSign(...signers) {
457
424
  if (signers.length === 0) {
458
425
  throw new Error('No signers')
459
426
  }
@@ -475,7 +442,7 @@ export class Transaction {
475
442
  * Add an externally created signature to a transaction. The public key
476
443
  * must correspond to a public key that was previously provided to `setSigners`.
477
444
  */
478
- addSignature(pubkey: PublicKey, signature: Buffer) {
445
+ addSignature(pubkey, signature) {
479
446
  invariant(signature.length === 64)
480
447
 
481
448
  const index = this.signatures.findIndex((sigpair) => pubkey.equals(sigpair.publicKey))
@@ -488,13 +455,13 @@ export class Transaction {
488
455
  /**
489
456
  * Verify signatures of a complete, signed Transaction
490
457
  */
491
- verifySignatures(): boolean {
458
+ verifySignatures() {
492
459
  return this._verifySignatures(this.serializeMessage(), true)
493
460
  }
494
461
  /**
495
462
  * @private
496
463
  */
497
- _verifySignatures(signData: Buffer, requireAllSignatures: boolean): boolean {
464
+ _verifySignatures(signData, requireAllSignatures) {
498
465
  for (const { signature, publicKey } of this.signatures) {
499
466
  if (signature === null) {
500
467
  if (requireAllSignatures) {
@@ -511,7 +478,7 @@ export class Transaction {
511
478
  /**
512
479
  * Serialize the Transaction in the wire format.
513
480
  */
514
- serialize(config?: SerializeConfig): Buffer {
481
+ serialize(config?) {
515
482
  const { requireAllSignatures, verifySignatures } = Object.assign(
516
483
  { requireAllSignatures: true, verifySignatures: true },
517
484
  config
@@ -527,7 +494,7 @@ export class Transaction {
527
494
  /**
528
495
  * @private
529
496
  */
530
- _serialize(signData: Buffer): Buffer {
497
+ _serialize(signData) {
531
498
  const { signatures } = this
532
499
  const signatureCount = []
533
500
  shortvec.encodeLength(signatureCount, signatures.length)
@@ -552,7 +519,7 @@ export class Transaction {
552
519
  * Deprecated method
553
520
  * @private
554
521
  */
555
- get keys(): Array<PublicKey> {
522
+ get keys() {
556
523
  invariant(this.instructions.length === 1)
557
524
  return this.instructions[0].keys.map((keyObj) => keyObj.pubkey)
558
525
  }
@@ -560,7 +527,7 @@ export class Transaction {
560
527
  * Deprecated method
561
528
  * @private
562
529
  */
563
- get programId(): PublicKey {
530
+ get programId() {
564
531
  invariant(this.instructions.length === 1)
565
532
  return this.instructions[0].programId
566
533
  }
@@ -568,14 +535,14 @@ export class Transaction {
568
535
  * Deprecated method
569
536
  * @private
570
537
  */
571
- get data(): Buffer {
538
+ get data() {
572
539
  invariant(this.instructions.length === 1)
573
540
  return this.instructions[0].data
574
541
  }
575
542
  /**
576
543
  * Parse a wire transaction into a Transaction object.
577
544
  */
578
- static from(buffer: Buffer | Uint8Array | Array<number>): Transaction {
545
+ static from(buffer) {
579
546
  // Slice up wire data
580
547
  let byteArray = [...buffer]
581
548
 
@@ -592,7 +559,7 @@ export class Transaction {
592
559
  /**
593
560
  * Populate Transaction object from message and signatures
594
561
  */
595
- static populate(message: Message, signatures: Array<string>): Transaction {
562
+ static populate(message, signatures) {
596
563
  const transaction = new Transaction()
597
564
  transaction.recentBlockhash = message.recentBlockhash
598
565
  signatures.forEach((signature, index) => {
@@ -1,6 +1,3 @@
1
- // @flow
2
-
3
1
  /**
4
2
  * @typedef {string} Blockhash
5
3
  */
6
- export type Blockhash = string
@@ -1,4 +1,3 @@
1
- // @flow
2
1
  import * as BufferLayout from '@exodus/buffer-layout'
3
2
 
4
3
  /**
@@ -12,6 +11,3 @@ export const FeeCalculatorLayout = BufferLayout.nu64('lamportsPerSignature')
12
11
  * @typedef {Object} FeeCalculator
13
12
  * @property {number} lamportsPerSignature lamports Cost in lamports to validate a signature
14
13
  */
15
- export type FeeCalculator = {
16
- lamportsPerSignature: number,
17
- }
@@ -1,25 +1,24 @@
1
- // @flow
2
1
  import BN from 'bn.js'
3
2
  import * as BufferLayout from '@exodus/buffer-layout'
4
3
 
5
4
  /**
6
5
  * Layout for a public key
7
6
  */
8
- export const publicKey = (property: string = 'publicKey'): Object => {
7
+ export const publicKey = (property = 'publicKey') => {
9
8
  return BufferLayout.blob(32, property)
10
9
  }
11
10
 
12
11
  /**
13
12
  * Layout for a 64bit unsigned value
14
13
  */
15
- export const uint64 = (property: string = 'uint64'): Object => {
14
+ export const uint64 = (property = 'uint64') => {
16
15
  return BufferLayout.blob(8, property)
17
16
  }
18
17
 
19
18
  /**
20
19
  * Layout for a Rust String type
21
20
  */
22
- export const rustString = (property: string = 'string') => {
21
+ export const rustString = (property = 'string') => {
23
22
  const rsl = BufferLayout.struct(
24
23
  [
25
24
  BufferLayout.u32('length'),
@@ -53,21 +52,21 @@ export const rustString = (property: string = 'string') => {
53
52
  /**
54
53
  * Layout for an Authorized object
55
54
  */
56
- export const authorized = (property: string = 'authorized') => {
55
+ export const authorized = (property = 'authorized') => {
57
56
  return BufferLayout.struct([publicKey('staker'), publicKey('withdrawer')], property)
58
57
  }
59
58
 
60
59
  /**
61
60
  * Layout for a Lockup object
62
61
  */
63
- export const lockup = (property: string = 'lockup') => {
62
+ export const lockup = (property = 'lockup') => {
64
63
  return BufferLayout.struct(
65
64
  [BufferLayout.ns64('unixTimestamp'), BufferLayout.ns64('epoch'), publicKey('custodian')],
66
65
  property
67
66
  )
68
67
  }
69
68
 
70
- export const bnAmountU64 = (property: string) => {
69
+ export const bnAmountU64 = (property) => {
71
70
  const rsl = BufferLayout.blob(8, property)
72
71
 
73
72
  const _decode = rsl.decode.bind(rsl)
@@ -84,7 +83,7 @@ export const bnAmountU64 = (property: string) => {
84
83
  return rsl
85
84
  }
86
85
 
87
- export function getAlloc(type: Object, fields: Object): number {
86
+ export function getAlloc(type, fields) {
88
87
  let alloc = 0
89
88
  type.layout.fields.forEach((item) => {
90
89
  if (item.span >= 0) {
@@ -1,6 +1,4 @@
1
- // @flow
2
-
3
- export function decodeLength(bytes: Array<number>): number {
1
+ export function decodeLength(bytes) {
4
2
  let len = 0
5
3
  let size = 0
6
4
  for (;;) {
@@ -14,7 +12,7 @@ export function decodeLength(bytes: Array<number>): number {
14
12
  return len
15
13
  }
16
14
 
17
- export function encodeLength(bytes: Array<number>, len: number) {
15
+ export function encodeLength(bytes, len) {
18
16
  let remLen = len
19
17
  for (;;) {
20
18
  let elem = remLen & 0x7f
@@ -1,4 +1,4 @@
1
- export const toBuffer = (arr: Buffer | Uint8Array | Array<number>): Buffer => {
1
+ export const toBuffer = (arr) => {
2
2
  if (arr instanceof Buffer) {
3
3
  return arr
4
4
  } else if (arr instanceof Uint8Array) {
@@ -14,7 +14,7 @@ class VersionedTx {
14
14
  // tx is not supported from the vendored library (solana-lib/src/vendor).
15
15
  // We also can't make any guarantees that Account from vendored lib with version X
16
16
  // will be compatible with the tx built by an outside library with version Y.
17
- static sign(tx, privateKey: Buffer | String, extraSigners: Account[] = []) {
17
+ static sign(tx, privateKey, extraSigners = []) {
18
18
  if (!privateKey) {
19
19
  throw new Error('Please provide a secretKey')
20
20
  }