@solana/transaction-messages 2.0.0-20241006045741

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.
Files changed (63) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +226 -0
  3. package/dist/index.browser.cjs +928 -0
  4. package/dist/index.browser.cjs.map +1 -0
  5. package/dist/index.browser.mjs +905 -0
  6. package/dist/index.browser.mjs.map +1 -0
  7. package/dist/index.native.mjs +905 -0
  8. package/dist/index.native.mjs.map +1 -0
  9. package/dist/index.node.cjs +928 -0
  10. package/dist/index.node.cjs.map +1 -0
  11. package/dist/index.node.mjs +905 -0
  12. package/dist/index.node.mjs.map +1 -0
  13. package/dist/types/addresses-by-lookup-table-address.d.ts +5 -0
  14. package/dist/types/addresses-by-lookup-table-address.d.ts.map +1 -0
  15. package/dist/types/blockhash.d.ts +16 -0
  16. package/dist/types/blockhash.d.ts.map +1 -0
  17. package/dist/types/codecs/address-table-lookup.d.ts +8 -0
  18. package/dist/types/codecs/address-table-lookup.d.ts.map +1 -0
  19. package/dist/types/codecs/header.d.ts +8 -0
  20. package/dist/types/codecs/header.d.ts.map +1 -0
  21. package/dist/types/codecs/index.d.ts +3 -0
  22. package/dist/types/codecs/index.d.ts.map +1 -0
  23. package/dist/types/codecs/instruction.d.ts +8 -0
  24. package/dist/types/codecs/instruction.d.ts.map +1 -0
  25. package/dist/types/codecs/message.d.ts +6 -0
  26. package/dist/types/codecs/message.d.ts.map +1 -0
  27. package/dist/types/codecs/transaction-version.d.ts +6 -0
  28. package/dist/types/codecs/transaction-version.d.ts.map +1 -0
  29. package/dist/types/compilable-transaction-message.d.ts +7 -0
  30. package/dist/types/compilable-transaction-message.d.ts.map +1 -0
  31. package/dist/types/compile/accounts.d.ts +28 -0
  32. package/dist/types/compile/accounts.d.ts.map +1 -0
  33. package/dist/types/compile/address-table-lookups.d.ts +10 -0
  34. package/dist/types/compile/address-table-lookups.d.ts.map +1 -0
  35. package/dist/types/compile/header.d.ts +9 -0
  36. package/dist/types/compile/header.d.ts.map +1 -0
  37. package/dist/types/compile/index.d.ts +2 -0
  38. package/dist/types/compile/index.d.ts.map +1 -0
  39. package/dist/types/compile/instructions.d.ts +10 -0
  40. package/dist/types/compile/instructions.d.ts.map +1 -0
  41. package/dist/types/compile/lifetime-token.d.ts +3 -0
  42. package/dist/types/compile/lifetime-token.d.ts.map +1 -0
  43. package/dist/types/compile/message.d.ts +26 -0
  44. package/dist/types/compile/message.d.ts.map +1 -0
  45. package/dist/types/compile/static-accounts.d.ts +4 -0
  46. package/dist/types/compile/static-accounts.d.ts.map +1 -0
  47. package/dist/types/compress-transaction-message.d.ts +14 -0
  48. package/dist/types/compress-transaction-message.d.ts.map +1 -0
  49. package/dist/types/create-transaction-message.d.ts +9 -0
  50. package/dist/types/create-transaction-message.d.ts.map +1 -0
  51. package/dist/types/decompile-message.d.ts +9 -0
  52. package/dist/types/decompile-message.d.ts.map +1 -0
  53. package/dist/types/durable-nonce.d.ts +35 -0
  54. package/dist/types/durable-nonce.d.ts.map +1 -0
  55. package/dist/types/fee-payer.d.ts +7 -0
  56. package/dist/types/fee-payer.d.ts.map +1 -0
  57. package/dist/types/index.d.ts +13 -0
  58. package/dist/types/index.d.ts.map +1 -0
  59. package/dist/types/instructions.d.ts +6 -0
  60. package/dist/types/instructions.d.ts.map +1 -0
  61. package/dist/types/transaction-message.d.ts +12 -0
  62. package/dist/types/transaction-message.d.ts.map +1 -0
  63. package/package.json +93 -0
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2023 Solana Labs, Inc
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,226 @@
1
+ [![npm][npm-image]][npm-url]
2
+ [![npm-downloads][npm-downloads-image]][npm-url]
3
+ [![semantic-release][semantic-release-image]][semantic-release-url]
4
+ <br />
5
+ [![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
6
+
7
+ [code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
8
+ [code-style-prettier-url]: https://github.com/prettier/prettier
9
+ [npm-downloads-image]: https://img.shields.io/npm/dm/@solana/transactions/rc.svg?style=flat
10
+ [npm-image]: https://img.shields.io/npm/v/@solana/transactions/rc.svg?style=flat
11
+ [npm-url]: https://www.npmjs.com/package/@solana/transactions/v/rc
12
+ [semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
13
+ [semantic-release-url]: https://github.com/semantic-release/semantic-release
14
+
15
+ # @solana/transaction-messages
16
+
17
+ This package contains types and functions for creating transaction messages. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@rc`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).
18
+
19
+ Transaction messages are built one step at a time using the transform functions offered by this package. To make it more ergonomic to apply consecutive transforms to your transaction messages, consider using a pipelining helper like the one in `@solana/functional`.
20
+
21
+ ```ts
22
+ import { pipe } from '@solana/functional';
23
+ import {
24
+ appendTransactionMessageInstruction,
25
+ createTransactionMessage,
26
+ setTransactionMessageFeePayer,
27
+ setTransactionMessageLifetimeUsingBlockhash,
28
+ } from '@solana/transaction-messages';
29
+
30
+ const transferTransaction = pipe(
31
+ createTransactionMessage({ version: 0 }),
32
+ tx => setTransactionMessageFeePayer(myAddress, tx),
33
+ tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
34
+ tx => appendTransactionMessageInstruction(createTransferInstruction(myAddress, toAddress, amountInLamports), tx),
35
+ );
36
+ ```
37
+
38
+ ## Creating transaction messages
39
+
40
+ ### Types
41
+
42
+ #### `TransactionVersion`
43
+
44
+ As Solana transactions acquire more capabilities their version will advance. This type is a union of all possible transaction versions.
45
+
46
+ ### Functions
47
+
48
+ #### `createTransactionMessage()`
49
+
50
+ Given a `TransactionVersion` this method will return an empty transaction having the capabilities of that version.
51
+
52
+ ```ts
53
+ import { createTransactionMessage } from '@solana/transaction-messages';
54
+
55
+ const tx = createTransactionMessage({ version: 0 });
56
+ ```
57
+
58
+ ## Setting the fee payer
59
+
60
+ ### Types
61
+
62
+ #### `ITransactionMessageWithFeePayer`
63
+
64
+ This type represents a transaction message for which a fee payer has been declared. A transaction must conform to this type to be compiled and landed on the network.
65
+
66
+ ### Functions
67
+
68
+ #### `setTransactionMessageFeePayer()`
69
+
70
+ Given a base58-encoded address of a system account, this method will return a new transaction message having the same type as the one supplied plus the `ITransactionMessageWithFeePayer` type.
71
+
72
+ ```ts
73
+ import { address } from '@solana/addresses';
74
+ import { setTransactionMessageFeePayer } from '@solana/transaction-messages';
75
+
76
+ const myAddress = address('mpngsFd4tmbUfzDYJayjKZwZcaR7aWb2793J6grLsGu');
77
+ const txPaidByMe = setTransactionMessageFeePayer(myAddress, tx);
78
+ ```
79
+
80
+ ## Defining a transaction message's lifetime
81
+
82
+ A signed transaction can be only be landed on the network if certain conditions are met:
83
+
84
+ - It includes the hash of a recent block
85
+ - Or it includes the value of an unused nonce known to the network
86
+
87
+ These conditions define a transaction's lifetime, after which it can no longer be landed, even if signed. The lifetime must be added to the transaction message before it is compiled to be sent.
88
+
89
+ ### Types
90
+
91
+ #### `TransactionMessageWithBlockhashLifetime`
92
+
93
+ This type represents a transaction message whose lifetime is defined by the age of the blockhash it includes. Such a transaction can only be landed on the network if the current block height of the network is less than or equal to the value of `TransactionMessageWithBlockhashLifetime['lifetimeConstraint']['lastValidBlockHeight']`.
94
+
95
+ #### `TransactionMessageWithDurableNonceLifetime`
96
+
97
+ This type represents a transaction message whose lifetime is defined by the value of a nonce it includes. Such a transaction can only be landed on the network if the nonce is known to the network and has not already been used to land a different transaction.
98
+
99
+ #### `Blockhash`
100
+
101
+ This type represents a string that is particularly known to be the base58-encoded value of a block.
102
+
103
+ #### `Nonce`
104
+
105
+ This type represents a string that is particularly known to be the base58-encoded value of a nonce.
106
+
107
+ ### Functions
108
+
109
+ #### `setTransactionMessageLifetimeUsingBlockhash()`
110
+
111
+ Given a blockhash and the last block height at which that blockhash is considered usable to land transactions, this method will return a new transaction message having the same type as the one supplied plus the `TransactionMessageWithBlockhashLifetime` type.
112
+
113
+ ```ts
114
+ import { setTransactionMessageLifetimeUsingBlockhash } from '@solana/transaction-messages';
115
+
116
+ const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
117
+ const txWithBlockhashLifetime = setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx);
118
+ ```
119
+
120
+ #### `setTransactionMessageLifetimeUsingDurableNonce()`
121
+
122
+ Given a nonce, the account where the value of the nonce is stored, and the address of the account authorized to consume that nonce, this method will return a new transaction having the same type as the one supplied plus the `TransactionMessageWithDurableNonceLifetime` type. In particular, this method _prepends_ an instruction to the transaction message designed to consume (or &lsquo;advance&rsquo;) the nonce in the same transaction whose lifetime is defined by it.
123
+
124
+ ```ts
125
+ import { setTransactionMessageLifetimeUsingDurableNonce } from '@solana/transactions';
126
+
127
+ const NONCE_VALUE_OFFSET =
128
+ 4 + // version(u32)
129
+ 4 + // state(u32)
130
+ 32; // nonce authority(pubkey)
131
+ // Then comes the nonce value.
132
+
133
+ const nonceAccountAddress = address('EGtMh4yvXswwHhwVhyPxGrVV2TkLTgUqGodbATEPvojZ');
134
+ const nonceAuthorityAddress = address('4KD1Rdrd89NG7XbzW3xsX9Aqnx2EExJvExiNme6g9iAT');
135
+ const { value: nonceAccount } = await rpc
136
+ .getAccountInfo(nonceAccountAddress, {
137
+ dataSlice: { length: 32, offset: NONCE_VALUE_OFFSET },
138
+ encoding: 'base58',
139
+ })
140
+ .send();
141
+ const nonce =
142
+ // This works because we asked for the exact slice of data representing the nonce
143
+ // value, and furthermore asked for it in `base58` encoding.
144
+ nonceAccount!.data[0] as unknown as Nonce;
145
+
146
+ const durableNonceTransactionMessage = setTransactionMessageLifetimeUsingDurableNonce(
147
+ { nonce, nonceAccountAddress, nonceAuthorityAddress },
148
+ tx,
149
+ );
150
+ ```
151
+
152
+ #### `assertIsBlockhash()`
153
+
154
+ Client applications primarily deal with blockhashes in the form of base58-encoded strings. Blockhashes returned from the RPC API conform to the type `Blockhash`. You can use a value of that type wherever a blockhash is expected.
155
+
156
+ From time to time you might acquire a string, that you expect to validate as a blockhash, from an untrusted network API or user input. To assert that such an arbitrary string is a base58-encoded blockhash, use the `assertIsBlockhash` function.
157
+
158
+ ```ts
159
+ import { assertIsBlockhash } from '@solana/transaction-messages';
160
+
161
+ // Imagine a function that asserts whether a user-supplied blockhash is valid or not.
162
+ function handleSubmit() {
163
+ // We know only that what the user typed conforms to the `string` type.
164
+ const blockhash: string = blockhashInput.value;
165
+ try {
166
+ // If this type assertion function doesn't throw, then
167
+ // Typescript will upcast `blockhash` to `Blockhash`.
168
+ assertIsBlockhash(blockhash);
169
+ // At this point, `blockhash` is a `Blockhash` that can be used with the RPC.
170
+ const blockhashIsValid = await rpc.isBlockhashValid(blockhash).send();
171
+ } catch (e) {
172
+ // `blockhash` turned out not to be a base58-encoded blockhash
173
+ }
174
+ }
175
+ ```
176
+
177
+ #### `assertIsDurableNonceTransactionMessage()`
178
+
179
+ From time to time you might acquire a transaction message that you expect to be a durable nonce transaction, from an untrusted network API or user input. To assert that such an arbitrary transaction is in fact a durable nonce transaction, use the `assertIsDurableNonceTransactionMessage` function.
180
+
181
+ See [`assertIsBlockhash()`](#assertisblockhash) for an example of how to use an assertion function.
182
+
183
+ ## Adding instructions to a transaction message
184
+
185
+ ### Types
186
+
187
+ #### `IInstruction`
188
+
189
+ This type represents an instruction to be issued to a program. Objects that conform to this type have a `programAddress` property that is the base58-encoded address of the program in question.
190
+
191
+ #### `IInstructionWithAccounts`
192
+
193
+ This type represents an instruction that specifies a list of accounts that a program may read from, write to, or require be signers of the transaction itself. Objects that conform to this type have an `accounts` property that is an array of `IAccountMeta | IAccountLookupMeta` in the order the instruction requires.
194
+
195
+ #### `IInstructionWithData`
196
+
197
+ This type represents an instruction that supplies some data as input to the program. Objects that conform to this type have a `data` property that can be any type of `Uint8Array`.
198
+
199
+ ### Functions
200
+
201
+ #### `appendTransactionMessageInstruction()`
202
+
203
+ Given an instruction, this method will return a new transaction message with that instruction having been added to the end of the list of existing instructions.
204
+
205
+ ```ts
206
+ import { address } from '@solana/addresses';
207
+ import { appendTransactionMessageInstruction } from '@solana/transaction-messages';
208
+
209
+ const memoTransaction = appendTransactionMessageInstruction(
210
+ {
211
+ data: new TextEncoder().encode('Hello world!'),
212
+ programAddress: address('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'),
213
+ },
214
+ tx,
215
+ );
216
+ ```
217
+
218
+ If you'd like to add multiple instructions to a transaction message at once, you may use the `appendTransactionInstructions` function instead which accepts an array of instructions.
219
+
220
+ #### `prependTransactionMessageInstruction()`
221
+
222
+ Given an instruction, this method will return a new transaction message with that instruction having been added to the beginning of the list of existing instructions.
223
+
224
+ If you'd like to prepend multiple instructions to a transaction message at once, you may use the `prependTransactionMessageInstructions` function instead which accepts an array of instructions.
225
+
226
+ See [`appendTransactionMessageInstruction()`](#appendtransactioninstruction) for an example of how to use this function.