@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.
@@ -43,7 +43,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
43
43
  exports.getContractCodeByCodeHash = exports.getTokenIdFromUnsignedTx = exports.getContractIdFromUnsignedTx = exports.getContractEventsCurrentCount = exports.multicallMethods = exports.signExecuteMethod = exports.callMethod = exports.subscribeContractEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.getMapItem = exports.RalphMap = exports.printDebugMessagesFromTx = exports.getDebugMessagesFromTx = exports.testMethod = exports.extractMapsFromApiResult = exports.addStdIdToFields = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.DestroyContractEventAddresses = exports.CreateContractEventAddresses = exports.ExecutableScript = exports.ContractFactory = exports.randomTxId = exports.fromApiEventFields = exports.fromApiArray = exports.getDefaultValue = exports.fromApiFields = exports.Script = exports.Contract = exports.Artifact = exports.Struct = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = exports.StdIdFieldName = void 0;
44
44
  const fs_1 = require("fs");
45
45
  const api_1 = require("../api");
46
- const signer_1 = require("../signer");
47
46
  const ralph = __importStar(require("./ralph"));
48
47
  const utils_1 = require("../utils");
49
48
  const address_1 = require("../address");
@@ -54,7 +53,6 @@ const blake = __importStar(require("blakejs"));
54
53
  const debug_1 = require("../debug");
55
54
  const codec_1 = require("../codec");
56
55
  const error_1 = require("../error");
57
- const script_codec_1 = require("../codec/script-codec");
58
56
  const crypto = new utils_1.WebCrypto();
59
57
  exports.StdIdFieldName = '__stdInterfaceId';
60
58
  exports.DEFAULT_NODE_COMPILER_OPTIONS = {
@@ -522,23 +520,9 @@ class Script extends Artifact {
522
520
  };
523
521
  return JSON.stringify(object, null, 2);
524
522
  }
525
- getBytecodeAndGroup(account, fields) {
526
- const bytecode = this.buildByteCodeToDeploy(fields);
527
- if ((0, signer_1.isGroupedAccount)(account)) {
528
- return [bytecode, account.group];
529
- }
530
- const group = getGroupFromTxScript(bytecode);
531
- const defaultGroup = (0, address_1.groupOfAddress)(account.address);
532
- if (group === undefined || group === defaultGroup) {
533
- return [bytecode, defaultGroup];
534
- }
535
- const newFields = ralph.updateFieldsWithGroup(fields, group);
536
- const newBytecode = this.buildByteCodeToDeploy(newFields);
537
- return [newBytecode, group];
538
- }
539
523
  async txParamsForExecution(params) {
540
524
  const selectedAccount = await params.signer.getSelectedAccount();
541
- const [bytecode, group] = this.getBytecodeAndGroup(selectedAccount, params.initialFields ?? {});
525
+ const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {});
542
526
  const signerParams = {
543
527
  signerAddress: selectedAccount.address,
544
528
  signerKeyType: selectedAccount.keyType,
@@ -547,7 +531,6 @@ class Script extends Artifact {
547
531
  tokens: params.tokens,
548
532
  gasAmount: params.gasAmount,
549
533
  gasPrice: params.gasPrice,
550
- group,
551
534
  dustAmount: params.dustAmount
552
535
  };
553
536
  return signerParams;
@@ -562,31 +545,6 @@ class Script extends Artifact {
562
545
  }
563
546
  }
564
547
  exports.Script = Script;
