@alephium/web3 0.2.0-rc.3 → 0.2.0-rc.31

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 (125) hide show
  1. package/.eslintignore +2 -2
  2. package/README.md +2 -135
  3. package/dist/alephium-web3.min.js +1 -1
  4. package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
  5. package/dist/alephium-web3.min.js.map +1 -1
  6. package/dist/src/api/api-alephium.d.ts +117 -17
  7. package/dist/src/api/api-alephium.js +145 -79
  8. package/dist/src/api/api-explorer.d.ts +163 -48
  9. package/dist/src/api/api-explorer.js +157 -34
  10. package/dist/src/api/index.d.ts +14 -2
  11. package/dist/src/api/index.js +46 -4
  12. package/dist/src/api/types.d.ts +23 -0
  13. package/dist/src/api/types.js +240 -0
  14. package/dist/src/api/utils.d.ts +6 -0
  15. package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
  16. package/dist/src/contract/contract.d.ts +110 -73
  17. package/dist/src/contract/contract.js +395 -451
  18. package/dist/src/contract/events.d.ts +4 -4
  19. package/dist/src/contract/events.js +2 -1
  20. package/dist/src/contract/index.js +5 -1
  21. package/dist/src/contract/ralph.d.ts +5 -4
  22. package/dist/src/contract/ralph.js +27 -1
  23. package/dist/src/global.d.ts +4 -0
  24. package/dist/{scripts/stop-devnet.js → src/global.js} +17 -11
  25. package/dist/src/index.d.ts +2 -0
  26. package/dist/src/index.js +23 -1
  27. package/dist/src/signer/index.d.ts +0 -1
  28. package/dist/src/signer/index.js +5 -2
  29. package/dist/src/signer/signer.d.ts +35 -28
  30. package/dist/src/signer/signer.js +79 -47
  31. package/dist/src/transaction/index.d.ts +0 -1
  32. package/dist/src/transaction/index.js +5 -2
  33. package/dist/src/transaction/status.d.ts +2 -1
  34. package/dist/src/transaction/status.js +2 -1
  35. package/dist/src/utils/bs58.d.ts +1 -0
  36. package/dist/src/utils/bs58.js +13 -1
  37. package/dist/src/utils/index.d.ts +0 -1
  38. package/dist/src/utils/index.js +5 -2
  39. package/dist/src/utils/subscription.d.ts +0 -3
  40. package/dist/src/utils/subscription.js +0 -1
  41. package/dist/src/utils/utils.d.ts +4 -9
  42. package/dist/src/utils/utils.js +20 -24
  43. package/jest-config.json +11 -0
  44. package/package.json +11 -47
  45. package/src/api/api-alephium.ts +169 -25
  46. package/src/api/api-explorer.ts +234 -51
  47. package/src/api/index.ts +53 -3
  48. package/src/api/types.ts +233 -0
  49. package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
  50. package/src/contract/contract.ts +582 -550
  51. package/src/contract/events.ts +6 -5
  52. package/src/contract/ralph.ts +29 -4
  53. package/src/{transaction/sign-verify.ts → global.ts} +14 -15
  54. package/src/index.ts +7 -0
  55. package/src/signer/index.ts +0 -1
  56. package/src/signer/signer.ts +122 -85
  57. package/src/transaction/index.ts +0 -1
  58. package/src/transaction/status.ts +5 -2
  59. package/src/utils/bs58.ts +11 -0
  60. package/src/utils/index.ts +0 -1
  61. package/src/utils/subscription.ts +0 -4
  62. package/src/utils/utils.ts +11 -19
  63. package/.eslintrc.json +0 -21
  64. package/LICENSE +0 -165
  65. package/contracts/add/add.ral +0 -16
  66. package/contracts/greeter/greeter.ral +0 -7
  67. package/contracts/greeter/greeter_interface.ral +0 -3
  68. package/contracts/greeter_main.ral +0 -9
  69. package/contracts/main.ral +0 -6
  70. package/contracts/sub/sub.ral +0 -9
  71. package/contracts/test/metadata.ral +0 -17
  72. package/contracts/test/warnings.ral +0 -5
  73. package/dev/user.conf +0 -29
  74. package/dist/scripts/create-project.d.ts +0 -2
  75. package/dist/scripts/create-project.js +0 -125
  76. package/dist/scripts/rename-gitignore.d.ts +0 -1
  77. package/dist/scripts/start-devnet.d.ts +0 -1
  78. package/dist/scripts/start-devnet.js +0 -131
  79. package/dist/scripts/stop-devnet.d.ts +0 -1
  80. package/dist/src/signer/node-wallet.d.ts +0 -13
  81. package/dist/src/signer/node-wallet.js +0 -60
  82. package/dist/src/test/index.d.ts +0 -7
  83. package/dist/src/test/index.js +0 -41
  84. package/dist/src/test/privatekey-wallet.d.ts +0 -12
  85. package/dist/src/test/privatekey-wallet.js +0 -68
  86. package/dist/src/transaction/sign-verify.d.ts +0 -2
  87. package/dist/src/transaction/sign-verify.js +0 -58
  88. package/dist/src/utils/password-crypto.d.ts +0 -2
  89. package/dist/src/utils/password-crypto.js +0 -69
  90. package/gitignore +0 -10
  91. package/scripts/create-project.ts +0 -137
  92. package/scripts/start-devnet.js +0 -141
  93. package/scripts/stop-devnet.js +0 -32
  94. package/src/contract/ralph.test.ts +0 -178
  95. package/src/fixtures/address.json +0 -36
  96. package/src/fixtures/balance.json +0 -9
  97. package/src/fixtures/self-clique.json +0 -19
  98. package/src/fixtures/transaction.json +0 -13
  99. package/src/fixtures/transactions.json +0 -179
  100. package/src/signer/fixtures/genesis.json +0 -26
  101. package/src/signer/fixtures/wallets.json +0 -26
  102. package/src/signer/node-wallet.ts +0 -74
  103. package/src/test/index.ts +0 -32
  104. package/src/test/privatekey-wallet.ts +0 -58
  105. package/src/transaction/sign-verify.test.ts +0 -50
  106. package/src/utils/address.test.ts +0 -47
  107. package/src/utils/djb2.test.ts +0 -35
  108. package/src/utils/password-crypto.test.ts +0 -27
  109. package/src/utils/password-crypto.ts +0 -77
  110. package/src/utils/utils.test.ts +0 -161
  111. package/templates/base/README.md +0 -34
  112. package/templates/base/package.json +0 -35
  113. package/templates/base/src/greeter.ts +0 -41
  114. package/templates/base/tsconfig.json +0 -19
  115. package/templates/react/README.md +0 -34
  116. package/templates/react/config-overrides.js +0 -18
  117. package/templates/react/package.json +0 -66
  118. package/templates/react/src/App.tsx +0 -42
  119. package/templates/react/src/artifacts/greeter.ral.json +0 -26
  120. package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
  121. package/templates/shared/.eslintrc.json +0 -12
  122. package/templates/shared/scripts/header.js +0 -0
  123. package/test/contract.test.ts +0 -197
  124. package/test/events.test.ts +0 -138
  125. package/test/transaction.test.ts +0 -72
