@alephium/web3 0.12.2 → 0.12.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.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +1 -1
- package/dist/src/api/api-alephium.js +1 -1
- package/dist/src/contract/contract.d.ts +11 -6
- package/dist/src/contract/contract.js +32 -18
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/webcrypto.d.ts +4 -0
- package/dist/src/utils/webcrypto.js +42 -0
- package/package.json +5 -5
- package/src/api/api-alephium.ts +1 -1
- package/src/contract/contract.ts +47 -15
- package/src/utils/index.ts +1 -0
- package/src/utils/webcrypto.ts +42 -0
|
@@ -938,7 +938,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
|
|
|
938
938
|
}
|
|
939
939
|
/**
|
|
940
940
|
* @title Alephium API
|
|
941
|
-
* @version 2.3.
|
|
941
|
+
* @version 2.3.3
|
|
942
942
|
* @baseUrl ../
|
|
943
943
|
*/
|
|
944
944
|
export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
|
@@ -128,18 +128,18 @@ export declare class Contract extends Artifact {
|
|
|
128
128
|
toApiContractStates(states?: ContractState[]): node.ContractState[] | undefined;
|
|
129
129
|
toApiTestContractParams(funcName: string, params: TestContractParams): node.TestContract;
|
|
130
130
|
fromApiContractState(state: node.ContractState): ContractState<Fields>;
|
|
131
|
-
static fromApiContractState(state: node.ContractState): ContractState;
|
|
131
|
+
static fromApiContractState(state: node.ContractState, getContractByCodeHash?: (codeHash: string) => Contract): ContractState;
|
|
132
132
|
static ContractCreatedEventIndex: number;
|
|
133
133
|
static ContractCreatedEvent: EventSig;
|
|
134
134
|
static ContractDestroyedEventIndex: number;
|
|
135
135
|
static ContractDestroyedEvent: EventSig;
|
|
136
|
-
static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined, txId: string): ContractEvent;
|
|
136
|
+
static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined, txId: string, getContractByCodeHash?: (codeHash: string) => Contract): ContractEvent;
|
|
137
137
|
fromApiTestContractResult(methodName: string, result: node.TestContractResult, txId: string): TestContractResult<unknown>;
|
|
138
138
|
txParamsForDeployment<P extends Fields>(signer: SignerProvider, params: DeployContractParams<P>): Promise<SignDeployContractTxParams>;
|
|
139
139
|
buildByteCodeToDeploy(initialFields: Fields): string;
|
|
140
|
-
static fromApiEvents(events: node.ContractEventByTxId[], addressToCodeHash: Map<string, string>, txId: string): ContractEvent[];
|
|
140
|
+
static fromApiEvents(events: node.ContractEventByTxId[], addressToCodeHash: Map<string, string>, txId: string, getContractByCodeHash?: (codeHash: string) => Contract): ContractEvent[];
|
|
141
141
|
toApiCallContract<T extends Arguments>(params: CallContractParams<T>, groupIndex: number, contractAddress: string, methodIndex: number): node.CallContract;
|
|
142
|
-
fromApiCallContractResult(result: node.CallContractResult, txId: string, methodIndex: number): CallContractResult<unknown>;
|
|
142
|
+
fromApiCallContractResult(result: node.CallContractResult, txId: string, methodIndex: number, getContractByCodeHash?: (codeHash: string) => Contract): CallContractResult<unknown>;
|
|
143
143
|
}
|
|
144
144
|
export declare class Script extends Artifact {
|
|
145
145
|
readonly bytecodeTemplate: string;
|
|
@@ -235,6 +235,11 @@ export declare abstract class ContractFactory<I extends ContractInstance, F exte
|
|
|
235
235
|
deploy(signer: SignerProvider, deployParams: DeployContractParams<F>): Promise<DeployContractResult<I>>;
|
|
236
236
|
stateForTest(initFields: F, asset?: Asset, address?: string): ContractState<F>;
|
|
237
237
|
}
|
|
238
|
+
export declare class ExecutableScript<P extends Fields = Fields> {
|
|
239
|
+
readonly script: Script;
|
|
240
|
+
constructor(script: Script);
|
|
241
|
+
execute(signer: SignerProvider, params: ExecuteScriptParams<P>): Promise<ExecuteScriptResult>;
|
|
242
|
+
}
|
|
238
243
|
export interface ExecuteScriptParams<P extends Fields = Fields> {
|
|
239
244
|
initialFields: P;
|
|
240
245
|
attoAlphAmount?: Number256;
|
|
@@ -296,7 +301,7 @@ export declare function subscribeContractDestroyedEvent(options: SubscribeOption
|
|
|
296
301
|
export declare function decodeEvent<F extends Fields, M extends ContractEvent<F>>(contract: Contract, instance: ContractInstance, event: node.ContractEvent, targetEventIndex: number): M;
|
|
297
302
|
export declare function subscribeContractEvent<F extends Fields, M extends ContractEvent<F>>(contract: Contract, instance: ContractInstance, options: SubscribeOptions<M>, eventName: string, fromCount?: number): EventSubscription;
|
|
298
303
|
export declare function subscribeContractEvents(contract: Contract, instance: ContractInstance, options: SubscribeOptions<ContractEvent<any>>, fromCount?: number): EventSubscription;
|
|
299
|
-
export declare function callMethod<I extends ContractInstance, F extends Fields, A extends Arguments, R>(contract: ContractFactory<I, F>, instance: ContractInstance, methodName: string, params: Optional<CallContractParams<A>, 'args'
|
|
300
|
-
export declare function multicallMethods<I extends ContractInstance, F extends Fields>(contract: ContractFactory<I, F>, instance: ContractInstance, calls: Record<string, Optional<CallContractParams<any>, 'args'
|
|
304
|
+
export declare function callMethod<I extends ContractInstance, F extends Fields, A extends Arguments, R>(contract: ContractFactory<I, F>, instance: ContractInstance, methodName: string, params: Optional<CallContractParams<A>, 'args'>, getContractByCodeHash?: (codeHash: string) => Contract): Promise<CallContractResult<R>>;
|
|
305
|
+
export declare function multicallMethods<I extends ContractInstance, F extends Fields>(contract: ContractFactory<I, F>, instance: ContractInstance, calls: Record<string, Optional<CallContractParams<any>, 'args'>>, getContractByCodeHash?: (codeHash: string) => Contract): Promise<Record<string, CallContractResult<any>>>;
|
|
301
306
|
export declare function getContractEventsCurrentCount(contractAddress: Address): Promise<number>;
|
|
302
307
|
export {};
|
|
@@ -43,9 +43,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
43
43
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
44
44
|
};
|
|
45
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
-
exports.getContractEventsCurrentCount = exports.multicallMethods = exports.callMethod = exports.subscribeContractEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.testMethod = exports.addStdIdToFields = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.DestroyContractEventAddress = exports.CreateContractEventAddress = exports.ContractFactory = exports.randomTxId = exports.toApiVals = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = exports.StdIdFieldName = void 0;
|
|
46
|
+
exports.getContractEventsCurrentCount = exports.multicallMethods = exports.callMethod = exports.subscribeContractEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.testMethod = exports.addStdIdToFields = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.DestroyContractEventAddress = exports.CreateContractEventAddress = exports.ExecutableScript = exports.ContractFactory = exports.randomTxId = exports.toApiVals = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = exports.StdIdFieldName = void 0;
|
|
47
47
|
const buffer_1 = require("buffer/");
|
|
48
|
-
const crypto_1 = require("crypto");
|
|
49
48
|
const fs_1 = __importDefault(require("fs"));
|
|
50
49
|
const fs_2 = require("fs");
|
|
51
50
|
const api_1 = require("../api");
|
|
@@ -55,6 +54,7 @@ const global_1 = require("../global");
|
|
|
55
54
|
const path = __importStar(require("path"));
|
|
56
55
|
const events_1 = require("./events");
|
|
57
56
|
const constants_1 = require("../constants");
|
|
57
|
+
const crypto = new utils_1.WebCrypto();
|
|
58
58
|
exports.StdIdFieldName = '__stdInterfaceId';
|
|
59
59
|
var SourceKind;
|
|
60
60
|
(function (SourceKind) {
|
|
@@ -112,7 +112,7 @@ class SourceInfo {
|
|
|
112
112
|
this.isExternal = isExternal;
|
|
113
113
|
}
|
|
114
114
|
static async from(type, name, sourceCode, contractRelativePath, isExternal) {
|
|
115
|
-
const sourceCodeHash = await
|
|
115
|
+
const sourceCodeHash = await crypto.subtle.digest('SHA-256', buffer_1.Buffer.from(sourceCode));
|
|
116
116
|
const sourceCodeHashHex = buffer_1.Buffer.from(sourceCodeHash).toString('hex');
|
|
117
117
|
return new SourceInfo(type, name, sourceCode, sourceCodeHashHex, contractRelativePath, isExternal);
|
|
118
118
|
}
|
|
@@ -553,7 +553,7 @@ class Contract extends Artifact {
|
|
|
553
553
|
// no need to be cryptographically strong random
|
|
554
554
|
static randomAddress() {
|
|
555
555
|
const bytes = new Uint8Array(33);
|
|
556
|
-
|
|
556
|
+
crypto.getRandomValues(bytes);
|
|
557
557
|
bytes[0] = 3;
|
|
558
558
|
return utils_1.bs58.encode(bytes);
|
|
559
559
|
}
|
|
@@ -620,11 +620,13 @@ class Contract extends Artifact {
|
|
|
620
620
|
asset: fromApiAsset(state.asset)
|
|
621
621
|
};
|
|
622
622
|
}
|
|
623
|
-
static fromApiContractState(state) {
|
|
624
|
-
const contract =
|
|
623
|
+
static fromApiContractState(state, getContractByCodeHash) {
|
|
624
|
+
const contract = getContractByCodeHash
|
|
625
|
+
? getContractByCodeHash(state.codeHash)
|
|
626
|
+
: Project.currentProject.contractByCodeHash(state.codeHash);
|
|
625
627
|
return contract.fromApiContractState(state);
|
|
626
628
|
}
|
|
627
|
-
static fromApiEvent(event, codeHash, txId) {
|
|
629
|
+
static fromApiEvent(event, codeHash, txId, getContractByCodeHash) {
|
|
628
630
|
let fields;
|
|
629
631
|
let name;
|
|
630
632
|
if (event.eventIndex == Contract.ContractCreatedEventIndex) {
|
|
@@ -636,7 +638,9 @@ class Contract extends Artifact {
|
|
|
636
638
|
name = Contract.ContractDestroyedEvent.name;
|
|
637
639
|
}
|
|
638
640
|
else {
|
|
639
|
-
const contract =
|
|
641
|
+
const contract = getContractByCodeHash
|
|
642
|
+
? getContractByCodeHash(codeHash)
|
|
643
|
+
: Project.currentProject.contractByCodeHash(codeHash);
|
|
640
644
|
const eventSig = contract.eventsSig[event.eventIndex];
|
|
641
645
|
fields = fromApiEventFields(event.fields, eventSig);
|
|
642
646
|
name = eventSig.name;
|
|
@@ -688,12 +692,12 @@ class Contract extends Artifact {
|
|
|
688
692
|
buildByteCodeToDeploy(initialFields) {
|
|
689
693
|
return ralph.buildContractByteCode(this.bytecode, initialFields, this.fieldsSig);
|
|
690
694
|
}
|
|
691
|
-
static fromApiEvents(events, addressToCodeHash, txId) {
|
|
695
|
+
static fromApiEvents(events, addressToCodeHash, txId, getContractByCodeHash) {
|
|
692
696
|
return events.map((event) => {
|
|
693
697
|
const contractAddress = event.contractAddress;
|
|
694
698
|
const codeHash = addressToCodeHash.get(contractAddress);
|
|
695
699
|
if (typeof codeHash !== 'undefined' || event.eventIndex < 0) {
|
|
696
|
-
return Contract.fromApiEvent(event, codeHash, txId);
|
|
700
|
+
return Contract.fromApiEvent(event, codeHash, txId, getContractByCodeHash);
|
|
697
701
|
}
|
|
698
702
|
else {
|
|
699
703
|
throw Error(`Cannot find codeHash for the contract address: ${contractAddress}`);
|
|
@@ -711,7 +715,7 @@ class Contract extends Artifact {
|
|
|
711
715
|
args: args
|
|
712
716
|
};
|
|
713
717
|
}
|
|
714
|
-
fromApiCallContractResult(result, txId, methodIndex) {
|
|
718
|
+
fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash) {
|
|
715
719
|
const returnTypes = this.functions[`${methodIndex}`].returnTypes;
|
|
716
720
|
const rawReturn = (0, api_1.fromApiArray)(result.returns, returnTypes);
|
|
717
721
|
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn;
|
|
@@ -720,10 +724,10 @@ class Contract extends Artifact {
|
|
|
720
724
|
return {
|
|
721
725
|
returns: returns,
|
|
722
726
|
gasUsed: result.gasUsed,
|
|
723
|
-
contracts: result.contracts.map((state) => Contract.fromApiContractState(state)),
|
|
727
|
+
contracts: result.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
|
|
724
728
|
txInputs: result.txInputs,
|
|
725
729
|
txOutputs: result.txOutputs.map((output) => fromApiOutput(output)),
|
|
726
|
-
events: Contract.fromApiEvents(result.events, addressToCodeHash, txId)
|
|
730
|
+
events: Contract.fromApiEvents(result.events, addressToCodeHash, txId, getContractByCodeHash)
|
|
727
731
|
};
|
|
728
732
|
}
|
|
729
733
|
}
|
|
@@ -901,7 +905,7 @@ function fromApiOutput(output) {
|
|
|
901
905
|
}
|
|
902
906
|
function randomTxId() {
|
|
903
907
|
const bytes = new Uint8Array(32);
|
|
904
|
-
|
|
908
|
+
crypto.getRandomValues(bytes);
|
|
905
909
|
return (0, utils_1.binToHex)(bytes);
|
|
906
910
|
}
|
|
907
911
|
exports.randomTxId = randomTxId;
|
|
@@ -931,6 +935,16 @@ class ContractFactory {
|
|
|
931
935
|
}
|
|
932
936
|
}
|
|
933
937
|
exports.ContractFactory = ContractFactory;
|
|
938
|
+
class ExecutableScript {
|
|
939
|
+
constructor(script) {
|
|
940
|
+
this.script = script;
|
|
941
|
+
}
|
|
942
|
+
async execute(signer, params) {
|
|
943
|
+
const signerParams = await this.script.txParamsForExecution(signer, params);
|
|
944
|
+
return await signer.signAndSubmitExecuteScriptTx(signerParams);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
exports.ExecutableScript = ExecutableScript;
|
|
934
948
|
function specialContractAddress(n) {
|
|
935
949
|
const bytes = new Uint8Array(32).fill(0);
|
|
936
950
|
bytes[31] = n;
|
|
@@ -1094,16 +1108,16 @@ function subscribeContractEvents(contract, instance, options, fromCount) {
|
|
|
1094
1108
|
return (0, events_1.subscribeToEvents)(opt, instance.address, fromCount);
|
|
1095
1109
|
}
|
|
1096
1110
|
exports.subscribeContractEvents = subscribeContractEvents;
|
|
1097
|
-
async function callMethod(contract, instance, methodName, params) {
|
|
1111
|
+
async function callMethod(contract, instance, methodName, params, getContractByCodeHash) {
|
|
1098
1112
|
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1099
1113
|
const txId = params?.txId ?? randomTxId();
|
|
1100
1114
|
const callParams = contract.contract.toApiCallContract({ ...params, txId: txId, args: params.args === undefined ? {} : params.args }, instance.groupIndex, instance.address, methodIndex);
|
|
1101
1115
|
const result = await (0, global_1.getCurrentNodeProvider)().contracts.postContractsCallContract(callParams);
|
|
1102
|
-
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex);
|
|
1116
|
+
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash);
|
|
1103
1117
|
return callResult;
|
|
1104
1118
|
}
|
|
1105
1119
|
exports.callMethod = callMethod;
|
|
1106
|
-
async function multicallMethods(contract, instance, calls) {
|
|
1120
|
+
async function multicallMethods(contract, instance, calls, getContractByCodeHash) {
|
|
1107
1121
|
const callEntries = Object.entries(calls);
|
|
1108
1122
|
const callsParams = callEntries.map((entry) => {
|
|
1109
1123
|
const [methodName, params] = entry;
|
|
@@ -1117,7 +1131,7 @@ async function multicallMethods(contract, instance, calls) {
|
|
|
1117
1131
|
const methodIndex = call.methodIndex;
|
|
1118
1132
|
const callResult = result.results[`${methodIndex}`];
|
|
1119
1133
|
const methodName = callEntries[`${index}`][`0`];
|
|
1120
|
-
callsResult[`${methodName}`] = contract.contract.fromApiCallContractResult(callResult, call.txId, methodIndex);
|
|
1134
|
+
callsResult[`${methodName}`] = contract.contract.fromApiCallContractResult(callResult, call.txId, methodIndex, getContractByCodeHash);
|
|
1121
1135
|
});
|
|
1122
1136
|
return callsResult;
|
|
1123
1137
|
}
|
package/dist/src/utils/index.js
CHANGED
|
@@ -31,6 +31,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
31
31
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
32
32
|
};
|
|
33
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
__exportStar(require("./webcrypto"), exports);
|
|
34
35
|
__exportStar(require("./address"), exports);
|
|
35
36
|
__exportStar(require("./bs58"), exports);
|
|
36
37
|
__exportStar(require("./djb2"), exports);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
4
|
+
This file is part of the alephium project.
|
|
5
|
+
|
|
6
|
+
The library is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
The library is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU Lesser General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.WebCrypto = void 0;
|
|
21
|
+
const buffer_1 = require("buffer/");
|
|
22
|
+
const crypto_1 = require("crypto");
|
|
23
|
+
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
24
|
+
class WebCrypto {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.subtle = isBrowser ? globalThis.crypto.subtle : crypto_1.webcrypto.subtle;
|
|
27
|
+
}
|
|
28
|
+
getRandomValues(array) {
|
|
29
|
+
if (!ArrayBuffer.isView(array)) {
|
|
30
|
+
throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'");
|
|
31
|
+
}
|
|
32
|
+
const buffer = buffer_1.Buffer.from(array.buffer, array.byteOffset, array.byteLength);
|
|
33
|
+
if (isBrowser) {
|
|
34
|
+
globalThis.crypto.getRandomValues(buffer);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
(0, crypto_1.randomFillSync)(buffer);
|
|
38
|
+
}
|
|
39
|
+
return array;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.WebCrypto = WebCrypto;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.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",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"author": "Alephium dev <dev@alephium.org>",
|
|
29
29
|
"config": {
|
|
30
|
-
"alephium_version": "2.3.
|
|
30
|
+
"alephium_version": "2.3.3",
|
|
31
31
|
"explorer_backend_version": "1.13.5"
|
|
32
32
|
},
|
|
33
33
|
"type": "commonjs",
|
|
@@ -40,8 +40,10 @@
|
|
|
40
40
|
"bn.js": "5.2.1",
|
|
41
41
|
"buffer": "^6.0.3",
|
|
42
42
|
"cross-fetch": "^3.1.5",
|
|
43
|
+
"crypto-browserify": "^3.12.0",
|
|
43
44
|
"elliptic": "6.5.4",
|
|
44
|
-
"eventemitter3": "^4.0.7"
|
|
45
|
+
"eventemitter3": "^4.0.7",
|
|
46
|
+
"path-browserify": "^1.0.1"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
47
49
|
"@babel/eslint-parser": "^7.21.3",
|
|
@@ -55,7 +57,6 @@
|
|
|
55
57
|
"@typescript-eslint/eslint-plugin": "^5.57.0",
|
|
56
58
|
"@typescript-eslint/parser": "^5.57.0",
|
|
57
59
|
"clean-webpack-plugin": "4.0.0",
|
|
58
|
-
"crypto-browserify": "^3.12.0",
|
|
59
60
|
"eslint": "^8.37.0",
|
|
60
61
|
"eslint-config-prettier": "^8.8.0",
|
|
61
62
|
"eslint-plugin-header": "^3.1.1",
|
|
@@ -67,7 +68,6 @@
|
|
|
67
68
|
"jest-websocket-mock": "^2.4.0",
|
|
68
69
|
"mock-fs": "^5.2.0",
|
|
69
70
|
"mock-socket": "^9.2.1",
|
|
70
|
-
"path-browserify": "^1.0.1",
|
|
71
71
|
"prettier": "^2.8.7",
|
|
72
72
|
"process": "^0.11.10",
|
|
73
73
|
"rewire": "^6.0.0",
|
package/src/api/api-alephium.ts
CHANGED
|
@@ -1228,7 +1228,7 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
1228
1228
|
|
|
1229
1229
|
/**
|
|
1230
1230
|
* @title Alephium API
|
|
1231
|
-
* @version 2.3.
|
|
1231
|
+
* @version 2.3.3
|
|
1232
1232
|
* @baseUrl ../
|
|
1233
1233
|
*/
|
|
1234
1234
|
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
package/src/contract/contract.ts
CHANGED
|
@@ -17,7 +17,6 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { Buffer } from 'buffer/'
|
|
20
|
-
import { webcrypto as crypto } from 'crypto'
|
|
21
20
|
import fs from 'fs'
|
|
22
21
|
import { promises as fsPromises } from 'fs'
|
|
23
22
|
import {
|
|
@@ -54,13 +53,16 @@ import {
|
|
|
54
53
|
Eq,
|
|
55
54
|
Optional,
|
|
56
55
|
groupOfAddress,
|
|
57
|
-
addressFromContractId
|
|
56
|
+
addressFromContractId,
|
|
57
|
+
WebCrypto
|
|
58
58
|
} from '../utils'
|
|
59
59
|
import { getCurrentNodeProvider } from '../global'
|
|
60
60
|
import * as path from 'path'
|
|
61
61
|
import { EventSubscription, subscribeToEvents } from './events'
|
|
62
62
|
import { ONE_ALPH } from '../constants'
|
|
63
63
|
|
|
64
|
+
const crypto = new WebCrypto()
|
|
65
|
+
|
|
64
66
|
export type FieldsSig = node.FieldsSig
|
|
65
67
|
export type EventSig = node.EventSig
|
|
66
68
|
export type FunctionSig = node.FunctionSig
|
|
@@ -920,8 +922,13 @@ export class Contract extends Artifact {
|
|
|
920
922
|
}
|
|
921
923
|
}
|
|
922
924
|
|
|
923
|
-
static fromApiContractState(
|
|
924
|
-
|
|
925
|
+
static fromApiContractState(
|
|
926
|
+
state: node.ContractState,
|
|
927
|
+
getContractByCodeHash?: (codeHash: string) => Contract
|
|
928
|
+
): ContractState {
|
|
929
|
+
const contract = getContractByCodeHash
|
|
930
|
+
? getContractByCodeHash(state.codeHash)
|
|
931
|
+
: Project.currentProject.contractByCodeHash(state.codeHash)
|
|
925
932
|
return contract.fromApiContractState(state)
|
|
926
933
|
}
|
|
927
934
|
|
|
@@ -939,7 +946,12 @@ export class Contract extends Artifact {
|
|
|
939
946
|
fieldTypes: ['Address']
|
|
940
947
|
}
|
|
941
948
|
|
|
942
|
-
static fromApiEvent(
|
|
949
|
+
static fromApiEvent(
|
|
950
|
+
event: node.ContractEventByTxId,
|
|
951
|
+
codeHash: string | undefined,
|
|
952
|
+
txId: string,
|
|
953
|
+
getContractByCodeHash?: (codeHash: string) => Contract
|
|
954
|
+
): ContractEvent {
|
|
943
955
|
let fields: Fields
|
|
944
956
|
let name: string
|
|
945
957
|
|
|
@@ -950,7 +962,9 @@ export class Contract extends Artifact {
|
|
|
950
962
|
fields = fromApiEventFields(event.fields, Contract.ContractDestroyedEvent, true)
|
|
951
963
|
name = Contract.ContractDestroyedEvent.name
|
|
952
964
|
} else {
|
|
953
|
-
const contract =
|
|
965
|
+
const contract = getContractByCodeHash
|
|
966
|
+
? getContractByCodeHash(codeHash!)
|
|
967
|
+
: Project.currentProject.contractByCodeHash(codeHash!)
|
|
954
968
|
const eventSig = contract.eventsSig[event.eventIndex]
|
|
955
969
|
fields = fromApiEventFields(event.fields, eventSig)
|
|
956
970
|
name = eventSig.name
|
|
@@ -1018,13 +1032,14 @@ export class Contract extends Artifact {
|
|
|
1018
1032
|
static fromApiEvents(
|
|
1019
1033
|
events: node.ContractEventByTxId[],
|
|
1020
1034
|
addressToCodeHash: Map<string, string>,
|
|
1021
|
-
txId: string
|
|
1035
|
+
txId: string,
|
|
1036
|
+
getContractByCodeHash?: (codeHash: string) => Contract
|
|
1022
1037
|
): ContractEvent[] {
|
|
1023
1038
|
return events.map((event) => {
|
|
1024
1039
|
const contractAddress = event.contractAddress
|
|
1025
1040
|
const codeHash = addressToCodeHash.get(contractAddress)
|
|
1026
1041
|
if (typeof codeHash !== 'undefined' || event.eventIndex < 0) {
|
|
1027
|
-
return Contract.fromApiEvent(event, codeHash, txId)
|
|
1042
|
+
return Contract.fromApiEvent(event, codeHash, txId, getContractByCodeHash)
|
|
1028
1043
|
} else {
|
|
1029
1044
|
throw Error(`Cannot find codeHash for the contract address: ${contractAddress}`)
|
|
1030
1045
|
}
|
|
@@ -1051,7 +1066,8 @@ export class Contract extends Artifact {
|
|
|
1051
1066
|
fromApiCallContractResult(
|
|
1052
1067
|
result: node.CallContractResult,
|
|
1053
1068
|
txId: string,
|
|
1054
|
-
methodIndex: number
|
|
1069
|
+
methodIndex: number,
|
|
1070
|
+
getContractByCodeHash?: (codeHash: string) => Contract
|
|
1055
1071
|
): CallContractResult<unknown> {
|
|
1056
1072
|
const returnTypes = this.functions[`${methodIndex}`].returnTypes
|
|
1057
1073
|
const rawReturn = fromApiArray(result.returns, returnTypes)
|
|
@@ -1062,10 +1078,10 @@ export class Contract extends Artifact {
|
|
|
1062
1078
|
return {
|
|
1063
1079
|
returns: returns,
|
|
1064
1080
|
gasUsed: result.gasUsed,
|
|
1065
|
-
contracts: result.contracts.map((state) => Contract.fromApiContractState(state)),
|
|
1081
|
+
contracts: result.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
|
|
1066
1082
|
txInputs: result.txInputs,
|
|
1067
1083
|
txOutputs: result.txOutputs.map((output) => fromApiOutput(output)),
|
|
1068
|
-
events: Contract.fromApiEvents(result.events, addressToCodeHash, txId)
|
|
1084
|
+
events: Contract.fromApiEvents(result.events, addressToCodeHash, txId, getContractByCodeHash)
|
|
1069
1085
|
}
|
|
1070
1086
|
}
|
|
1071
1087
|
}
|
|
@@ -1402,6 +1418,19 @@ export abstract class ContractFactory<I extends ContractInstance, F extends Fiel
|
|
|
1402
1418
|
}
|
|
1403
1419
|
}
|
|
1404
1420
|
|
|
1421
|
+
export class ExecutableScript<P extends Fields = Fields> {
|
|
1422
|
+
readonly script: Script
|
|
1423
|
+
|
|
1424
|
+
constructor(script: Script) {
|
|
1425
|
+
this.script = script
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
async execute(signer: SignerProvider, params: ExecuteScriptParams<P>): Promise<ExecuteScriptResult> {
|
|
1429
|
+
const signerParams = await this.script.txParamsForExecution(signer, params)
|
|
1430
|
+
return await signer.signAndSubmitExecuteScriptTx(signerParams)
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1405
1434
|
export interface ExecuteScriptParams<P extends Fields = Fields> {
|
|
1406
1435
|
initialFields: P
|
|
1407
1436
|
attoAlphAmount?: Number256
|
|
@@ -1683,7 +1712,8 @@ export async function callMethod<I extends ContractInstance, F extends Fields, A
|
|
|
1683
1712
|
contract: ContractFactory<I, F>,
|
|
1684
1713
|
instance: ContractInstance,
|
|
1685
1714
|
methodName: string,
|
|
1686
|
-
params: Optional<CallContractParams<A>, 'args'
|
|
1715
|
+
params: Optional<CallContractParams<A>, 'args'>,
|
|
1716
|
+
getContractByCodeHash?: (codeHash: string) => Contract
|
|
1687
1717
|
): Promise<CallContractResult<R>> {
|
|
1688
1718
|
const methodIndex = contract.contract.getMethodIndex(methodName)
|
|
1689
1719
|
const txId = params?.txId ?? randomTxId()
|
|
@@ -1694,14 +1724,15 @@ export async function callMethod<I extends ContractInstance, F extends Fields, A
|
|
|
1694
1724
|
methodIndex
|
|
1695
1725
|
)
|
|
1696
1726
|
const result = await getCurrentNodeProvider().contracts.postContractsCallContract(callParams)
|
|
1697
|
-
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex)
|
|
1727
|
+
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash)
|
|
1698
1728
|
return callResult as CallContractResult<R>
|
|
1699
1729
|
}
|
|
1700
1730
|
|
|
1701
1731
|
export async function multicallMethods<I extends ContractInstance, F extends Fields>(
|
|
1702
1732
|
contract: ContractFactory<I, F>,
|
|
1703
1733
|
instance: ContractInstance,
|
|
1704
|
-
calls: Record<string, Optional<CallContractParams<any>, 'args'
|
|
1734
|
+
calls: Record<string, Optional<CallContractParams<any>, 'args'>>,
|
|
1735
|
+
getContractByCodeHash?: (codeHash: string) => Contract
|
|
1705
1736
|
): Promise<Record<string, CallContractResult<any>>> {
|
|
1706
1737
|
const callEntries = Object.entries(calls)
|
|
1707
1738
|
const callsParams = callEntries.map((entry) => {
|
|
@@ -1724,7 +1755,8 @@ export async function multicallMethods<I extends ContractInstance, F extends Fie
|
|
|
1724
1755
|
callsResult[`${methodName}`] = contract.contract.fromApiCallContractResult(
|
|
1725
1756
|
callResult,
|
|
1726
1757
|
call.txId!,
|
|
1727
|
-
methodIndex
|
|
1758
|
+
methodIndex,
|
|
1759
|
+
getContractByCodeHash
|
|
1728
1760
|
) as CallContractResult<any>
|
|
1729
1761
|
})
|
|
1730
1762
|
return callsResult
|
package/src/utils/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ 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 './webcrypto'
|
|
19
20
|
export * from './address'
|
|
20
21
|
export * from './bs58'
|
|
21
22
|
export * from './djb2'
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { Buffer } from 'buffer/'
|
|
20
|
+
import { webcrypto, randomFillSync } from 'crypto'
|
|
21
|
+
|
|
22
|
+
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'
|
|
23
|
+
|
|
24
|
+
export class WebCrypto {
|
|
25
|
+
subtle = isBrowser ? globalThis.crypto.subtle : webcrypto.subtle
|
|
26
|
+
|
|
27
|
+
public getRandomValues<T extends ArrayBufferView | null>(array: T): T {
|
|
28
|
+
if (!ArrayBuffer.isView(array)) {
|
|
29
|
+
throw new TypeError(
|
|
30
|
+
"Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'"
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
const buffer = Buffer.from(array.buffer, array.byteOffset, array.byteLength)
|
|
34
|
+
|
|
35
|
+
if (isBrowser) {
|
|
36
|
+
globalThis.crypto.getRandomValues(buffer)
|
|
37
|
+
} else {
|
|
38
|
+
randomFillSync(buffer)
|
|
39
|
+
}
|
|
40
|
+
return array
|
|
41
|
+
}
|
|
42
|
+
}
|