@alephium/web3 2.0.0-rc.2 → 2.0.0

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.
@@ -16,7 +16,16 @@ 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 { Val, decodeArrayType, toApiAddress, toApiBoolean, toApiByteVec, toApiNumber256, PrimitiveTypes } from '../api'
19
+ import {
20
+ Val,
21
+ decodeArrayType,
22
+ toApiAddress,
23
+ toApiBoolean,
24
+ toApiByteVec,
25
+ toApiNumber256,
26
+ PrimitiveTypes,
27
+ decodeTupleType
28
+ } from '../api'
20
29
  import { HexString, binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
21
30
  import { Fields, FieldsSig, Struct } from './contract'
22
31
  import {
@@ -208,6 +217,19 @@ export function calcFieldSize(
208
217
  const base = calcFieldSize(baseType, isMutable, structs)
209
218
  return { immFields: base.immFields * size, mutFields: base.mutFields * size }
210
219
  }
220
+ if (type.startsWith('(')) {
221
+ const tuple = decodeTupleType(type)
222
+ return tuple.reduce(
223
+ (acc, fieldType) => {
224
+ const subFieldSize = calcFieldSize(fieldType, isMutable, structs)
225
+ return {
226
+ immFields: acc.immFields + subFieldSize.immFields,
227
+ mutFields: acc.mutFields + subFieldSize.mutFields
228
+ }
229
+ },
230
+ { immFields: 0, mutFields: 0 }
231
+ )
232
+ }
211
233
  return isMutable ? { immFields: 0, mutFields: 1 } : { immFields: 1, mutFields: 0 }
212
234
  }
213
235
 
@@ -284,6 +306,11 @@ export function typeLength(typ: string, structs: Struct[]): number {
284
306
  return size * typeLength(baseType, structs)
285
307
  }
286
308
 
309
+ if (typ.startsWith('(')) {
310
+ const tuple = decodeTupleType(typ)
311
+ return tuple.reduce((acc, fieldType) => acc + typeLength(fieldType, structs), 0)
312
+ }
313
+
287
314
  const struct = structs.find((s) => s.name === typ)
288
315
  if (struct !== undefined) {
289
316
  return struct.fieldTypes.reduce((acc, fieldType) => acc + typeLength(fieldType, structs), 0)
@@ -323,6 +350,15 @@ function flattenField(
323
350
  return flattenField(isMutable, `${name}[${index}]`, baseType, item, structs)
324
351
  })
325
352
  }
353
+ if (Array.isArray(value) && type.startsWith('(')) {
354
+ const tuple = decodeTupleType(type)
355
+ if (value.length !== tuple.length) {
356
+ throw Error(`Invalid tuple length, expected ${tuple.length}, got ${value.length}`)
357
+ }
358
+ return tuple.flatMap((fieldType, index) => {
359
+ return flattenField(isMutable, `${name}._${index}`, fieldType, value[`${index}`], structs)
360
+ })
361
+ }
326
362
  const struct = structs.find((s) => s.name === type)
327
363
  if (struct !== undefined) {
328
364
  if (typeof value !== 'object') {
@@ -207,13 +207,14 @@ export abstract class TransactionBuilder {
207
207
  private buildExecuteScriptTxParams(params: SignExecuteScriptTxParams, publicKey: string): node.BuildExecuteScriptTx {
208
208
  TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType)
209
209
 
210
- const { attoAlphAmount, tokens, gasPrice, ...rest } = params
210
+ const { attoAlphAmount, tokens, gasPrice, dustAmount, ...rest } = params
211
211
  return {
212
212
  fromPublicKey: publicKey,
213
213
  fromPublicKeyType: params.signerKeyType,
214
214
  attoAlphAmount: toApiNumber256Optional(attoAlphAmount),
215
215
  tokens: toApiTokens(tokens),
216
216
  gasPrice: toApiNumber256Optional(gasPrice),
217
+ dustAmount: toApiNumber256Optional(dustAmount),
217
218
  ...rest
218
219
  }
219
220
  }
@@ -34,8 +34,12 @@ export interface Destination {
34
34
  }
35
35
  assertType<Eq<keyof Destination, keyof node.Destination>>
36
36
 
37
- export type GroupedKeyType = 'default' | 'bip340-schnorr'
38
- export type GrouplessKeyType = 'gl-secp256k1' | 'gl-secp256r1' | 'gl-ed25519' | 'gl-webauthn'
37
+ export const groupedKeyTypes = ['default', 'bip340-schnorr'] as const
38
+ export const grouplessKeyTypes = ['gl-secp256k1', 'gl-secp256r1', 'gl-ed25519', 'gl-webauthn'] as const
39
+ export const keyTypes = [...groupedKeyTypes, ...grouplessKeyTypes] as const
40
+
41
+ export type GroupedKeyType = (typeof groupedKeyTypes)[number]
42
+ export type GrouplessKeyType = (typeof grouplessKeyTypes)[number]
39
43
 
40
44
  export type KeyType = GroupedKeyType | GrouplessKeyType
41
45
 
@@ -54,12 +58,20 @@ export interface GrouplessAccount {
54
58
 
55
59
  export type Account = GroupedAccount | GrouplessAccount
56
60
 
61
+ export function isGroupedKeyType(keyType: KeyType): keyType is GroupedKeyType {
62
+ return keyType === 'default' || keyType === 'bip340-schnorr'
63
+ }
64
+
65
+ export function isGrouplessKeyType(keyType: KeyType): keyType is GrouplessKeyType {
66
+ return keyType !== 'default' && keyType !== 'bip340-schnorr'
67
+ }
68
+
57
69
  export function isGroupedAccount(account: Account): account is GroupedAccount {
58
- return account.keyType === 'default' || account.keyType === 'bip340-schnorr'
70
+ return isGroupedKeyType(account.keyType)
59
71
  }
60
72
 
61
73
  export function isGrouplessAccount(account: Account): account is GrouplessAccount {
62
- return account.keyType !== 'default' && account.keyType !== 'bip340-schnorr'
74
+ return isGrouplessKeyType(account.keyType)
63
75
  }
64
76
 
65
77
  export type SignerAddress = { signerAddress: string; signerKeyType?: KeyType }
@@ -127,6 +139,7 @@ export interface SignExecuteScriptTxParams {
127
139
  gasPrice?: Number256
128
140
  gasEstimationMultiplier?: number
129
141
  group?: number
142
+ dustAmount?: Number256
130
143
  }
131
144
  assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
132
145
  export interface SignExecuteScriptTxResult {
package/src/utils/sign.ts CHANGED
@@ -38,7 +38,7 @@ necc.utils.hmacSha256Sync = (key: Uint8Array, ...messages: Uint8Array[]): Uint8A
38
38
 
39
39
  function checkKeyType(keyType: KeyType) {
40
40
  if (keyType !== 'default' && keyType !== 'bip340-schnorr' && keyType !== 'gl-secp256k1') {
41
- throw new Error(`Invalid key type ${keyType}`)
41
+ throw new Error(`Invalid key type ${keyType}, only supports secp256k1 and schnorr for now`)
42
42
  }
43
43
  }
44
44