@@ -16,14 +16,15 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import { ContractEvent } from '../api/api-alephium'
19
+ import { web3 } from '..'
20
+ import { node } from '../api'
20
21
  import { Subscription, SubscribeOptions } from '../utils'
21
22
 
22
- export class EventSubscription extends Subscription<ContractEvent> {
23
+ export class EventSubscription extends Subscription<node.ContractEvent> {
23
24
  readonly contractAddress: string
24
25
  private fromCount: number
25
26
 
26
- constructor(options: SubscribeOptions<ContractEvent>, contractAddress: string, fromCount?: number) {
27
+ constructor(options: SubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number) {
27
28
  super(options)
28
29
  this.contractAddress = contractAddress
29
30
  this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount
@@ -44,7 +45,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
44
45
 
45
46
  override async polling(): Promise<void> {
46
47
  try {
47
- const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
48
+ const events = await web3.getCurrentNodeProvider().events.getEventsContractContractaddress(this.contractAddress, {
48
49
  start: this.fromCount
49
50
  })
50
51
  if (this.cancelled) {
@@ -67,7 +68,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
67
68
  }
68
69
 
69
70
  export function subscribeToEvents(
70
- options: SubscribeOptions<ContractEvent>,
71
+ options: SubscribeOptions<node.ContractEvent>,
71
72
  contractAddress: string,
72
73
  fromCount?: number
73
74
  ): EventSubscription {
@@ -17,9 +17,9 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import { Buffer } from 'buffer/'
20
+ import { Val } from '../api'
20
21
  import { bs58, isHexString } from '../utils'
21
- import { node } from '../api'
22
- import { Fields, Val } from './contract'
22
+ import { Fields, FieldsSig } from './contract'
23
23
 
24
24
  const bigIntZero = BigInt(0)
25
25
 
@@ -236,7 +236,7 @@ export function encodeScriptField(tpe: string, value: Val): Uint8Array {
236
236
 
237
237
  const scriptFieldRegex = /\{([0-9]*)\}/g
238
238
 
239
- export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: node.FieldsSig): string {
239
+ export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig): string {
240
240
  return bytecodeTemplate.replace(scriptFieldRegex, (_, fieldIndex: string) => {
241
241
  const fieldName = fieldsSig.names[`${fieldIndex}`]
242
242
  const fieldType = fieldsSig.types[`${fieldIndex}`]
@@ -249,7 +249,7 @@ export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fi
249
249
  })
250
250
  }
251
251
 
252
- export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: node.FieldsSig): string {
252
+ export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: FieldsSig): string {
253
253
  const fieldsEncoded = fieldsSig.names.flatMap((fieldName, fieldIndex) => {
254
254
  const fieldType = fieldsSig.types[`${fieldIndex}`]
255
255
  if (fieldName in fields) {
@@ -343,6 +343,31 @@ function invalidVal(tpe: string, value: Val): Error {
343
343
  return Error(`Invalid API value ${value} for type ${tpe}`)
344
344
  }
345
345
 
346
+ export function buildDebugBytecode(bytecode: string, bytecodePatch: string): string {
347
+ if (bytecodePatch === '') {
348
+ return bytecode
349
+ }
350
+
351
+ const pattern = /[=+-][0-9a-f]*/g
352
+ let result = ''
353
+ let index = 0
354
+ for (const parts of bytecodePatch.matchAll(pattern)) {
355
+ const part = parts[0]
356
+ const diffType = part[0]
357
+ if (diffType === '=') {
358
+ const length = parseInt(part.substring(1))
359
+ result = result + bytecode.slice(index, index + length)
360
+ index = index + length
361
+ } else if (diffType === '+') {
362
+ result = result + part.substring(1)
363
+ } else {
364
+ const length = parseInt(part.substring(1))
365
+ index = index + length
366
+ }
367
+ }
368
+ return result
369
+ }
370
+
346
371
  // export function buildContractByteCode(
347
372
  // compiled: node.TemplateContractByteCode,
348
373
  // templateVariables: TemplateVariables
@@ -16,24 +16,23 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import { ec as EC } from 'elliptic'
19
+ import { NodeProvider } from './api'
20
20
 
21
- import * as utils from '../utils'
21
+ let _currentNodeProvider: NodeProvider | undefined = undefined
22
22
 
23
- const ec = new EC('secp256k1')
24
-
25
- export function transactionSign(txHash: string, privateKey: string): string {
26
- const keyPair = ec.keyFromPrivate(privateKey)
27
- const signature = keyPair.sign(txHash)
28
-
29
- return utils.signatureEncode(signature)
23
+ export function setCurrentNodeProvider(provider: NodeProvider): void
24
+ export function setCurrentNodeProvider(baseUrl: string, apiKey?: string): void
25
+ export function setCurrentNodeProvider(provider: NodeProvider | string, apiKey?: string): void {
26
+ if (typeof provider == 'string') {
27
+ _currentNodeProvider = new NodeProvider(provider, apiKey)
28
+ } else {
29
+ _currentNodeProvider = provider
30
+ }
30
31
  }
31
32
 
32
- export function transactionVerifySignature(txHash: string, publicKey: string, signature: string): boolean {
33
- try {
34
- const key = ec.keyFromPublic(publicKey, 'hex')
35
- return key.verify(txHash, utils.signatureDecode(ec, signature))
36
- } catch (error) {
37
- return false
33
+ export function getCurrentNodeProvider(): NodeProvider {
34
+ if (typeof _currentNodeProvider === 'undefined') {
35
+ throw Error('No node provider is set.')
38
36
  }
37
+ return _currentNodeProvider
39
38
  }
package/src/index.ts CHANGED
@@ -16,6 +16,10 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
+ BigInt.prototype['toJSON'] = function () {
20
+ return this.toString()
21
+ }
22
+
19
23
  export * from './api'
20
24
  export * from './contract'
21
25
  export * from './signer'
@@ -23,3 +27,6 @@ export * from './utils'
23
27
  export * from './transaction'
24
28
 
25
29
  export * from './constants'
30
+ export * as web3 from './global'
31
+
32
+ export * as utils from './utils'
@@ -17,4 +17,3 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  export * from './signer'
20
- export * from './node-wallet'
@@ -17,12 +17,22 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import { ec as EC } from 'elliptic'
20
- import { NodeProvider } from '../api'
20
+ import {
21
+ fromApiNumber256,
22
+ fromApiTokens,
23
+ NodeProvider,
24
+ Number256,
25
+ toApiNumber256,
26
+ toApiNumber256Optional,
27
+ toApiTokens,
28
+ Token
29
+ } from '../api'
21
30
  import { node } from '../api'
22
31
  import * as utils from '../utils'
23
32
  import { Eq, assertType } from '../utils'
24
33
  import blake from 'blakejs'
25
- import { Token } from '../api/api-alephium'
34
+
35
+ export type OutputRef = node.OutputRef
26
36
 
27
37
  const ec = new EC('secp256k1')
28
38
 
@@ -40,22 +50,20 @@ export interface Account {
40
50
  publicKey: string
41
51
  }
42
52
 
43
- export type SubmitTx = { submitTx?: boolean }
44
53
  export type SignerAddress = { signerAddress: string }
45
- type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress & SubmitTx
54
+ type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress
46
55
 
47
56
  export type GetAccountsParams = undefined
48
57
  export type GetAccountsResult = Account[]
49
58
 
50
59
  export interface SignTransferTxParams {
51
60
  signerAddress: string
52
- destinations: node.Destination[]
53
- utxos?: node.OutputRef[]
61
+ destinations: Destination[]
62
+ utxos?: OutputRef[]
54
63
  gasAmount?: number
55
- gasPrice?: string
56
- submitTx?: boolean
64
+ gasPrice?: Number256
57
65
  }
58
- assertType<Eq<SignTransferTxParams, TxBuildParams<node.BuildTransaction>>>()
66
+ assertType<Eq<keyof SignTransferTxParams, keyof TxBuildParams<node.BuildTransaction>>>()
59
67
  export interface SignTransferTxResult {
60
68
  fromGroup: number
61
69
  toGroup: number
@@ -68,14 +76,13 @@ assertType<Eq<SignTransferTxResult, SignResult>>()
68
76
  export interface SignDeployContractTxParams {
69
77
  signerAddress: string
70
78
  bytecode: string
71
- initialAttoAlphAmount?: string
79
+ initialAttoAlphAmount?: Number256
72
80
  initialTokenAmounts?: Token[]
73
- issueTokenAmount?: string
81
+ issueTokenAmount?: Number256
74
82
  gasAmount?: number
75
- gasPrice?: string
76
- submitTx?: boolean
83
+ gasPrice?: Number256
77
84
  }
78
- assertType<Eq<SignDeployContractTxParams, TxBuildParams<node.BuildDeployContractTx>>>()
85
+ assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
79
86
  export interface SignDeployContractTxResult {
80
87
  fromGroup: number
81
88
  toGroup: number
@@ -91,12 +98,11 @@ export interface SignExecuteScriptTxParams {
91
98
  signerAddress: string
92
99
  bytecode: string
93
100
  attoAlphAmount?: string
94
- tokens?: node.Token[]
101
+ tokens?: Token[]
95
102
  gasAmount?: number
96
103
  gasPrice?: string
97
- submitTx?: boolean
98
104
  }
99
- assertType<Eq<SignExecuteScriptTxParams, TxBuildParams<node.BuildExecuteScriptTx>>>()
105
+ assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
100
106
  export interface SignExecuteScriptTxResult {
101
107
  fromGroup: number
102
108
  toGroup: number
@@ -109,9 +115,8 @@ assertType<Eq<SignExecuteScriptTxResult, SignResult>>()
109
115
  export interface SignUnsignedTxParams {
110
116
  signerAddress: string
111
117
  unsignedTx: string
112
- submitTx?: boolean
113
118
  }
114
- assertType<Eq<SignUnsignedTxParams, { unsignedTx: string } & SubmitTx & SignerAddress>>()
119
+ assertType<Eq<SignUnsignedTxParams, { unsignedTx: string } & SignerAddress>>()
115
120
  export interface SignUnsignedTxResult {
116
121
  fromGroup: number
117
122
  toGroup: number
@@ -141,8 +146,8 @@ export interface SignMessageResult {
141
146
  }
142
147
  assertType<Eq<SignMessageResult, Pick<SignResult, 'signature'>>>()
143
148
 
144
- export interface SignerProvider {
145
- getAccounts(): Promise<Account[]>
149
+ export interface SignerProviderWithoutNodeProvider {
150
+ getSelectedAccount(): Promise<Account>
146
151
  signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>
147
152
  signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>
148
153
  signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>
@@ -151,117 +156,112 @@ export interface SignerProvider {
151
156
  signMessage(params: SignMessageParams): Promise<SignMessageResult>
152
157
  }
153
158
 
154
- export abstract class SignerWithNodeProvider implements SignerProvider {
155
- readonly provider: NodeProvider
156
- alwaysSubmitTx: boolean
159
+ export abstract class SignerProvider implements SignerProviderWithoutNodeProvider {
160
+ abstract get nodeProvider(): NodeProvider
161
+ abstract getSelectedAccount(): Promise<Account>
157
162
 
158
- abstract getAccounts(): Promise<Account[]>
159
-
160
- async getAccount(signerAddress: string): Promise<Account> {
161
- const accounts = await this.getAccounts()
162
- const account = accounts.find((a) => a.address === signerAddress)
163
- if (typeof account === 'undefined') {
164
- throw new Error('Unmatched signerAddress')
165
- } else {
166
- return account
167
- }
163
+ async submitTransaction(unsignedTx: string, signature: string): Promise<SubmissionResult> {
164
+ const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
165
+ return this.nodeProvider.transactions.postTransactionsSubmit(params)
168
166
  }
169
167
 
170
- constructor(provider: NodeProvider, alwaysSubmitTx: boolean) {
171
- this.provider = provider
172
- this.alwaysSubmitTx = alwaysSubmitTx
168
+ async signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SubmissionResult> {
169
+ const signResult = await this.signTransferTx(params)
170
+ return this.submitTransaction(signResult.unsignedTx, signResult.signature)
173
171
  }
174
-
175
- private async defaultSignerAddress(): Promise<string> {
176
- return (await this.getAccounts())[0].address
172
+ async signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SubmissionResult> {
173
+ const signResult = await this.signDeployContractTx(params)
174
+ return this.submitTransaction(signResult.unsignedTx, signResult.signature)
177
175
  }
178
-
179
- async submitTransaction(unsignedTx: string, txHash: string, signerAddress?: string): Promise<SubmissionResult> {
180
- const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress()
181
- const signature = await this.signRaw(address, txHash)
182
- const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
183
- return this.provider.transactions.postTransactionsSubmit(params)
176
+ async signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SubmissionResult> {
177
+ const signResult = await this.signExecuteScriptTx(params)
178
+ return this.submitTransaction(signResult.unsignedTx, signResult.signature)
184
179
  }
185
-
186
- private shouldSubmitTx(params: SubmitTx): boolean {
187
- return this.alwaysSubmitTx || (params.submitTx ? params.submitTx : true)
180
+ async signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SubmissionResult> {
181
+ const signResult = await this.signUnsignedTx(params)
182
+ return this.submitTransaction(signResult.unsignedTx, signResult.signature)
188
183
  }
189
184
 
190
185
  private async usePublicKey<T extends SignerAddress>(
191
186
  params: T
192
187
  ): Promise<Omit<T, 'signerAddress'> & { fromPublicKey: string }> {
193
188
  const { signerAddress, ...restParams } = params
194
- const allAccounts = await this.getAccounts()
195
- const signerAccount = allAccounts.find((account) => account.address === signerAddress)
196
- if (typeof signerAccount === 'undefined') {
197
- throw new Error('Unknown signer address')
189
+ const selectedAccount = await this.getSelectedAccount()
190
+ if (signerAddress !== selectedAccount.address) {
191
+ throw new Error('The signer address is not the selected address')
198
192
  } else {
199
- return { fromPublicKey: signerAccount.publicKey, ...restParams }
193
+ return { fromPublicKey: selectedAccount.publicKey, ...restParams }
200
194
  }
201
195
  }
202
196
 
203
197
  async signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult> {
204
198
  const response = await this.buildTransferTx(params)
205
- return this.handleSign({ signerAddress: params.signerAddress, ...response }, this.shouldSubmitTx(params))
199
+ return this.handleSign({ signerAddress: params.signerAddress, ...response })
206
200
  }
207
201
 
208
202
  async buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult> {
209
- return this.provider.transactions.postTransactionsBuild(await this.usePublicKey(params))
203
+ const data: node.BuildTransaction = {
204
+ ...(await this.usePublicKey(params)),
205
+ destinations: toApiDestinations(params.destinations),
206
+ gasPrice: toApiNumber256Optional(params.gasPrice)
207
+ }
208
+ return this.nodeProvider.transactions.postTransactionsBuild(data)
210
209
  }
211
210
 
212
211
  async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
213
212
  const response = await this.buildContractCreationTx(params)
214
- const result = await this.handleSign(
215
- { signerAddress: params.signerAddress, ...response },
216
- this.shouldSubmitTx(params)
217
- )
213
+ const result = await this.handleSign({ signerAddress: params.signerAddress, ...response })
218
214
  const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress))
219
215
  return { ...result, contractId: contractId, contractAddress: response.contractAddress }
220
216
  }
221
217
 
222
218
  async buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult> {
223
- return this.provider.contracts.postContractsUnsignedTxDeployContract(await this.usePublicKey(params))
219
+ const data: node.BuildDeployContractTx = {
220
+ ...(await this.usePublicKey(params)),
221
+ initialAttoAlphAmount: toApiNumber256Optional(params.initialAttoAlphAmount),
222
+ initialTokenAmounts: toApiTokens(params.initialTokenAmounts),
223
+ issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
224
+ gasPrice: toApiNumber256Optional(params.gasPrice)
225
+ }
226
+ return this.nodeProvider.contracts.postContractsUnsignedTxDeployContract(data)
224
227
  }
225
228
 
226
229
  async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
227
230
  const response = await this.buildScriptTx(params)
228
- return this.handleSign({ signerAddress: params.signerAddress, ...response }, this.shouldSubmitTx(params))
231
+ return this.handleSign({ signerAddress: params.signerAddress, ...response })
229
232
  }
230
233
 
231
234
  async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
232
- return this.provider.contracts.postContractsUnsignedTxExecuteScript(await this.usePublicKey(params))
235
+ const data: node.BuildExecuteScriptTx = {
236
+ ...(await this.usePublicKey(params)),
237
+ tokens: toApiTokens(params.tokens)
238
+ }
239
+ return this.nodeProvider.contracts.postContractsUnsignedTxExecuteScript(data)
233
240
  }
234
241
 
235
242
  // in general, wallet should show the decoded information to user for confirmation
236
243
  // please overwrite this function for real wallet
237
244
  async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
238
245
  const data = { unsignedTx: params.unsignedTx }
239
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx(data)
240
- return this.handleSign(
241
- {
242
- fromGroup: decoded.fromGroup,
243
- toGroup: decoded.toGroup,
244
- signerAddress: params.signerAddress,
245
- unsignedTx: params.unsignedTx,
246
- txId: decoded.unsignedTx.txId
247
- },
248
- params.submitTx ? params.submitTx : true // we don't consider `alwaysSubmitTx` as the tx might needs multiple signatures
249
- )
246
+ const decoded = await this.nodeProvider.transactions.postTransactionsDecodeUnsignedTx(data)
247
+ return this.handleSign({
248
+ fromGroup: decoded.fromGroup,
249
+ toGroup: decoded.toGroup,
250
+ signerAddress: params.signerAddress,
251
+ unsignedTx: params.unsignedTx,
252
+ txId: decoded.unsignedTx.txId
253
+ })
250
254
  }
251
255
 
252
- protected async handleSign(
253
- response: { fromGroup: number; toGroup: number; signerAddress: string; unsignedTx: string; txId: string },
254
- submitTx: boolean
255
- ): Promise<SignResult> {
256
+ protected async handleSign(response: {
257
+ fromGroup: number
258
+ toGroup: number
259
+ signerAddress: string
260
+ unsignedTx: string
261
+ txId: string
262
+ }): Promise<SignResult> {
256
263
  // sign the tx
257
264
  const signature = await this.signRaw(response.signerAddress, response.txId)
258
- // submit the tx if required
259
- if (submitTx) {
260
- await this.provider.transactions.postTransactionsSubmit({
261
- unsignedTx: response.unsignedTx,
262
- signature: signature
263
- })
264
- }
265
265
  // return the signature back to the provider
266
266
  return {
267
267
  fromGroup: response.fromGroup,
@@ -287,6 +287,22 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
287
287
  abstract signRaw(signerAddress: string, hexString: string): Promise<string>
288
288
  }
289
289
 
290
+ export abstract class SignerProviderWithMultipleAccounts extends SignerProvider {
291
+ abstract getAccounts(): Promise<Account[]>
292
+
293
+ async getAccount(signerAddress: string): Promise<Account> {
294
+ const accounts = await this.getAccounts()
295
+ const account = accounts.find((a) => a.address === signerAddress)
296
+ if (typeof account === 'undefined') {
297
+ throw new Error('Unmatched signerAddress')
298
+ } else {
299
+ return account
300
+ }
301
+ }
302
+
303
+ abstract setSelectedAccount(address: string): Promise<void>
304
+ }
305
+
290
306
  export interface SubmissionResult {
291
307
  txId: string
292
308
  fromGroup: number
@@ -311,3 +327,24 @@ export function verifySignedMessage(message: string, publicKey: string, signatur
311
327
  const messageHash = blake.blake2b(extendedMessage, undefined, 32)
312
328
  return verifyHexString(utils.binToHex(messageHash), publicKey, signature)
313
329
  }
330
+
331
+ export interface Destination {
332
+ address: string
333
+ attoAlphAmount: Number256
334
+ tokens?: Token[]
335
+ lockTime?: number
336
+ message?: string
337
+ }
338
+ assertType<Eq<keyof Destination, keyof node.Destination>>
339
+
340
+ export function toApiDestination(data: Destination): node.Destination {
341
+ return { ...data, attoAlphAmount: toApiNumber256(data.attoAlphAmount), tokens: toApiTokens(data.tokens) }
342
+ }
343
+
344
+ export function toApiDestinations(data: Destination[]): node.Destination[] {
345
+ return data.map(toApiDestination)
346
+ }
347
+
348
+ export function fromApiDestination(data: node.Destination): Destination {
349
+ return { ...data, attoAlphAmount: fromApiNumber256(data.attoAlphAmount), tokens: fromApiTokens(data.tokens) }
350
+ }
@@ -16,5 +16,4 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- export * from './sign-verify'
20
19
  export * from './status'
@@ -16,9 +16,12 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import { TxStatus } from '../api/api-alephium'
19
+ import { web3 } from '..'
20
+ import { node } from '../api'
20
21
  import { Subscription, SubscribeOptions } from '../utils'
21
22
 
23
+ export type TxStatus = node.TxStatus
24
+
22
25
  export class TxStatusSubscription extends Subscription<TxStatus> {
23
26
  readonly txId: string
24
27
  readonly fromGroup?: number
@@ -35,7 +38,7 @@ export class TxStatusSubscription extends Subscription<TxStatus> {
35
38
 
36
39
  override async polling(): Promise<void> {
37
40
  try {
38
- const txStatus = await this.provider.transactions.getTransactionsStatus({
41
+ const txStatus = await web3.getCurrentNodeProvider().transactions.getTransactionsStatus({
39
42
  txId: this.txId,
40
43
  fromGroup: this.fromGroup,
41
44
  toGroup: this.toGroup
package/src/utils/bs58.ts CHANGED
@@ -23,4 +23,15 @@ const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
23
23
 
24
24
  export const bs58 = basex(ALPHABET)
25
25
 
26
+ export function isBase58(s: string): boolean {
27
+ if (s === '' || s.trim() === '') {
28
+ return false
29
+ }
30
+ try {
31
+ return bs58.encode(bs58.decode(s)) === s
32
+ } catch (err) {
33
+ return false
34
+ }
35
+ }
36
+
26
37
  export default bs58
@@ -19,6 +19,5 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
19
19
  export * from './address'
20
20
  export * from './bs58'
21
21
  export * from './djb2'
22
- export * from './password-crypto'
23
22
  export * from './utils'
24
23
  export * from './subscription'
@@ -17,20 +17,17 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import EventEmitter from 'eventemitter3'
20
- import { NodeProvider } from '../api'
21
20
 
22
21
  type MessageCallback<Message> = (message: Message) => Promise<void>
23
22
  type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>
24
23
 
25
24
  export interface SubscribeOptions<Message> {
26
- provider: NodeProvider
27
25
  pollingInterval: number
28
26
  messageCallback: MessageCallback<Message>
29
27
  errorCallback: ErrorCallback<Message>
30
28
  }
31
29
 
32
30
  export abstract class Subscription<Message> {
33
- provider: NodeProvider
34
31
  pollingInterval: number
35
32
 
36
33
  protected messageCallback: MessageCallback<Message>
@@ -40,7 +37,6 @@ export abstract class Subscription<Message> {
40
37
  protected cancelled: boolean
41
38
 
42
39
  constructor(options: SubscribeOptions<Message>) {
43
- this.provider = options.provider
44
40
  this.pollingInterval = options.pollingInterval
45
41
  this.messageCallback = options.messageCallback
46
42
  this.errorCallback = options.errorCallback