@alephium/web3 2.0.2 → 2.0.4

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.
@@ -42,9 +42,7 @@ import {
42
42
  SignExecuteScriptTxParams,
43
43
  SignerProvider,
44
44
  Address,
45
- SignExecuteScriptTxResult,
46
- Account,
47
- isGroupedAccount
45
+ SignExecuteScriptTxResult
48
46
  } from '../signer'
49
47
  import * as ralph from './ralph'
50
48
  import {
@@ -61,13 +59,7 @@ import {
61
59
  isHexString,
62
60
  hexToString
63
61
  } from '../utils'
64
- import {
65
- contractIdFromAddress,
66
- groupOfAddress,
67
- addressFromContractId,
68
- subContractId,
69
- isGrouplessAddressWithoutGroupIndex
70
- } from '../address'
62
+ import { contractIdFromAddress, groupOfAddress, addressFromContractId, subContractId } from '../address'
71
63
  import { getCurrentNodeProvider } from '../global'
72
64
  import { EventSubscribeOptions, EventSubscription, subscribeToEvents } from './events'
73
65
  import { MINIMAL_CONTRACT_DEPOSIT, ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants'
@@ -100,7 +92,6 @@ import {
100
92
  } from '../codec'
101
93
  import { TraceableError } from '../error'
102
94
  import { SimulationResult } from '../api/api-alephium'
103
- import { scriptCodec } from '../codec/script-codec'
104
95
 
105
96
  const crypto = new WebCrypto()
106
97
 
@@ -796,26 +787,9 @@ export class Script extends Artifact {
796
787
  return JSON.stringify(object, null, 2)
797
788
  }
798
789
 
799
- getBytecodeAndGroup<P extends Fields>(account: Account, fields: P): [string, number] {
800
- const bytecode = this.buildByteCodeToDeploy(fields)
801
- if (isGroupedAccount(account)) {
802
- return [bytecode, account.group]
803
- }
804
-
805
- const group = getGroupFromTxScript(bytecode)
806
- const defaultGroup = groupOfAddress(account.address)
807
- if (group === undefined || group === defaultGroup) {
808
- return [bytecode, defaultGroup]
809
- }
810
-
811
- const newFields = ralph.updateFieldsWithGroup(fields, group) as P
812
- const newBytecode = this.buildByteCodeToDeploy(newFields)
813
- return [newBytecode, group]
814
- }
815
-
816
790
  async txParamsForExecution<P extends Fields>(params: ExecuteScriptParams<P>): Promise<SignExecuteScriptTxParams> {
817
791
  const selectedAccount = await params.signer.getSelectedAccount()
818
- const [bytecode, group] = this.getBytecodeAndGroup(selectedAccount, params.initialFields ?? {})
792
+ const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {})
819
793
  const signerParams: SignExecuteScriptTxParams = {
820
794
  signerAddress: selectedAccount.address,
821
795
  signerKeyType: selectedAccount.keyType,
@@ -824,7 +798,6 @@ export class Script extends Artifact {
824
798
  tokens: params.tokens,
825
799
  gasAmount: params.gasAmount,
826
800
  gasPrice: params.gasPrice,
827
- group,
828
801
  dustAmount: params.dustAmount
829
802
  }
830
803
  return signerParams
@@ -839,34 +812,6 @@ export class Script extends Artifact {
839
812
  }
840
813
  }
841
814
 
842
- function getGroupFromTxScript(bytecode: string): number | undefined {
843
- const script = scriptCodec.decode(hexToBinUnsafe(bytecode))
844
- const instrs = script.methods.flatMap((method) => method.instrs)
845
- for (let index = 0; index < instrs.length - 1; index += 1) {
846
- const instr = instrs[`${index}`]
847
- const nextInstr = instrs[index + 1]
848
- if (
849
- instr.name === 'BytesConst' &&
850
- instr.value.length === 32 &&
851
- (nextInstr.name === 'CallExternal' || nextInstr.name === 'CallExternalBySelector')
852
- ) {
853
- const groupIndex = instr.value[instr.value.length - 1]
854
- if (groupIndex >= 0 && groupIndex < TOTAL_NUMBER_OF_GROUPS) {
855
- return groupIndex
856
- }
857
- }
858
- }
859
- for (const instr of instrs) {
860
- if (instr.name === 'BytesConst' && instr.value.length === 32) {
861
- const groupIndex = instr.value[instr.value.length - 1]
862
- if (groupIndex >= 0 && groupIndex < TOTAL_NUMBER_OF_GROUPS) {
863
- return groupIndex
864
- }
865
- }
866
- }
867
- return undefined
868
- }
869
-
870
815
  export function fromApiFields(
871
816
  immFields: node.Val[],
872
817
  mutFields: node.Val[],
@@ -399,32 +399,6 @@ function checkPrimitiveValue(name: string, ralphType: string, value: Val): strin
399
399
  throw Error(`Invalid value ${value} for ${name}, expected a value of type ${ralphType}`)
400
400
  }
401
401
 
402
- export function updateFieldsWithGroup(fields: Fields, group: number): Fields {
403
- const newFields: Fields = {}
404
- for (const key in fields) {
405
- const value = fields[`${key}`]
406
- newFields[`${key}`] = updateValWithGroup(value, group)
407
- }
408
- return newFields
409
- }
410
-
411
- function updateValWithGroup(value: Val, group: number): Val {
412
- if (typeof value === 'string') {
413
- if (!isValidAddress(value)) return value
414
- if (isGrouplessAddressWithoutGroupIndex(value)) return `${value}:${group}`
415
- return value
416
- }
417
-
418
- if (Array.isArray(value)) {
419
- return value.map((v) => updateValWithGroup(v, group))
420
- }
421
-
422
- if (typeof value === 'object') {
423
- return updateFieldsWithGroup(value as Fields, group)
424
- }
425
- return value
426
- }
427
-
428
402
  const scriptFieldRegex = /\{([0-9]*)\}/g
429
403
 
430
404
  export function buildScriptByteCode(
@@ -18,7 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { binToHex, hexToBinUnsafe } from '../utils'
20
20
  import { fromApiNumber256, node, NodeProvider, toApiNumber256Optional, toApiTokens } from '../api'
21
- import { addressFromPublicKey, contractIdFromAddress } from '../address'
21
+ import { addressFromPublicKey, contractIdFromAddress, groupOfAddress, isGrouplessAddress } from '../address'
22
22
  import { toApiDestinations } from './signer'
23
23
  import {
24
24
  SignChainedTxParams,
@@ -36,7 +36,7 @@ import {
36
36
  SignUnsignedTxParams,
37
37
  SignUnsignedTxResult,
38
38
  BuildTxResult,
39
- GrouplessBuildTxResult
39
+ isGroupedKeyType
40
40
  } from './types'
41
41
  import { unsignedTxCodec } from '../codec'
42
42
  import { groupIndexOfTransaction } from '../transaction'
@@ -47,6 +47,9 @@ import {
47
47
  BuildExecuteScriptTxResult,
48
48
  BuildTransferTxResult
49
49
  } from '../api/api-alephium'
50
+ import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
51
+ import { scriptCodec } from '../codec/script-codec'
52
+ import { LockupScript } from '../codec/lockup-script-codec'
50
53
 
51
54
  export abstract class TransactionBuilder {
52
55
  abstract get nodeProvider(): NodeProvider
@@ -204,13 +207,34 @@ export abstract class TransactionBuilder {
204
207
  }
205
208
  }
206
209
 
210
+ private static checkAndGetParams(params: SignExecuteScriptTxParams): SignExecuteScriptTxParams {
211
+ if (isGroupedKeyType(params.signerKeyType ?? 'default')) {
212
+ return params
213
+ }
214
+
215
+ if (!isGrouplessAddress(params.signerAddress)) {
216
+ throw new Error('Invalid signer key type for groupless address')
217
+ }
218
+
219
+ const group = params.group ?? getGroupFromTxScript(params.bytecode)
220
+ const defaultGroup = groupOfAddress(params.signerAddress)
221
+ if (group === undefined || group === defaultGroup) {
222
+ return { ...params, group: defaultGroup }
223
+ }
224
+
225
+ const newBytecode = updateBytecodeWithGroup(params.bytecode, group)
226
+ const newParams = { ...params, bytecode: newBytecode }
227
+ return { ...newParams, group }
228
+ }
229
+
207
230
  private buildExecuteScriptTxParams(params: SignExecuteScriptTxParams, publicKey: string): node.BuildExecuteScriptTx {
208
231
  TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType)
209
232
 
210
- const { attoAlphAmount, tokens, gasPrice, dustAmount, ...rest } = params
233
+ const newParams = TransactionBuilder.checkAndGetParams(params)
234
+ const { signerKeyType, attoAlphAmount, tokens, gasPrice, dustAmount, ...rest } = newParams
211
235
  return {
212
236
  fromPublicKey: publicKey,
213
- fromPublicKeyType: params.signerKeyType,
237
+ fromPublicKeyType: signerKeyType,
214
238
  attoAlphAmount: toApiNumber256Optional(attoAlphAmount),
215
239
  tokens: toApiTokens(tokens),
216
240
  gasPrice: toApiNumber256Optional(gasPrice),
@@ -296,3 +320,47 @@ export abstract class TransactionBuilder {
296
320
  }
297
321
  }
298
322
  }
323
+
324
+ export function getGroupFromTxScript(bytecode: string): number | undefined {
325
+ const script = scriptCodec.decode(hexToBinUnsafe(bytecode))
326
+ const instrs = script.methods.flatMap((method) => method.instrs)
327
+ for (let index = 0; index < instrs.length - 1; index += 1) {
328
+ const instr = instrs[`${index}`]
329
+ const nextInstr = instrs[index + 1]
330
+ if (
331
+ instr.name === 'BytesConst' &&
332
+ instr.value.length === 32 &&
333
+ (nextInstr.name === 'CallExternal' || nextInstr.name === 'CallExternalBySelector')
334
+ ) {
335
+ const groupIndex = instr.value[instr.value.length - 1]
336
+ if (groupIndex >= 0 && groupIndex < TOTAL_NUMBER_OF_GROUPS) {
337
+ return groupIndex
338
+ }
339
+ }
340
+ }
341
+ for (const instr of instrs) {
342
+ if (instr.name === 'BytesConst' && instr.value.length === 32) {
343
+ const groupIndex = instr.value[instr.value.length - 1]
344
+ if (groupIndex >= 0 && groupIndex < TOTAL_NUMBER_OF_GROUPS) {
345
+ return groupIndex
346
+ }
347
+ }
348
+ }
349
+ return undefined
350
+ }
351
+
352
+ export function updateBytecodeWithGroup(bytecode: string, group: number): string {
353
+ const script = scriptCodec.decode(hexToBinUnsafe(bytecode))
354
+ const newMethods = script.methods.map((method) => {
355
+ const newInstrs = method.instrs.map((instr) => {
356
+ if (instr.name === 'AddressConst' && instr.value.kind === 'P2PK') {
357
+ const newLockupScript: LockupScript = { ...instr.value, value: { ...instr.value.value, group } }
358
+ return { ...instr, value: newLockupScript }
359
+ }
360
+ return instr
361
+ })
362
+ return { ...method, instrs: newInstrs }
363
+ })
364
+ const bytes = scriptCodec.encode({ methods: newMethods })
365
+ return binToHex(bytes)
366
+ }