565
- function getGroupFromTxScript(bytecode) {
566
- const script = script_codec_1.scriptCodec.decode((0, utils_1.hexToBinUnsafe)(bytecode));
567
- const instrs = script.methods.flatMap((method) => method.instrs);
568
- for (let index = 0; index < instrs.length - 1; index += 1) {
569
- const instr = instrs[`${index}`];
570
- const nextInstr = instrs[index + 1];
571
- if (instr.name === 'BytesConst' &&
572
- instr.value.length === 32 &&
573
- (nextInstr.name === 'CallExternal' || nextInstr.name === 'CallExternalBySelector')) {
574
- const groupIndex = instr.value[instr.value.length - 1];
575
- if (groupIndex >= 0 && groupIndex < constants_1.TOTAL_NUMBER_OF_GROUPS) {
576
- return groupIndex;
577
- }
578
- }
579
- }
580
- for (const instr of instrs) {
581
- if (instr.name === 'BytesConst' && instr.value.length === 32) {
582
- const groupIndex = instr.value[instr.value.length - 1];
583
- if (groupIndex >= 0 && groupIndex < constants_1.TOTAL_NUMBER_OF_GROUPS) {
584
- return groupIndex;
585
- }
586
- }
587
- }
588
- return undefined;
589
- }
590
548
  function fromApiFields(immFields, mutFields, fieldsSig, structs) {
591
549
  let [immIndex, mutIndex] = [0, 0];
592
550
  const func = (type, isMutable) => {
@@ -63,7 +63,6 @@ export declare function flattenFields(fields: Fields, names: string[], types: st
63
63
  value: Val;
64
64
  isMutable: boolean;
65
65
  }[];
66
- export declare function updateFieldsWithGroup(fields: Fields, group: number): Fields;
67
66
  export declare function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): string;
68
67
  export declare function encodeContractFields(fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): {
69
68
  encodedImmFields: Uint8Array;
@@ -17,7 +17,7 @@ You should have received a copy of the GNU Lesser General Public License
17
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.updateFieldsWithGroup = exports.flattenFields = exports.typeLength = exports.encodeMapKey = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodePrimitiveValues = exports.addressVal = exports.byteVecVal = exports.u256Val = exports.i256Val = exports.boolVal = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = void 0;
20
+ exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.flattenFields = exports.typeLength = exports.encodeMapKey = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodePrimitiveValues = exports.addressVal = exports.byteVecVal = exports.u256Val = exports.i256Val = exports.boolVal = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = void 0;
21
21
  const api_1 = require("../api");
22
22
  const utils_1 = require("../utils");
23
23
  const codec_1 = require("../codec");
@@ -336,31 +336,6 @@ function checkPrimitiveValue(name, ralphType, value) {
336
336
  }
337
337
  throw Error(`Invalid value ${value} for ${name}, expected a value of type ${ralphType}`);
338
338
  }
339
- function updateFieldsWithGroup(fields, group) {
340
- const newFields = {};
341
- for (const key in fields) {
342
- const value = fields[`${key}`];
343
- newFields[`${key}`] = updateValWithGroup(value, group);
344
- }
345
- return newFields;
346
- }
347
- exports.updateFieldsWithGroup = updateFieldsWithGroup;
348
- function updateValWithGroup(value, group) {
349
- if (typeof value === 'string') {
350
- if (!(0, address_1.isValidAddress)(value))
351
- return value;
352
- if ((0, address_1.isGrouplessAddressWithoutGroupIndex)(value))
353
- return `${value}:${group}`;
354
- return value;
355
- }
356
- if (Array.isArray(value)) {
357
- return value.map((v) => updateValWithGroup(v, group));
358
- }
359
- if (typeof value === 'object') {
360
- return updateFieldsWithGroup(value, group);
361
- }
362
- return value;
363
- }
364
339
  const scriptFieldRegex = /\{([0-9]*)\}/g;
365
340
  function buildScriptByteCode(bytecodeTemplate, fields, fieldsSig, structs) {
366
341
  const allFields = flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs);
@@ -12,8 +12,11 @@ export declare abstract class TransactionBuilder {
12
12
  static buildUnsignedTx(params: SignUnsignedTxParams): Omit<SignUnsignedTxResult, 'signature'>;
13
13
  private buildTransferTxParams;
14
14
  private buildDeployContractTxParams;
15
+ private static checkAndGetParams;
15
16
  private buildExecuteScriptTxParams;
16
17
  private convertTransferTxResult;
17
18
  private convertDeployContractTxResult;
18
19
  private convertExecuteScriptTxResult;
19
20
  }
21
+ export declare function getGroupFromTxScript(bytecode: string): number | undefined;
22
+ export declare function updateBytecodeWithGroup(bytecode: string, group: number): string;
@@ -17,14 +17,17 @@ You should have received a copy of the GNU Lesser General Public License
17
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.TransactionBuilder = void 0;
20
+ exports.updateBytecodeWithGroup = exports.getGroupFromTxScript = exports.TransactionBuilder = void 0;
21
21
  const utils_1 = require("../utils");
22
22
  const api_1 = require("../api");
23
23
  const address_1 = require("../address");
24
24
  const signer_1 = require("./signer");
25
+ const types_1 = require("./types");
25
26
  const codec_1 = require("../codec");
26
27
  const transaction_1 = require("../transaction");
27
28
  const hash_1 = require("../codec/hash");
29
+ const constants_1 = require("../constants");
30
+ const script_codec_1 = require("../codec/script-codec");
28
31
  class TransactionBuilder {
29
32
  static from(param0, param1, customFetch) {
30
33
  const nodeProvider = typeof param0 === 'string' ? new api_1.NodeProvider(param0, param1, customFetch) : param0;
@@ -147,12 +150,29 @@ class TransactionBuilder {
147
150
  ...rest
148
151
  };
149
152
  }
153
+ static checkAndGetParams(params) {
154
+ if ((0, types_1.isGroupedKeyType)(params.signerKeyType ?? 'default')) {
155
+ return params;
156
+ }
157
+ if (!(0, address_1.isGrouplessAddress)(params.signerAddress)) {
158
+ throw new Error('Invalid signer key type for groupless address');
159
+ }
160
+ const group = params.group ?? getGroupFromTxScript(params.bytecode);
161
+ const defaultGroup = (0, address_1.groupOfAddress)(params.signerAddress);
162
+ if (group === undefined || group === defaultGroup) {
163
+ return { ...params, group: defaultGroup };
164
+ }
165
+ const newBytecode = updateBytecodeWithGroup(params.bytecode, group);
166
+ const newParams = { ...params, bytecode: newBytecode };
167
+ return { ...newParams, group };
168
+ }
150
169
  buildExecuteScriptTxParams(params, publicKey) {
151
170
  TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType);
152
- const { attoAlphAmount, tokens, gasPrice, dustAmount, ...rest } = params;
171
+ const newParams = TransactionBuilder.checkAndGetParams(params);
172
+ const { signerKeyType, attoAlphAmount, tokens, gasPrice, dustAmount, ...rest } = newParams;
153
173
  return {
154
174
  fromPublicKey: publicKey,
155
- fromPublicKeyType: params.signerKeyType,
175
+ fromPublicKeyType: signerKeyType,
156
176
  attoAlphAmount: (0, api_1.toApiNumber256Optional)(attoAlphAmount),
157
177
  tokens: (0, api_1.toApiTokens)(tokens),
158
178
  gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
@@ -229,3 +249,45 @@ class TransactionBuilder {
229
249
  }
230
250
  }
231
251
  exports.TransactionBuilder = TransactionBuilder;
252
+ function getGroupFromTxScript(bytecode) {
253
+ const script = script_codec_1.scriptCodec.decode((0, utils_1.hexToBinUnsafe)(bytecode));
254
+ const instrs = script.methods.flatMap((method) => method.instrs);
255
+ for (let index = 0; index < instrs.length - 1; index += 1) {
256
+ const instr = instrs[`${index}`];
257
+ const nextInstr = instrs[index + 1];
258
+ if (instr.name === 'BytesConst' &&
259
+ instr.value.length === 32 &&
260
+ (nextInstr.name === 'CallExternal' || nextInstr.name === 'CallExternalBySelector')) {
261
+ const groupIndex = instr.value[instr.value.length - 1];
262
+ if (groupIndex >= 0 && groupIndex < constants_1.TOTAL_NUMBER_OF_GROUPS) {
263
+ return groupIndex;
264
+ }
265
+ }
266
+ }
267
+ for (const instr of instrs) {
268
+ if (instr.name === 'BytesConst' && instr.value.length === 32) {
269
+ const groupIndex = instr.value[instr.value.length - 1];
270
+ if (groupIndex >= 0 && groupIndex < constants_1.TOTAL_NUMBER_OF_GROUPS) {
271
+ return groupIndex;
272
+ }
273
+ }
274
+ }
275
+ return undefined;
276
+ }
277
+ exports.getGroupFromTxScript = getGroupFromTxScript;
278
+ function updateBytecodeWithGroup(bytecode, group) {
279
+ const script = script_codec_1.scriptCodec.decode((0, utils_1.hexToBinUnsafe)(bytecode));
280
+ const newMethods = script.methods.map((method) => {
281
+ const newInstrs = method.instrs.map((instr) => {
282
+ if (instr.name === 'AddressConst' && instr.value.kind === 'P2PK') {
283
+ const newLockupScript = { ...instr.value, value: { ...instr.value.value, group } };
284
+ return { ...instr, value: newLockupScript };
285
+ }
286
+ return instr;
287
+ });
288
+ return { ...method, instrs: newInstrs };
289
+ });
290
+ const bytes = script_codec_1.scriptCodec.encode({ methods: newMethods });
291
+ return (0, utils_1.binToHex)(bytes);
292
+ }
293
+ exports.updateBytecodeWithGroup = updateBytecodeWithGroup;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -34,7 +34,7 @@
34
34
  "author": "Alephium dev <dev@alephium.org>",
35
35
  "config": {
36
36
  "alephium_version": "4.2.3",
37
- "explorer_backend_version": "2.3.2"
37
+ "explorer_backend_version": "3.3.2"
38
38
  },
39
39
  "type": "commonjs",
40
40
  "dependencies": {