@kynesyslabs/demosdk 1.0.21 → 1.0.23

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 (180) hide show
  1. package/build/encryption/Cryptography.d.ts +18 -1
  2. package/build/encryption/Cryptography.js +182 -1
  3. package/build/encryption/Cryptography.js.map +1 -1
  4. package/build/encryption/Hashing.d.ts +1 -1
  5. package/build/encryption/Hashing.js +2 -1
  6. package/build/encryption/Hashing.js.map +1 -1
  7. package/build/encryption/index.d.ts +2 -2
  8. package/build/encryption/index.js +4 -2
  9. package/build/encryption/index.js.map +1 -1
  10. package/build/index.d.ts +1 -0
  11. package/build/index.js +2 -1
  12. package/build/index.js.map +1 -1
  13. package/build/multichain/core/solana.d.ts +16 -29
  14. package/build/multichain/core/solana.js +40 -126
  15. package/build/multichain/core/solana.js.map +1 -1
  16. package/build/multichain/core/types/defaultChain.d.ts +0 -18
  17. package/build/multichain/localsdk/index.d.ts +5 -4
  18. package/build/multichain/localsdk/index.js +3 -1
  19. package/build/multichain/localsdk/index.js.map +1 -1
  20. package/build/multichain/localsdk/solana.d.ts +1 -3
  21. package/build/multichain/localsdk/solana.js +5 -10
  22. package/build/multichain/localsdk/solana.js.map +1 -1
  23. package/build/multichain/websdk/index.d.ts +1 -0
  24. package/build/multichain/websdk/index.js +3 -1
  25. package/build/multichain/websdk/index.js.map +1 -1
  26. package/build/multichain/websdk/solana.d.ts +4 -0
  27. package/build/multichain/websdk/solana.js +11 -0
  28. package/build/multichain/websdk/solana.js.map +1 -0
  29. package/build/types/blockchain/Confirmation.d.ts +9 -0
  30. package/build/types/blockchain/Confirmation.js +23 -0
  31. package/build/types/blockchain/Confirmation.js.map +1 -0
  32. package/build/types/blockchain/WalletTypes.d.ts +3 -0
  33. package/build/types/blockchain/WalletTypes.js +3 -0
  34. package/build/types/blockchain/WalletTypes.js.map +1 -0
  35. package/build/utils/dataManipulation.d.ts +1 -0
  36. package/build/utils/dataManipulation.js +5 -1
  37. package/build/utils/dataManipulation.js.map +1 -1
  38. package/build/utils/getRemoteIP.d.ts +1 -0
  39. package/build/utils/getRemoteIP.js +19 -0
  40. package/build/utils/getRemoteIP.js.map +1 -0
  41. package/build/wallet/Wallet.d.ts +25 -0
  42. package/build/wallet/Wallet.js +94 -0
  43. package/build/wallet/Wallet.js.map +1 -0
  44. package/build/wallet/index.d.ts +1 -0
  45. package/build/wallet/index.js +28 -0
  46. package/build/wallet/index.js.map +1 -0
  47. package/build/websdk/DemosTransactions.d.ts +1 -1
  48. package/build/websdk/DemosTransactions.js +3 -2
  49. package/build/websdk/DemosTransactions.js.map +1 -1
  50. package/build/websdk/demos.d.ts +2 -2
  51. package/package.json +55 -57
  52. package/.eslintignore +0 -6
  53. package/.eslintrc.cjs +0 -31
  54. package/.gitattributes +0 -4
  55. package/.github/workflows/publish.yml +0 -43
  56. package/.github/workflows/test:multichain.yml +0 -35
  57. package/.gitignore +0 -8
  58. package/.prettierrc +0 -13
  59. package/build/tests/multichain/chainProviders.d.ts +0 -29
  60. package/build/tests/multichain/chainProviders.js +0 -34
  61. package/build/tests/multichain/chainProviders.js.map +0 -1
  62. package/build/tests/multichain/evm.test.d.ts +0 -1
  63. package/build/tests/multichain/evm.test.js +0 -53
  64. package/build/tests/multichain/evm.test.js.map +0 -1
  65. package/build/tests/multichain/ibc.test.d.ts +0 -1
  66. package/build/tests/multichain/ibc.test.js +0 -74
  67. package/build/tests/multichain/ibc.test.js.map +0 -1
  68. package/build/tests/multichain/index.d.ts +0 -1
  69. package/build/tests/multichain/index.js +0 -94
  70. package/build/tests/multichain/index.js.map +0 -1
  71. package/build/tests/multichain/multiversx.test.d.ts +0 -1
  72. package/build/tests/multichain/multiversx.test.js +0 -49
  73. package/build/tests/multichain/multiversx.test.js.map +0 -1
  74. package/build/tests/multichain/solana.spec.d.ts +0 -1
  75. package/build/tests/multichain/solana.spec.js +0 -50
  76. package/build/tests/multichain/solana.spec.js.map +0 -1
  77. package/build/tests/multichain/template.test.d.ts +0 -0
  78. package/build/tests/multichain/template.test.js +0 -33
  79. package/build/tests/multichain/template.test.js.map +0 -1
  80. package/build/tests/multichain/xrpl.test.d.ts +0 -1
  81. package/build/tests/multichain/xrpl.test.js +0 -57
  82. package/build/tests/multichain/xrpl.test.js.map +0 -1
  83. package/build/tests/utils/index.d.ts +0 -14
  84. package/build/tests/utils/index.js +0 -34
  85. package/build/tests/utils/index.js.map +0 -1
  86. package/build/tests/utils/wallets.d.ts +0 -21
  87. package/build/tests/utils/wallets.js +0 -48
  88. package/build/tests/utils/wallets.js.map +0 -1
  89. package/build/tests/utils.test.d.ts +0 -1
  90. package/build/tests/utils.test.js +0 -19
  91. package/build/tests/utils.test.js.map +0 -1
  92. package/documentation/multichain/README.md +0 -85
  93. package/documentation/multichain/ibc.md +0 -3
  94. package/documentation/multichain/solana.md +0 -13
  95. package/jest.config.ts +0 -20
  96. package/src/encryption/Cryptography.ts +0 -128
  97. package/src/encryption/FHE/index.ts +0 -35
  98. package/src/encryption/Hashing.ts +0 -20
  99. package/src/encryption/PQC/index.ts +0 -260
  100. package/src/encryption/index.ts +0 -5
  101. package/src/encryption/zK/index.ts +0 -1
  102. package/src/encryption/zK/interactive/index.ts +0 -47
  103. package/src/encryption/zK/primer.ts +0 -71
  104. package/src/index.ts +0 -8
  105. package/src/multichain/archive/btc.ts +0 -72
  106. package/src/multichain/archive/demos.ts +0 -51
  107. package/src/multichain/archive/tron.ts +0 -86
  108. package/src/multichain/archive/xlm.ts +0 -65
  109. package/src/multichain/core/README.md +0 -1
  110. package/src/multichain/core/evm.ts +0 -275
  111. package/src/multichain/core/ibc.ts +0 -318
  112. package/src/multichain/core/index.ts +0 -29
  113. package/src/multichain/core/multiversx.ts +0 -310
  114. package/src/multichain/core/solana.ts +0 -336
  115. package/src/multichain/core/types/defaultChain.ts +0 -254
  116. package/src/multichain/core/types/interfaces.ts +0 -102
  117. package/src/multichain/core/utils.ts +0 -22
  118. package/src/multichain/core/xrp.ts +0 -253
  119. package/src/multichain/index.ts +0 -3
  120. package/src/multichain/localsdk/README.md +0 -1
  121. package/src/multichain/localsdk/evm.ts +0 -77
  122. package/src/multichain/localsdk/ibc.ts +0 -25
  123. package/src/multichain/localsdk/index.ts +0 -4
  124. package/src/multichain/localsdk/multiversx.ts +0 -66
  125. package/src/multichain/localsdk/solana.ts +0 -37
  126. package/src/multichain/localsdk/xrp.ts +0 -56
  127. package/src/multichain/websdk/README.md +0 -1
  128. package/src/multichain/websdk/evm.ts +0 -9
  129. package/src/multichain/websdk/ibc.ts +0 -10
  130. package/src/multichain/websdk/index.ts +0 -4
  131. package/src/multichain/websdk/multiversx.ts +0 -84
  132. package/src/multichain/websdk/xrp.ts +0 -9
  133. package/src/tests/multichain/chainProviders.ts +0 -32
  134. package/src/tests/multichain/evm.test.ts +0 -64
  135. package/src/tests/multichain/ibc.test.ts +0 -93
  136. package/src/tests/multichain/index.ts +0 -105
  137. package/src/tests/multichain/multiversx.test.ts +0 -57
  138. package/src/tests/multichain/solana.spec.ts +0 -56
  139. package/src/tests/multichain/template.test.ts +0 -37
  140. package/src/tests/multichain/xrpl.test.ts +0 -71
  141. package/src/tests/utils/index.ts +0 -34
  142. package/src/tests/utils/wallets.ts +0 -46
  143. package/src/tests/utils.test.ts +0 -21
  144. package/src/types/blockchain/ISignature.ts +0 -6
  145. package/src/types/blockchain/Transaction.ts +0 -34
  146. package/src/types/blockchain/TxFee.ts +0 -5
  147. package/src/types/blockchain/ValidityData.ts +0 -15
  148. package/src/types/blockchain/addressInfo.ts +0 -7
  149. package/src/types/blockchain/blocks.ts +0 -29
  150. package/src/types/blockchain/genesisTypes.ts +0 -39
  151. package/src/types/blockchain/rawTransaction.ts +0 -27
  152. package/src/types/blockchain/statusNative.ts +0 -6
  153. package/src/types/blockchain/statusProperties.ts +0 -8
  154. package/src/types/communication/transmit.ts +0 -27
  155. package/src/types/gls/Operation.ts +0 -25
  156. package/src/types/gls/StateChange.ts +0 -33
  157. package/src/types/index.ts +0 -60
  158. package/src/types/network/ExecutionResult.ts +0 -9
  159. package/src/types/network/SecurityTypes.ts +0 -18
  160. package/src/types/peers/Peer.ts +0 -18
  161. package/src/types/web2/index.ts +0 -76
  162. package/src/types/xm/index.ts +0 -21
  163. package/src/utils/dataManipulation.ts +0 -37
  164. package/src/utils/index.ts +0 -1
  165. package/src/websdk/DemosTransactions.ts +0 -74
  166. package/src/websdk/DemosWebAuth.ts +0 -186
  167. package/src/websdk/Web2Transactions.ts +0 -41
  168. package/src/websdk/XMTransactions.ts +0 -140
  169. package/src/websdk/demos.ts +0 -461
  170. package/src/websdk/index.ts +0 -15
  171. package/src/websdk/rsa.ts +0 -85
  172. package/src/websdk/types/IBuffer.ts +0 -4
  173. package/src/websdk/types/KeyPair.ts +0 -9
  174. package/src/websdk/utils/bufferizer.ts +0 -16
  175. package/src/websdk/utils/forge_converter.ts +0 -72
  176. package/src/websdk/utils/required.ts +0 -44
  177. package/src/websdk/utils/sha256.ts +0 -13
  178. package/src/websdk/utils/skeletons.ts +0 -69
  179. package/tsconfig.json +0 -34
  180. package/yarn.lock +0 -3705
