@alephium/web3 0.2.0-rc.1 → 0.2.0-rc.12

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 (99) hide show
  1. package/.eslintignore +2 -2
  2. package/README.md +2 -135
  3. package/contracts/greeter/greeter.ral +2 -2
  4. package/contracts/greeter/greeter_interface.ral +1 -0
  5. package/contracts/greeter_main.ral +1 -3
  6. package/dist/alephium-web3.min.js +1 -1
  7. package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
  8. package/dist/alephium-web3.min.js.map +1 -1
  9. package/dist/scripts/create-project.js +3 -2
  10. package/dist/src/api/api-alephium.d.ts +44 -11
  11. package/dist/src/api/api-alephium.js +81 -74
  12. package/dist/src/api/api-explorer.d.ts +16 -16
  13. package/dist/src/api/api-explorer.js +24 -33
  14. package/dist/src/api/index.d.ts +3 -2
  15. package/dist/src/api/index.js +22 -3
  16. package/dist/src/api/types.d.ts +23 -0
  17. package/dist/src/api/types.js +240 -0
  18. package/dist/src/api/utils.d.ts +6 -0
  19. package/{src/utils/djb2.test.ts → dist/src/api/utils.js} +11 -17
  20. package/dist/src/contract/contract.d.ts +104 -64
  21. package/dist/src/contract/contract.js +370 -426
  22. package/dist/src/contract/events.d.ts +4 -4
  23. package/dist/src/contract/index.js +5 -1
  24. package/dist/src/contract/ralph.d.ts +4 -4
  25. package/dist/src/global.d.ts +4 -0
  26. package/dist/src/global.js +38 -0
  27. package/dist/src/index.d.ts +2 -0
  28. package/dist/src/index.js +20 -1
  29. package/dist/src/signer/index.d.ts +0 -1
  30. package/dist/src/signer/index.js +5 -2
  31. package/dist/src/signer/signer.d.ts +21 -11
  32. package/dist/src/signer/signer.js +46 -9
  33. package/dist/src/transaction/index.d.ts +0 -1
  34. package/dist/src/transaction/index.js +5 -2
  35. package/dist/src/transaction/status.d.ts +2 -1
  36. package/dist/src/utils/index.d.ts +0 -1
  37. package/dist/src/utils/index.js +5 -2
  38. package/dist/src/utils/subscription.d.ts +0 -1
  39. package/dist/src/utils/subscription.js +2 -1
  40. package/dist/src/utils/utils.d.ts +5 -11
  41. package/dist/src/utils/utils.js +17 -25
  42. package/jest-config.json +11 -0
  43. package/package.json +8 -36
  44. package/scripts/create-project.ts +3 -2
  45. package/src/api/api-alephium.ts +54 -16
  46. package/src/api/api-explorer.ts +1 -11
  47. package/src/api/index.ts +14 -3
  48. package/src/api/types.ts +233 -0
  49. package/src/{utils/password-crypto.test.ts → api/utils.ts} +7 -9
  50. package/src/contract/contract.ts +537 -526
  51. package/src/contract/events.ts +6 -6
  52. package/src/contract/ralph.ts +4 -4
  53. package/src/{transaction/sign-verify.ts → global.ts} +14 -15
  54. package/src/index.ts +3 -0
  55. package/src/signer/index.ts +0 -1
  56. package/src/signer/signer.ts +70 -19
  57. package/src/transaction/index.ts +0 -1
  58. package/src/transaction/status.ts +4 -2
  59. package/src/utils/index.ts +0 -1
  60. package/src/utils/subscription.ts +3 -3
  61. package/src/utils/utils.ts +10 -20
  62. package/templates/base/package.json +2 -2
  63. package/templates/base/src/greeter.ts +10 -9
  64. package/templates/react/package.json +2 -2
  65. package/templates/react/src/App.tsx +2 -2
  66. package/.eslintrc.json +0 -21
  67. package/LICENSE +0 -165
  68. package/contracts/add/add.ral +0 -16
  69. package/contracts/main.ral +0 -6
  70. package/contracts/sub/sub.ral +0 -9
  71. package/dist/src/signer/node-wallet.d.ts +0 -13
  72. package/dist/src/signer/node-wallet.js +0 -60
  73. package/dist/src/test/index.d.ts +0 -7
  74. package/dist/src/test/index.js +0 -41
  75. package/dist/src/test/privatekey-wallet.d.ts +0 -12
  76. package/dist/src/test/privatekey-wallet.js +0 -68
  77. package/dist/src/transaction/sign-verify.d.ts +0 -2
  78. package/dist/src/transaction/sign-verify.js +0 -58
  79. package/dist/src/utils/password-crypto.d.ts +0 -2
  80. package/dist/src/utils/password-crypto.js +0 -69
  81. package/gitignore +0 -10
  82. package/src/contract/ralph.test.ts +0 -178
  83. package/src/fixtures/address.json +0 -36
  84. package/src/fixtures/balance.json +0 -9
  85. package/src/fixtures/self-clique.json +0 -19
  86. package/src/fixtures/transaction.json +0 -13
  87. package/src/fixtures/transactions.json +0 -179
  88. package/src/signer/fixtures/genesis.json +0 -26
  89. package/src/signer/fixtures/wallets.json +0 -26
  90. package/src/signer/node-wallet.ts +0 -74
  91. package/src/test/index.ts +0 -32
  92. package/src/test/privatekey-wallet.ts +0 -58
  93. package/src/transaction/sign-verify.test.ts +0 -50
  94. package/src/utils/address.test.ts +0 -47
  95. package/src/utils/password-crypto.ts +0 -77
  96. package/src/utils/utils.test.ts +0 -161
  97. package/test/contract.test.ts +0 -178
  98. package/test/events.test.ts +0 -138
  99. package/test/transaction.test.ts +0 -72