@@ -1,310 +0,0 @@
1
- import {
2
- Account,
3
- Address,
4
- GasEstimator,
5
- IPlainTransactionObject,
6
- TokenTransfer,
7
- Transaction,
8
- TransferTransactionsFactory,
9
- } from '@multiversx/sdk-core'
10
- import { ExtensionProvider } from '@multiversx/sdk-extension-provider'
11
- import { ApiNetworkProvider } from '@multiversx/sdk-network-providers'
12
- import { INetworkProvider } from '@multiversx/sdk-network-providers/out/interface'
13
- import { UserSigner } from '@multiversx/sdk-wallet'
14
-
15
- import {
16
- DefaultChain,
17
- EGLDSignTxOptions,
18
- IPayOptions,
19
- required,
20
- } from '@/multichain/core'
21
-
22
- export class MULTIVERSX extends DefaultChain {
23
- declare provider: INetworkProvider
24
- declare wallet: UserSigner | ExtensionProvider
25
- chainId: string = ''
26
-
27
- constructor(rpc_url: string) {
28
- super(rpc_url)
29
- this.name = 'egld'
30
-
31
- if (rpc_url) {
32
- this.setRPC(rpc_url)
33
- }
34
- }
35
-
36
- setRPC(rpc_url: string) {
37
- this.rpc_url = rpc_url
38
- this.provider = new ApiNetworkProvider(this.rpc_url)
39
- }
40
-
41
- async connect() {
42
- required(this.provider, 'Provider not connected')
43
- try {
44
- const networkConfig = await this.provider.getNetworkConfig()
45
- this.chainId = networkConfig.ChainID
46
-
47
- this.connected = Boolean(this.chainId)
48
- return this.connected
49
- } catch (error) {
50
- this.connected = false
51
- }
52
-
53
- return false
54
- }
55
-
56
- // REVIEW: Where is this needed?
57
- // Answer to that: src/routes/crosschain/xM_WalletConnectionCard.svelte
58
- // static async create(rpc_url: any) {
59
- // const instance = new MULTIVERSX(rpc_url)
60
-
61
- // if (rpc_url) {
62
- // await instance.connect()
63
- // }
64
-
65
- // return instance
66
- // }
67
-
68
- async connectKeyFileWallet(keyFile: string, password: string) {
69
- try {
70
- // INFO: Parse the keyFile JSON string
71
- keyFile = JSON.parse(keyFile)
72
- } catch (error) {
73
- throw new Error('Failed to load the wallet. Invalid KeyFile!')
74
- }
75
-
76
- return UserSigner.fromWallet(keyFile, password)
77
- }
78
-
79
- async connectWallet(privateKey: string, options: { password: string }) {
80
- // INFO: This method is overriden in the web sdk
81
- // to connect with the extension wallet
82
- this.wallet = await this.connectKeyFileWallet(
83
- privateKey,
84
- options?.password as string
85
- )
86
- return this.wallet
87
- }
88
-
89
- getAddress() {
90
- // INFO: method is overriden in the web sdk
91
- required(this.wallet, 'Wallet not connected')
92
-
93
- if (this.wallet instanceof ExtensionProvider) {
94
- return this.wallet.account.address
95
- } else if (this.wallet instanceof UserSigner) {
96
- return this.wallet.getAddress().bech32()
97
- }
98
-
99
- throw new Error("Unknown wallet type. Can't get address.")
100
- }
101
-
102
- // SECTION: Reads
103
-
104
- async getBalance(address: string): Promise<string> {
105
- required(address, 'address is required to get the balance')
106
-
107
- const Iaddress = new Address(address)
108
- const account = await this.provider.getAccount(Iaddress)
109
-
110
- return account.balance.toString()
111
- }
112
-
113
- async getTokenBalance(token_id: string) {
114
- required(this.provider)
115
- required(this.wallet)
116
-
117
- const walletAddress = this.getAddress()
118
- const address = new Address(walletAddress)
119
- const token = await this.provider.getFungibleTokenOfAccount(
120
- address,
121
- token_id
122
- )
123
-
124
- return token.balance.toNumber()
125
- }
126
-
127
- async getNFTs() {
128
- required(this.provider)
129
- required(this.wallet)
130
-
131
- const address = this.getAddress()
132
- const account = new Address(address)
133
- return this.provider.getNonFungibleTokensOfAccount(account)
134
- }
135
-
136
- // /**
137
- // * Connect a Multiversx wallet. Pass the `keyFile` JSON string as the `privateKey` and its `password` to override the De-Fi wallet prompt.
138
- // */
139
- // async connectWallet(
140
- // privateKey?: string,
141
- // options?: {
142
- // password: string;
143
- // }
144
- // ) {
145
- // if (privateKey && options?.password) {
146
- // return this.connectKeyFileWallet(privateKey, options.password);
147
- // }
148
-
149
- // this.wallet = ExtensionProvider.getInstance();
150
-
151
- // await this.wallet.init();
152
-
153
- // // INFO: wallet.isInitialized() checks if the wallet is installed.
154
- // // It's equivalent to checking for `window.elrondWallet` object which is injected by the extension
155
- // // LINK to src: https://github.com/multiversx/mx-sdk-js-extension-provider/blob/36518d0fe0b295de8d2b977727f0c30cdc014c78/src/extensionProvider.ts#L45
156
-
157
- // if (!this.wallet.isInitialized()) {
158
- // throw new Error('Wallet not detected. Is the MultiversX DeFi Wallet extension installed?');
159
- // }
160
-
161
- // await this.wallet.login();
162
- // return this.wallet.isConnected();
163
- // }
164
-
165
- // SECTION: Writes
166
- /**
167
- * Signs a single transaction. Calls `signTransactions` with a single transaction and returns the first element of the result.
168
- * @returns The signed transaction as a plain object
169
- */
170
- async signTransaction(
171
- transaction: Transaction,
172
- options: EGLDSignTxOptions
173
- ) {
174
- const txs = await this.signTransactions([transaction], options)
175
- return txs[0]
176
- }
177
-
178
- protected async addTxNonces(transactions: Transaction[]) {
179
- required(this.provider, 'Provider not connected')
180
-
181
- // INFO: Override wallet connection
182
- // if (options?.privateKey) {
183
- // await this.connectWallet(options.privateKey, {
184
- // password: options.password,
185
- // })
186
- // }
187
-
188
- // INFO: Retrieve account on network to get nonce
189
- const senderBech32 = this.getAddress()
190
- const senderAddress = new Address(senderBech32)
191
-
192
- const account = new Account(senderAddress)
193
- const senderOnNetwork = await this.provider.getAccount(senderAddress)
194
- account.update(senderOnNetwork)
195
-
196
- // INFO: Local nonce management
197
- // LINK: https://docs.multiversx.com/integrators/creating-transactions/#nonce-management
198
- let currentNonce = account.nonce.valueOf()
199
-
200
- transactions.forEach((tx) => {
201
- tx.setNonce(currentNonce)
202
- // INFO: Increment the nonce for the next tx
203
- currentNonce++
204
- })
205
-
206
- return transactions
207
- }
208
-
209
- /**
210
- * Signs multiple transactions
211
- * @returns The signed transactions as an array of plain objects
212
- */
213
- async signTransactions(
214
- transactions: Transaction[],
215
- options?: EGLDSignTxOptions
216
- ): Promise<IPlainTransactionObject[]> {
217
- required(this.wallet || options?.privateKey, 'Wallet not connected')
218
-
219
- // INFO: Override wallet connection
220
- if (options?.privateKey) {
221
- await this.connectWallet(options.privateKey, {
222
- password: options.password,
223
- })
224
- }
225
-
226
- // INFO: Retrieve account on network to get nonce
227
- // const senderBech32 = this.getAddress()
228
- // const senderAddress = new Address(senderBech32)
229
-
230
- // const account = new Account(senderAddress)
231
- // const senderOnNetwork = await this.provider.getAccount(senderAddress)
232
- // account.update(senderOnNetwork)
233
-
234
- // // INFO: Local nonce management
235
- // // LINK: https://docs.multiversx.com/integrators/creating-transactions/#nonce-management
236
- // let currentNonce = account.nonce.valueOf()
237
-
238
- // transactions.forEach((tx) => {
239
- // tx.setNonce(currentNonce)
240
- // // INFO: Increment the nonce for the next tx
241
- // currentNonce++
242
- // })
243
-
244
- transactions = await this.addTxNonces(transactions)
245
-
246
- // try {
247
- // // INFO: When wallet is loaded from Extension
248
- // transactions = await (
249
- // this.wallet as ExtensionProvider
250
- // ).signTransactions(transactions)
251
- // } catch (error) {
252
- for (const tx of transactions) {
253
- const serializedTx = tx.serializeForSigning()
254
- const txSign = await (this.wallet as UserSigner).sign(serializedTx)
255
-
256
- tx.applySignature(txSign)
257
- }
258
- // }
259
-
260
- return transactions.map((tx) => {
261
- // INFO: Return plain objects
262
- return tx.toSendable()
263
- })
264
- }
265
-
266
- async preparePay(address: string, amount: string) {
267
- const txs = await this.preparePays([{ address, amount }])
268
- return txs[0]
269
- }
270
-
271
- async preparePays(payments: IPayOptions[]) {
272
- required(this.wallet)
273
-
274
- const sender = this.getAddress()
275
- const senderAddress = new Address(sender)
276
-
277
- const transactions = payments.map((payment) => {
278
- const transfer = TokenTransfer.egldFromAmount(payment.amount)
279
- const receiverAddress = new Address(payment.address)
280
- const factory = new TransferTransactionsFactory(new GasEstimator())
281
-
282
- return factory.createEGLDTransfer({
283
- value: transfer,
284
- sender: senderAddress,
285
- receiver: receiverAddress,
286
- chainID: this.chainId,
287
- })
288
- })
289
-
290
- return this.signTransactions(transactions)
291
- }
292
-
293
- prepareTransfer(receiver: string, amount: string) {
294
- return this.preparePay(receiver, amount)
295
- }
296
-
297
- prepareTransfers(tranfers: IPayOptions[]) {
298
- return this.preparePays(tranfers)
299
- }
300
-
301
- async disconnect() {
302
- this.resetInstance()
303
- return !this.connected
304
- }
305
-
306
- // SECTION Unimplemented methods
307
- getEmptyTransaction() {
308
- throw new Error('Method not implemented.')
309
- }
310
- }
@@ -1,336 +0,0 @@
1
- import bs58 from "bs58"
2
- import {
3
- Keypair,
4
- PublicKey,
5
- Connection,
6
- Transaction,
7
- NonceAccount,
8
- SystemProgram,
9
- LAMPORTS_PER_SOL,
10
- TransactionNonceCtor,
11
- NONCE_ACCOUNT_LENGTH,
12
- TransactionInstruction,
13
- } from "@solana/web3.js"
14
-
15
- import { required } from "./utils"
16
- import { IPayOptions } from "./types/interfaces"
17
- import { DefaultChain, SolanaDefaultChain } from "./types/defaultChain"
18
-
19
- /* LICENSE
20
-
21
- © 2023 by KyneSys Labs, licensed under CC BY-NC-ND 4.0
22
-
23
- Full license text: https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode
24
- Human readable license: https://creativecommons.org/licenses/by-nc-nd/4.0/
25
-
26
- KyneSys Labs: https://www.kynesys.xyz/
27
-
28
- */
29
-
30
- // LINK https://docs.solana.com/developing/clients/javascript-api
31
-
32
- // INTERFACES (TO BE MOVED)
33
- interface SignTxOptions {
34
- /**
35
- * The private key to sign the transaction with, instead of the connected wallet.
36
- */
37
- privateKey?: string
38
- /**
39
- * The address of your nonce account for signing with durable nonces.
40
- */
41
- nonceAccountAddress?: string
42
-
43
- /**
44
- * The secret key of the nonce account authority, for signing the tx.
45
- *
46
- * Defaults to the connected wallet's secret key.
47
- */
48
- nonceAccountAuthority?: string
49
- }
50
-
51
- export class SOLANA extends DefaultChain implements SolanaDefaultChain {
52
- private static instance: SOLANA
53
-
54
- declare wallet: Keypair
55
- declare provider: Connection
56
-
57
- constructor(rpc_url: string) {
58
- super(rpc_url)
59
- this.name = "solana"
60
- }
61
-
62
- async connect() {
63
- this.provider = new Connection(this.rpc_url)
64
-
65
- const version = await this.provider.getVersion()
66
- this.connected = Number.isInteger(version["feature-set"])
67
-
68
- return this.connected
69
- }
70
-
71
- async disconnect() {
72
- this.resetInstance()
73
- return true
74
- }
75
-
76
- // async createWallet() {}
77
-
78
- // ANCHOR Public methods
79
- async connectWallet(
80
- privateKey: string,
81
- options?: {
82
- /**
83
- * If the private key is in base58 format
84
- */
85
- base58: boolean
86
- },
87
- ) {
88
- let privateKeyBuffer: Uint8Array
89
-
90
- if (options && options.base58) {
91
- privateKeyBuffer = bs58.decode(privateKey)
92
- } else {
93
- const pk = privateKey.split(",").map(x => parseInt(x))
94
- privateKeyBuffer = Buffer.from(pk)
95
- }
96
-
97
- this.wallet = Keypair.fromSecretKey(privateKeyBuffer)
98
- return this.wallet
99
- }
100
-
101
- async getBalance(address: string) {
102
- const publicKey = new PublicKey(address)
103
- const balance = await this.provider.getBalance(publicKey)
104
- return balance.toString()
105
- }
106
-
107
- // async pay(to: string, amount: string): Promise<any> {
108
- // required(this.wallet, 'Wallet not connected')
109
- // // TODO
110
- // return null
111
- // }
112
-
113
- async info(): Promise<string> {
114
- let info = ""
115
- // TODO
116
- return info
117
- }
118
-
119
- // INFO Returning an empty raw transaction skeleton
120
- // async createRawTransaction(): Promise<Transaction> {
121
-
122
- // }
123
-
124
- // INFO Placeholder compatibility function that is here only for the interface
125
- override async signTransaction(tx: Transaction, options?: SignTxOptions) {
126
- required(this.wallet, "Wallet not connected")
127
- // LINK https://docs.shyft.to/tutorials/how-to-sign-transactions-on-solana
128
- // NOTE Due to the above, the transaction is signed and sent at the same time.
129
- // tx.addSignature()
130
- const txs = await this.signTransactions([tx], options)
131
- return txs[0]
132
- }
133
-
134
- async readNonce(address: string) {
135
- console.log("reading nonce account: ", address)
136
- const pubkey = new PublicKey(address)
137
- const accountInfo = await this.provider.getAccountInfo(pubkey)
138
- console.log("accountInfo: ", accountInfo)
139
-
140
- if (accountInfo) {
141
- return NonceAccount.fromAccountData(accountInfo?.data)
142
- }
143
-
144
- return null
145
- }
146
-
147
- async createNonceAccount() {
148
- required(this.wallet, "Wallet not connected")
149
-
150
- let tx = new Transaction()
151
- const nonceAccount = Keypair.generate()
152
-
153
- const create_acc_ix = SystemProgram.createAccount({
154
- fromPubkey: this.wallet.publicKey,
155
- newAccountPubkey: nonceAccount.publicKey,
156
- lamports: await this.provider.getMinimumBalanceForRentExemption(
157
- NONCE_ACCOUNT_LENGTH,
158
- ),
159
- space: NONCE_ACCOUNT_LENGTH,
160
- programId: SystemProgram.programId,
161
- })
162
-
163
- const init_nonce_ix = SystemProgram.nonceInitialize({
164
- noncePubkey: nonceAccount.publicKey,
165
- authorizedPubkey: this.wallet.publicKey,
166
- })
167
-
168
- tx.add(create_acc_ix, init_nonce_ix)
169
-
170
- const txhash = this.provider.sendTransaction(tx, [
171
- this.wallet,
172
- nonceAccount,
173
- ])
174
- console.log("txhash: ", txhash)
175
-
176
- return nonceAccount.publicKey.toBase58()
177
- }
178
-
179
- async signTransactions(
180
- transactions: Transaction[],
181
- options?: SignTxOptions,
182
- ) {
183
- required(this.wallet, "Wallet not connected")
184
-
185
- let nonceAccount: NonceAccount | null = null
186
- let advanceNonceIx: TransactionInstruction | null = null
187
- let nonceAuthority: Keypair = this.wallet
188
-
189
- const nonceAccAvailable =
190
- options && options.nonceAccountAddress ? true : false
191
-
192
- // if we have the nonce authority, overwrite.
193
- if (nonceAccAvailable && options!.nonceAccountAuthority) {
194
- nonceAuthority = Keypair.fromSecretKey(
195
- bs58.decode(options!.nonceAccountAuthority),
196
- )
197
- }
198
-
199
- // if we have the nonce address, create a nonce advance instruction
200
- if (nonceAccAvailable) {
201
- advanceNonceIx = SystemProgram.nonceAdvance({
202
- authorizedPubkey: nonceAuthority.publicKey,
203
- noncePubkey: new PublicKey(options!.nonceAccountAddress!),
204
- })
205
-
206
- nonceAccount = await this.readNonce(options!.nonceAccountAddress!)
207
- }
208
-
209
- // if advance instruction is not null
210
- // ie. we have the nonce address,
211
- // insert the advance nonce ix at instructions index 0
212
- // on each transaction
213
- if (advanceNonceIx && nonceAccount) {
214
- transactions.forEach(tx => {
215
- tx.instructions.splice(0, 0, advanceNonceIx!)
216
-
217
- // update recent block hash to use the current nonce
218
- tx.recentBlockhash = nonceAccount!.nonce
219
- nonceAccount?.nonce
220
- // TODO: FIND OUT WHAT HAPPENS WHEN MULTIPLE TX HAVE THE SAME DURABLE NONCE
221
- })
222
- }
223
-
224
- const signers = new Set([this.wallet, nonceAuthority])
225
- console.log("signers length: ", signers.size)
226
- console.log("signers: ", signers)
227
-
228
- transactions.forEach(async tx => {
229
- tx.sign(...signers)
230
- })
231
-
232
- return transactions
233
- }
234
-
235
- override getAddress() {
236
- required(this.wallet, "Wallet not connected")
237
-
238
- return this.wallet.publicKey.toBase58()
239
- }
240
-
241
- getEmptyTransaction() {
242
- // const recentBlockhash = await this.provider.getLatestBlockhash()
243
- const options = <TransactionNonceCtor>{
244
- feePayer: this.wallet.publicKey,
245
- }
246
-
247
- let empty_tx = new Transaction(options)
248
- // empty_tx.recentBlockhash = recentBlockhash.blockhash
249
- // empty_tx.lastValidBlockHeight = recentBlockhash.lastValidBlockHeight
250
-
251
- return empty_tx
252
- }
253
-
254
- override async preparePay(
255
- receiver: string,
256
- amount: string,
257
- options?: SignTxOptions,
258
- ) {
259
- const tx = await this.preparePays(
260
- [{ address: receiver, amount }],
261
- options,
262
- )
263
- return tx[0]
264
- }
265
-
266
- override async preparePays(
267
- payments: IPayOptions[],
268
- options?: SignTxOptions,
269
- ) {
270
- const recentBlockhash = await this.provider.getLatestBlockhash()
271
-
272
- const transactions = payments.map(payment => {
273
- const tx = this.getEmptyTransaction()
274
- tx.recentBlockhash = recentBlockhash.blockhash
275
- tx.lastValidBlockHeight = recentBlockhash.lastValidBlockHeight
276
-
277
- const transferIx = SystemProgram.transfer({
278
- fromPubkey: this.wallet.publicKey,
279
- toPubkey: new PublicKey(payment.address),
280
- lamports:
281
- parseFloat(payment.amount as string) * LAMPORTS_PER_SOL,
282
- })
283
-
284
- tx.add(transferIx)
285
- return tx
286
- })
287
-
288
- return this.signTransactions(transactions, options)
289
- }
290
-
291
- override async prepareTransfer(
292
- receiver: string,
293
- amount: string,
294
- options: {},
295
- ) {
296
- return await this.preparePay(receiver, amount, options)
297
- }
298
-
299
- override async prepareTransfers(transfers: IPayOptions[], options: {}) {
300
- return await this.preparePays(transfers, options)
301
- }
302
-
303
- // TODO: move sendTransaction to localsdk
304
- // INFO Sending a transfer transaction on Solana network
305
- // sendTransaction({ to, amount }) {
306
- // required(this.wallet, 'Wallet not connected')
307
- // let tx = new Transaction()
308
- // tx.add(
309
- // SystemProgram.transfer({
310
- // fromPubkey: this.wallet.publicKey,
311
- // toPubkey: to,
312
- // lamports: amount * LAMPORTS_PER_SOL,
313
- // })
314
- // )
315
- // let result = sendAndConfirmTransaction(this.provider, tx, [
316
- // this.wallet,
317
- // ])
318
- // return result
319
- // }
320
-
321
- // ANCHOR Static singleton methods
322
-
323
- static getInstance(): SOLANA | boolean {
324
- if (!SOLANA.instance) {
325
- return false
326
- }
327
- return SOLANA.instance
328
- }
329
-
330
- static createInstance(rpc_url: string): SOLANA {
331
- if (!SOLANA.instance) {
332
- SOLANA.instance = new SOLANA(rpc_url)
333
- }
334
- return SOLANA.instance
335
- }
336
- }