@@ -16,14 +16,14 @@ 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 { node } from '../api'
20
20
  import { Subscription, SubscribeOptions } from '../utils'
21
21
 
22
- export class EventSubscription extends Subscription<ContractEvent> {
22
+ export class EventSubscription extends Subscription<node.ContractEvent> {
23
23
  readonly contractAddress: string
24
24
  private fromCount: number
25
25
 
26
- constructor(options: SubscribeOptions<ContractEvent>, contractAddress: string, fromCount?: number) {
26
+ constructor(options: SubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number) {
27
27
  super(options)
28
28
  this.contractAddress = contractAddress
29
29
  this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount
@@ -31,7 +31,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
31
31
  this.startPolling()
32
32
  }
33
33
 
34
- override startPolling() {
34
+ override startPolling(): void {
35
35
  this.eventEmitter.on('tick', async () => {
36
36
  await this.polling()
37
37
  })
@@ -42,7 +42,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
42
42
  return this.fromCount
43
43
  }
44
44
 
45
- override async polling() {
45
+ override async polling(): Promise<void> {
46
46
  try {
47
47
  const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
48
48
  start: this.fromCount
@@ -67,7 +67,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
67
67
  }
68
68
 
69
69
  export function subscribeToEvents(
70
- options: SubscribeOptions<ContractEvent>,
70
+ options: SubscribeOptions<node.ContractEvent>,
71
71
  contractAddress: string,
72
72
  fromCount?: number
73
73
  ): 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) {
@@ -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
@@ -23,3 +23,6 @@ export * from './utils'
23
23
  export * from './transaction'
24
24
 
25
25
  export * from './constants'
26
+ export * as web3 from './global'
27
+
28
+ 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,23 @@ 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
+ import { getCurrentNodeProvider } from '../global'
35
+
36
+ export type OutputRef = node.OutputRef
26
37
 
27
38
  const ec = new EC('secp256k1')
28
39
 
@@ -49,13 +60,13 @@ export type GetAccountsResult = Account[]
49
60
 
50
61
  export interface SignTransferTxParams {
51
62
  signerAddress: string
52
- destinations: node.Destination[]
53
- utxos?: node.OutputRef[]
63
+ destinations: Destination[]
64
+ utxos?: OutputRef[]
54
65
  gasAmount?: number
55
- gasPrice?: string
66
+ gasPrice?: Number256
56
67
  submitTx?: boolean
57
68
  }
58
- assertType<Eq<SignTransferTxParams, TxBuildParams<node.BuildTransaction>>>()
69
+ assertType<Eq<keyof SignTransferTxParams, keyof TxBuildParams<node.BuildTransaction>>>()
59
70
  export interface SignTransferTxResult {
60
71
  fromGroup: number
61
72
  toGroup: number
@@ -68,14 +79,14 @@ assertType<Eq<SignTransferTxResult, SignResult>>()
68
79
  export interface SignDeployContractTxParams {
69
80
  signerAddress: string
70
81
  bytecode: string
71
- initialAttoAlphAmount?: string
82
+ initialAttoAlphAmount?: Number256
72
83
  initialTokenAmounts?: Token[]
73
- issueTokenAmount?: string
84
+ issueTokenAmount?: Number256
74
85
  gasAmount?: number
75
- gasPrice?: string
86
+ gasPrice?: Number256
76
87
  submitTx?: boolean
77
88
  }
78
- assertType<Eq<SignDeployContractTxParams, TxBuildParams<node.BuildDeployContractTx>>>()
89
+ assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
79
90
  export interface SignDeployContractTxResult {
80
91
  fromGroup: number
81
92
  toGroup: number
@@ -91,12 +102,12 @@ export interface SignExecuteScriptTxParams {
91
102
  signerAddress: string
92
103
  bytecode: string
93
104
  attoAlphAmount?: string
94
- tokens?: node.Token[]
105
+ tokens?: Token[]
95
106
  gasAmount?: number
96
107
  gasPrice?: string
97
108
  submitTx?: boolean
98
109
  }
99
- assertType<Eq<SignExecuteScriptTxParams, TxBuildParams<node.BuildExecuteScriptTx>>>()
110
+ assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
100
111
  export interface SignExecuteScriptTxResult {
101
112
  fromGroup: number
102
113
  toGroup: number
@@ -167,8 +178,8 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
167
178
  }
168
179
  }
169
180
 
170
- constructor(provider: NodeProvider, alwaysSubmitTx: boolean) {
171
- this.provider = provider
181
+ constructor(alwaysSubmitTx: boolean) {
182
+ this.provider = getCurrentNodeProvider()
172
183
  this.alwaysSubmitTx = alwaysSubmitTx
173
184
  }
174
185
 
@@ -176,9 +187,12 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
176
187
  return (await this.getAccounts())[0].address
177
188
  }
178
189
 
179
- async submitTransaction(unsignedTx: string, txHash: string, signerAddress?: string): Promise<SubmissionResult> {
190
+ async submitTransaction(unsignedTx: string, signerAddress?: string): Promise<SubmissionResult> {
191
+ const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx })
192
+ const txId = decoded.unsignedTx.txId
193
+
180
194
  const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress()
181
- const signature = await this.signRaw(address, txHash)
195
+ const signature = await this.signRaw(address, txId)
182
196
  const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
183
197
  return this.provider.transactions.postTransactionsSubmit(params)
184
198
  }
@@ -206,7 +220,12 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
206
220
  }
207
221
 
208
222
  async buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult> {
209
- return this.provider.transactions.postTransactionsBuild(await this.usePublicKey(params))
223
+ const data: node.BuildTransaction = {
224
+ ...(await this.usePublicKey(params)),
225
+ destinations: toApiDestinations(params.destinations),
226
+ gasPrice: toApiNumber256Optional(params.gasPrice)
227
+ }
228
+ return this.provider.transactions.postTransactionsBuild(data)
210
229
  }
211
230
 
212
231
  async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
@@ -220,7 +239,14 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
220
239
  }
221
240
 
222
241
  async buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult> {
223
- return this.provider.contracts.postContractsUnsignedTxDeployContract(await this.usePublicKey(params))
242
+ const data: node.BuildDeployContractTx = {
243
+ ...(await this.usePublicKey(params)),
244
+ initialAttoAlphAmount: toApiNumber256Optional(params.initialAttoAlphAmount),
245
+ initialTokenAmounts: toApiTokens(params.initialTokenAmounts),
246
+ issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
247
+ gasPrice: toApiNumber256Optional(params.gasPrice)
248
+ }
249
+ return this.provider.contracts.postContractsUnsignedTxDeployContract(data)
224
250
  }
225
251
 
226
252
  async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
@@ -229,7 +255,11 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
229
255
  }
230
256
 
231
257
  async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
232
- return this.provider.contracts.postContractsUnsignedTxExecuteScript(await this.usePublicKey(params))
258
+ const data: node.BuildExecuteScriptTx = {
259
+ ...(await this.usePublicKey(params)),
260
+ tokens: toApiTokens(params.tokens)
261
+ }
262
+ return this.provider.contracts.postContractsUnsignedTxExecuteScript(data)
233
263
  }
234
264
 
235
265
  // in general, wallet should show the decoded information to user for confirmation
@@ -311,3 +341,24 @@ export function verifySignedMessage(message: string, publicKey: string, signatur
311
341
  const messageHash = blake.blake2b(extendedMessage, undefined, 32)
312
342
  return verifyHexString(utils.binToHex(messageHash), publicKey, signature)
313
343
  }
344
+
345
+ export interface Destination {
346
+ address: string
347
+ attoAlphAmount: Number256
348
+ tokens?: Token[]
349
+ lockTime?: number
350
+ message?: string
351
+ }
352
+ assertType<Eq<keyof Destination, keyof node.Destination>>
353
+
354
+ export function toApiDestination(data: Destination): node.Destination {
355
+ return { ...data, attoAlphAmount: toApiNumber256(data.attoAlphAmount), tokens: toApiTokens(data.tokens) }
356
+ }
357
+
358
+ export function toApiDestinations(data: Destination[]): node.Destination[] {
359
+ return data.map(toApiDestination)
360
+ }
361
+
362
+ export function fromApiDestination(data: node.Destination): Destination {
363
+ return { ...data, attoAlphAmount: fromApiNumber256(data.attoAlphAmount), tokens: fromApiTokens(data.tokens) }
364
+ }
@@ -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,11 @@ 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 { node } from '../api'
20
20
  import { Subscription, SubscribeOptions } from '../utils'
21
21
 
22
+ export type TxStatus = node.TxStatus
23
+
22
24
  export class TxStatusSubscription extends Subscription<TxStatus> {
23
25
  readonly txId: string
24
26
  readonly fromGroup?: number
@@ -33,7 +35,7 @@ export class TxStatusSubscription extends Subscription<TxStatus> {
33
35
  this.startPolling()
34
36
  }
35
37
 
36
- override async polling() {
38
+ override async polling(): Promise<void> {
37
39
  try {
38
40
  const txStatus = await this.provider.transactions.getTransactionsStatus({
39
41
  txId: this.txId,
@@ -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,13 +17,13 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import EventEmitter from 'eventemitter3'
20
+ import { getCurrentNodeProvider } from '../global'
20
21
  import { NodeProvider } from '../api'
21
22
 
22
23
  type MessageCallback<Message> = (message: Message) => Promise<void>
23
24
  type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>
24
25
 
25
26
  export interface SubscribeOptions<Message> {
26
- provider: NodeProvider
27
27
  pollingInterval: number
28
28
  messageCallback: MessageCallback<Message>
29
29
  errorCallback: ErrorCallback<Message>
@@ -40,7 +40,7 @@ export abstract class Subscription<Message> {
40
40
  protected cancelled: boolean
41
41
 
42
42
  constructor(options: SubscribeOptions<Message>) {
43
- this.provider = options.provider
43
+ this.provider = getCurrentNodeProvider()
44
44
  this.pollingInterval = options.pollingInterval
45
45
  this.messageCallback = options.messageCallback
46
46
  this.errorCallback = options.errorCallback
@@ -49,7 +49,7 @@ export abstract class Subscription<Message> {
49
49
  this.eventEmitter = new EventEmitter()
50
50
  }
51
51
 
52
- startPolling() {
52
+ startPolling(): void {
53
53
  this.eventEmitter.on('tick', async () => {
54
54
  await this.polling()
55
55
 
@@ -29,16 +29,6 @@ import * as explorer from '../api/api-explorer'
29
29
 
30
30
  const ec = new EC('secp256k1')
31
31
 
32
- export function convertHttpResponse<T>(
33
- response: node.HttpResponse<T, { detail: string }> | explorer.HttpResponse<T, { detail: string }>
34
- ): T {
35
- if (response.error) {
36
- throw new Error(response.error.detail)
37
- } else {
38
- return response.data
39
- }
40
- }
41
-
42
32
  export function signatureEncode(signature: EC.Signature): string {
43
33
  let sNormalized = signature.s
44
34
  if (ec.n && signature.s.cmp(ec.nh) === 1) {
@@ -51,7 +41,7 @@ export function signatureEncode(signature: EC.Signature): string {
51
41
  }
52
42
 
53
43
  // the signature should be in hex string format for 64 bytes
54
- export const signatureDecode = (ec: EC, signature: string): SignatureInput => {
44
+ export function signatureDecode(ec: EC, signature: string): SignatureInput {
55
45
  if (signature.length !== 128) {
56
46
  throw new Error('Invalid signature length')
57
47
  }
@@ -66,7 +56,7 @@ export const signatureDecode = (ec: EC, signature: string): SignatureInput => {
66
56
  }
67
57
  }
68
58
 
69
- const xorByte = (intValue: number) => {
59
+ export function xorByte(intValue: number): number {
70
60
  const byte0 = (intValue >> 24) & 0xff
71
61
  const byte1 = (intValue >> 16) & 0xff
72
62
  const byte2 = (intValue >> 8) & 0xff
@@ -85,7 +75,7 @@ enum AddressType {
85
75
  P2C = 0x03
86
76
  }
87
77
 
88
- export const groupOfAddress = (address: string): number => {
78
+ export function groupOfAddress(address: string): number {
89
79
  const decoded = bs58.decode(address)
90
80
 
91
81
  if (decoded.length == 0) throw new Error('Address string is empty')
@@ -103,7 +93,7 @@ export const groupOfAddress = (address: string): number => {
103
93
  }
104
94
  }
105
95
 
106
- const groupOfAddressBytes = (bytes: Uint8Array): number => {
96
+ function groupOfAddressBytes(bytes: Uint8Array): number {
107
97
  const hint = djb2(bytes) | 1
108
98
  const hash = xorByte(hint)
109
99
  const group = hash % TOTAL_NUMBER_OF_GROUPS
@@ -111,7 +101,7 @@ const groupOfAddressBytes = (bytes: Uint8Array): number => {
111
101
  }
112
102
 
113
103
  // Pay to public key hash address
114
- const groupOfP2pkhAddress = (address: Uint8Array): number => {
104
+ function groupOfP2pkhAddress(address: Uint8Array): number {
115
105
  if (address.length != 32) {
116
106
  throw new Error(`Invalid p2pkh address length: ${address.length}`)
117
107
  }
@@ -120,7 +110,7 @@ const groupOfP2pkhAddress = (address: Uint8Array): number => {
120
110
  }
121
111
 
122
112
  // Pay to multiple public key hash address
123
- const groupOfP2mpkhAddress = (address: Uint8Array): number => {
113
+ function groupOfP2mpkhAddress(address: Uint8Array): number {
124
114
  if ((address.length - 2) % 32 != 0) {
125
115
  throw new Error(`Invalid p2mpkh address length: ${address.length}`)
126
116
  }
@@ -129,7 +119,7 @@ const groupOfP2mpkhAddress = (address: Uint8Array): number => {
129
119
  }
130
120
 
131
121
  // Pay to script hash address
132
- const groupOfP2shAddress = (address: Uint8Array): number => {
122
+ function groupOfP2shAddress(address: Uint8Array): number {
133
123
  return groupOfAddressBytes(address)
134
124
  }
135
125
 
@@ -203,11 +193,11 @@ export function stringToHex(str: string): string {
203
193
  return hex
204
194
  }
205
195
 
206
- export function hexToString(str: any): string {
207
- return Buffer.from(str.toString(), 'hex').toString()
196
+ export function hexToString(str: string): string {
197
+ return Buffer.from(str, 'hex').toString()
208
198
  }
209
199
 
210
- export function timeout(ms: number) {
200
+ export function timeout(ms: number): Promise<void> {
211
201
  return new Promise((resolve) => setTimeout(resolve, ms))
212
202
  }
213
203
 
@@ -3,7 +3,7 @@
3
3
  "version": "0.1.0",
4
4
  "license": "GPL",
5
5
  "config": {
6
- "alephium_version": "1.5.0-rc0"
6
+ "alephium_version": "1.5.0-rc7"
7
7
  },
8
8
  "scripts": {
9
9
  "build": "rm -rf dist && npx tsc --build .",
@@ -12,7 +12,7 @@
12
12
  "stop-devnet": "node scripts/stop-devnet.js"
13
13
  },
14
14
  "dependencies": {
15
- "@alephium/web3": "0.2.0-rc.1"
15
+ "@alephium/web3": "0.2.0-rc.12"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/elliptic": "^6.4.13",
@@ -2,31 +2,32 @@
2
2
  * greeter.ts
3
3
  */
4
4
 
5
- import { NodeProvider, Contract, Script, TestContractParams } from '@alephium/web3'
6
- import { testWallet } from '@alephium/web3/test'
5
+ import { TestContractParams, web3, Project } from '@alephium/web3'
6
+ import { testNodeWallet } from '@alephium/web3/test'
7
7
 
8
8
  async function greet() {
9
- const provider = new NodeProvider('http://127.0.0.1:22973')
9
+ web3.setCurrentNodeProvider('http://127.0.0.1:22973')
10
+ await Project.build()
10
11
 
11
- const greeter = await Contract.fromSource(provider, 'greeter.ral')
12
+ const greeter = Project.contract('Greeter')
12
13
 
13
14
  const testParams: TestContractParams = {
14
15
  initialFields: { btcPrice: 1 }
15
16
  }
16
- const testResult = await greeter.testPublicMethod(provider, 'greet', testParams)
17
+ const testResult = await greeter.testPublicMethod('greet', testParams)
17
18
  console.log(testResult)
18
19
 
19
- const signer = await testWallet(provider)
20
+ const signer = await testNodeWallet()
20
21
 
21
22
  const deployTx = await greeter.transactionForDeployment(signer, { initialFields: testParams.initialFields })
22
23
  const greeterContractId = deployTx.contractId
23
24
  console.log(deployTx.fromGroup)
24
25
  console.log(deployTx.toGroup)
25
26
 
26
- const submitResult = await signer.submitTransaction(deployTx.unsignedTx, deployTx.txId)
27
+ const submitResult = await signer.submitTransaction(deployTx.unsignedTx)
27
28
  console.log(submitResult)
28
29
 
29
- const main = await Script.fromSource(provider, 'greeter_main.ral')
30
+ const main = Project.script('GreeterMain')
30
31
 
31
32
  const mainScriptTx = await main.transactionForDeployment(signer, {
32
33
  initialFields: { greeterContractId: greeterContractId }
@@ -34,7 +35,7 @@ async function greet() {
34
35
  console.log(mainScriptTx.fromGroup)
35
36
  console.log(mainScriptTx.toGroup)
36
37
 
37
- const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx, mainScriptTx.txId)
38
+ const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx)
38
39
  console.log(mainSubmitResult)
39
40
  }
40
41
 
@@ -2,7 +2,7 @@
2
2
  "name": "my-dapp",
3
3
  "version": "0.1.0",
4
4
  "config": {
5
- "alephium_version": "1.5.0-rc0"
5
+ "alephium_version": "1.5.0-rc7"
6
6
  },
7
7
  "dependencies": {
8
8
  "@testing-library/jest-dom": "^5.16.4",
@@ -12,7 +12,7 @@
12
12
  "@types/node": "^16.11.26",
13
13
  "@types/react": "^18.0.3",
14
14
  "@types/react-dom": "^18.0.0",
15
- "@alephium/web3": "0.2.0-rc.1",
15
+ "@alephium/web3": "0.2.0-rc.12",
16
16
  "react": "^18.0.0",
17
17
  "react-dom": "^18.0.0",
18
18
  "react-scripts": "5.0.1",
@@ -1,12 +1,12 @@
1
1
  import React, { useEffect, useState } from 'react'
2
2
  import './App.css'
3
3
 
4
- import { explorer, Contract, Script } from '@alephium/web3'
4
+ import { ExplorerProvider, Contract, Script } from '@alephium/web3'
5
5
  import contractJson from './artifacts/greeter.ral.json'
6
6
  import scriptJson from './artifacts/greeter_main.ral.json'
7
7
 
8
8
  function Dashboard() {
9
- const api = new explorer.Api<null>({ baseUrl: 'https://mainnet-backend.alephium.org' })
9
+ const api = new ExplorerProvider('https://mainnet-backend.alephium.org')
10
10
  const contract = Contract.fromJson(contractJson).toString()
11
11
  const script = Script.fromJson(scriptJson).toString()
12
12
  const [blocks, setBlocks] = useState('')
package/.eslintrc.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "plugins": ["security", "header"],
3
- "extends": [
4
- "prettier",
5
- "plugin:prettier/recommended",
6
- "plugin:security/recommended",
7
- "plugin:@typescript-eslint/recommended"
8
- ],
9
- "parser": "@typescript-eslint/parser",
10
- "rules": {
11
- "header/header": [2, "scripts/header.js"]
12
- },
13
- "overrides": [
14
- {
15
- "files": ["*.ts"],
16
- "rules": {
17
- "security/detect-non-literal-fs-filename": "off"
18
- }
19
- }
20
- ]
21
- }