@alephium/web3 0.5.0-rc.0 → 0.5.0-rc.10
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.LICENSE.txt +2 -0
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +57 -20
- package/dist/src/api/api-alephium.js +57 -15
- package/dist/src/api/index.d.ts +1 -0
- package/dist/src/api/index.js +1 -0
- package/dist/src/api/types.d.ts +1 -1
- package/dist/src/api/types.js +6 -2
- package/dist/src/constants.d.ts +1 -0
- package/dist/src/constants.js +2 -1
- package/dist/src/contract/contract.d.ts +37 -17
- package/dist/src/contract/contract.js +167 -19
- package/dist/src/signer/signer.d.ts +29 -30
- package/dist/src/signer/signer.js +34 -25
- package/dist/src/signer/tx-builder.d.ts +2 -7
- package/dist/src/signer/tx-builder.js +10 -7
- package/dist/src/signer/types.d.ts +8 -0
- package/dist/src/transaction/sign-verify.d.ts +3 -2
- package/dist/src/transaction/sign-verify.js +4 -14
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/sign.d.ts +3 -0
- package/dist/src/utils/sign.js +89 -0
- package/dist/src/utils/utils.d.ts +5 -3
- package/dist/src/utils/utils.js +25 -10
- package/package.json +3 -2
- package/src/api/api-alephium.ts +88 -32
- package/src/api/index.ts +2 -0
- package/src/api/types.ts +12 -2
- package/src/constants.ts +1 -0
- package/src/contract/contract.ts +288 -38
- package/src/signer/signer.ts +69 -55
- package/src/signer/tx-builder.ts +13 -7
- package/src/signer/types.ts +10 -2
- package/src/transaction/sign-verify.ts +10 -15
- package/src/utils/index.ts +1 -0
- package/src/utils/sign.ts +66 -0
- package/src/utils/utils.ts +27 -10
|
@@ -43,7 +43,7 @@ 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.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.ContractFactory = exports.randomTxId = exports.toApiVals = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = void 0;
|
|
46
|
+
exports.callMethod = exports.subscribeAllEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.testMethod = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.ContractFactory = exports.randomTxId = exports.toApiVals = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = void 0;
|
|
47
47
|
const buffer_1 = require("buffer/");
|
|
48
48
|
const crypto_1 = require("crypto");
|
|
49
49
|
const fs_1 = __importDefault(require("fs"));
|
|
@@ -54,6 +54,7 @@ const utils_1 = require("../utils");
|
|
|
54
54
|
const global_1 = require("../global");
|
|
55
55
|
const path = __importStar(require("path"));
|
|
56
56
|
const events_1 = require("./events");
|
|
57
|
+
const constants_1 = require("../constants");
|
|
57
58
|
var SourceKind;
|
|
58
59
|
(function (SourceKind) {
|
|
59
60
|
SourceKind[SourceKind["Contract"] = 0] = "Contract";
|
|
@@ -578,34 +579,43 @@ class Contract extends Artifact {
|
|
|
578
579
|
return contract.fromApiContractState(state);
|
|
579
580
|
}
|
|
580
581
|
static fromApiEvent(event, codeHash, txId) {
|
|
581
|
-
let
|
|
582
|
+
let fields;
|
|
583
|
+
let name;
|
|
582
584
|
if (event.eventIndex == Contract.ContractCreatedEventIndex) {
|
|
583
|
-
|
|
585
|
+
fields = fromApiSystemEventFields(event.fields, Contract.ContractCreatedEvent);
|
|
586
|
+
name = Contract.ContractCreatedEvent.name;
|
|
584
587
|
}
|
|
585
588
|
else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
|
|
586
|
-
|
|
589
|
+
fields = fromApiSystemEventFields(event.fields, Contract.ContractDestroyedEvent);
|
|
590
|
+
name = Contract.ContractDestroyedEvent.name;
|
|
587
591
|
}
|
|
588
592
|
else {
|
|
589
593
|
const contract = Project.currentProject.contractByCodeHash(codeHash);
|
|
590
|
-
eventSig = contract.eventsSig[event.eventIndex];
|
|
594
|
+
const eventSig = contract.eventsSig[event.eventIndex];
|
|
595
|
+
fields = fromApiEventFields(event.fields, eventSig);
|
|
596
|
+
name = eventSig.name;
|
|
591
597
|
}
|
|
592
598
|
return {
|
|
593
599
|
txId: txId,
|
|
594
600
|
blockHash: event.blockHash,
|
|
595
601
|
contractAddress: event.contractAddress,
|
|
596
|
-
name:
|
|
602
|
+
name: name,
|
|
597
603
|
eventIndex: event.eventIndex,
|
|
598
|
-
fields:
|
|
604
|
+
fields: fields
|
|
599
605
|
};
|
|
600
606
|
}
|
|
601
|
-
fromApiTestContractResult(
|
|
607
|
+
fromApiTestContractResult(methodName, result, txId) {
|
|
608
|
+
const methodIndex = this.functions.findIndex((sig) => sig.name === methodName);
|
|
609
|
+
const returnTypes = this.functions[`${methodIndex}`].returnTypes;
|
|
610
|
+
const rawReturn = (0, api_1.fromApiArray)(result.returns, returnTypes);
|
|
611
|
+
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn;
|
|
602
612
|
const addressToCodeHash = new Map();
|
|
603
613
|
addressToCodeHash.set(result.address, result.codeHash);
|
|
604
614
|
result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
605
615
|
return {
|
|
606
616
|
contractId: (0, utils_1.binToHex)((0, utils_1.contractIdFromAddress)(result.address)),
|
|
607
617
|
contractAddress: result.address,
|
|
608
|
-
returns:
|
|
618
|
+
returns: returns,
|
|
609
619
|
gasUsed: result.gasUsed,
|
|
610
620
|
contracts: result.contracts.map((contract) => Contract.fromApiContractState(contract)),
|
|
611
621
|
txOutputs: result.txOutputs.map(fromApiOutput),
|
|
@@ -615,8 +625,10 @@ class Contract extends Artifact {
|
|
|
615
625
|
}
|
|
616
626
|
async txParamsForDeployment(signer, params) {
|
|
617
627
|
const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {});
|
|
628
|
+
const selectedAccount = await signer.getSelectedAccount();
|
|
618
629
|
const signerParams = {
|
|
619
|
-
signerAddress:
|
|
630
|
+
signerAddress: selectedAccount.address,
|
|
631
|
+
signerKeyType: selectedAccount.keyType,
|
|
620
632
|
bytecode: bytecode,
|
|
621
633
|
initialAttoAlphAmount: params?.initialAttoAlphAmount,
|
|
622
634
|
issueTokenAmount: params?.issueTokenAmount,
|
|
@@ -653,10 +665,11 @@ class Contract extends Artifact {
|
|
|
653
665
|
};
|
|
654
666
|
}
|
|
655
667
|
fromApiCallContractResult(result, txId, methodIndex) {
|
|
668
|
+
const returnTypes = this.functions[`${methodIndex}`].returnTypes;
|
|
669
|
+
const rawReturn = (0, api_1.fromApiArray)(result.returns, returnTypes);
|
|
670
|
+
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn;
|
|
656
671
|
const addressToCodeHash = new Map();
|
|
657
672
|
result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
658
|
-
const functionSig = this.functions[`${methodIndex}`];
|
|
659
|
-
const returns = (0, api_1.fromApiArray)(result.returns, functionSig.returnTypes);
|
|
660
673
|
return {
|
|
661
674
|
returns: returns,
|
|
662
675
|
gasUsed: result.gasUsed,
|
|
@@ -672,7 +685,9 @@ Contract.ContractCreatedEventIndex = -1;
|
|
|
672
685
|
Contract.ContractCreatedEvent = {
|
|
673
686
|
name: 'ContractCreated',
|
|
674
687
|
fieldNames: ['address'],
|
|
675
|
-
fieldTypes: ['Address']
|
|
688
|
+
fieldTypes: ['Address'],
|
|
689
|
+
optionalFieldNames: ['parentAddress'],
|
|
690
|
+
optionalFieldTypes: ['Address']
|
|
676
691
|
};
|
|
677
692
|
Contract.ContractDestroyedEventIndex = -2;
|
|
678
693
|
Contract.ContractDestroyedEvent = {
|
|
@@ -717,8 +732,10 @@ class Script extends Artifact {
|
|
|
717
732
|
return JSON.stringify(object, null, 2);
|
|
718
733
|
}
|
|
719
734
|
async txParamsForExecution(signer, params) {
|
|
735
|
+
const selectedAccount = await signer.getSelectedAccount();
|
|
720
736
|
const signerParams = {
|
|
721
|
-
signerAddress:
|
|
737
|
+
signerAddress: selectedAccount.address,
|
|
738
|
+
signerKeyType: selectedAccount.keyType,
|
|
722
739
|
bytecode: this.buildByteCodeToDeploy(params.initialFields ?? {}),
|
|
723
740
|
attoAlphAmount: params.attoAlphAmount,
|
|
724
741
|
tokens: params.tokens,
|
|
@@ -752,6 +769,9 @@ function fromApiFields(immFields, mutFields, fieldsSig) {
|
|
|
752
769
|
function fromApiEventFields(vals, eventSig) {
|
|
753
770
|
return (0, api_1.fromApiVals)(vals, eventSig.fieldNames, eventSig.fieldTypes);
|
|
754
771
|
}
|
|
772
|
+
function fromApiSystemEventFields(vals, systemEventSig) {
|
|
773
|
+
return (0, api_1.fromApiVals)(vals, systemEventSig.fieldNames, systemEventSig.fieldTypes, systemEventSig.optionalFieldNames ?? [], systemEventSig.optionalFieldTypes ?? []);
|
|
774
|
+
}
|
|
755
775
|
function toApiAsset(asset) {
|
|
756
776
|
return {
|
|
757
777
|
attoAlphAmount: (0, api_1.toApiNumber256)(asset.alphAmount),
|
|
@@ -856,27 +876,38 @@ class ContractFactory {
|
|
|
856
876
|
instance: this.at(result.contractAddress)
|
|
857
877
|
};
|
|
858
878
|
}
|
|
879
|
+
// This is used for testing contract functions
|
|
880
|
+
stateForTest(initFields, asset, address) {
|
|
881
|
+
const newAsset = {
|
|
882
|
+
alphAmount: asset?.alphAmount ?? constants_1.ONE_ALPH,
|
|
883
|
+
tokens: asset?.tokens
|
|
884
|
+
};
|
|
885
|
+
return this.contract.toState(initFields, newAsset, address);
|
|
886
|
+
}
|
|
859
887
|
}
|
|
860
888
|
exports.ContractFactory = ContractFactory;
|
|
861
|
-
function
|
|
889
|
+
function decodeSystemEvent(event, systemEventSig, eventIndex) {
|
|
862
890
|
if (event.eventIndex !== eventIndex) {
|
|
863
891
|
throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`);
|
|
864
892
|
}
|
|
865
|
-
return (
|
|
893
|
+
return fromApiSystemEventFields(event.fields, systemEventSig);
|
|
866
894
|
}
|
|
867
895
|
function decodeContractCreatedEvent(event) {
|
|
868
|
-
const fields =
|
|
896
|
+
const fields = decodeSystemEvent(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
|
|
869
897
|
return {
|
|
870
898
|
blockHash: event.blockHash,
|
|
871
899
|
txId: event.txId,
|
|
872
900
|
eventIndex: event.eventIndex,
|
|
873
901
|
name: Contract.ContractCreatedEvent.name,
|
|
874
|
-
fields: {
|
|
902
|
+
fields: {
|
|
903
|
+
address: fields['address'],
|
|
904
|
+
parentAddress: fields['parentAddress'] === undefined ? undefined : fields['parentAddress']
|
|
905
|
+
}
|
|
875
906
|
};
|
|
876
907
|
}
|
|
877
908
|
exports.decodeContractCreatedEvent = decodeContractCreatedEvent;
|
|
878
909
|
function decodeContractDestroyedEvent(event) {
|
|
879
|
-
const fields =
|
|
910
|
+
const fields = decodeSystemEvent(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
|
|
880
911
|
return {
|
|
881
912
|
blockHash: event.blockHash,
|
|
882
913
|
txId: event.txId,
|
|
@@ -904,3 +935,120 @@ function subscribeEventsFromContract(options, address, eventIndex, decodeFunc, f
|
|
|
904
935
|
return (0, events_1.subscribeToEvents)(opt, address, fromCount);
|
|
905
936
|
}
|
|
906
937
|
exports.subscribeEventsFromContract = subscribeEventsFromContract;
|
|
938
|
+
async function testMethod(contract, methodName, params) {
|
|
939
|
+
const txId = params?.txId ?? randomTxId();
|
|
940
|
+
const apiParams = contract.contract.toApiTestContractParams(methodName, {
|
|
941
|
+
...params,
|
|
942
|
+
txId: txId,
|
|
943
|
+
initialFields: params.initialFields === undefined ? {} : params.initialFields,
|
|
944
|
+
testArgs: params.testArgs === undefined ? {} : params.testArgs
|
|
945
|
+
});
|
|
946
|
+
const apiResult = await (0, global_1.getCurrentNodeProvider)().contracts.postContractsTestContract(apiParams);
|
|
947
|
+
const testResult = contract.contract.fromApiTestContractResult(methodName, apiResult, txId);
|
|
948
|
+
contract.contract.printDebugMessages(methodName, testResult.debugMessages);
|
|
949
|
+
return testResult;
|
|
950
|
+
}
|
|
951
|
+
exports.testMethod = testMethod;
|
|
952
|
+
class ContractInstance {
|
|
953
|
+
constructor(address) {
|
|
954
|
+
this.address = address;
|
|
955
|
+
this.contractId = (0, utils_1.binToHex)((0, utils_1.contractIdFromAddress)(address));
|
|
956
|
+
this.groupIndex = (0, utils_1.groupOfAddress)(address);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
exports.ContractInstance = ContractInstance;
|
|
960
|
+
async function fetchContractState(contract, instance) {
|
|
961
|
+
const contractState = await (0, global_1.getCurrentNodeProvider)().contracts.getContractsAddressState(instance.address, {
|
|
962
|
+
group: instance.groupIndex
|
|
963
|
+
});
|
|
964
|
+
const state = contract.contract.fromApiContractState(contractState);
|
|
965
|
+
return {
|
|
966
|
+
...state,
|
|
967
|
+
fields: state.fields
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
exports.fetchContractState = fetchContractState;
|
|
971
|
+
function subscribeContractCreatedEvent(instance, options, fromCount) {
|
|
972
|
+
return subscribeEventsFromContract(options, instance.address, Contract.ContractCreatedEventIndex, (event) => {
|
|
973
|
+
return {
|
|
974
|
+
...decodeContractCreatedEvent(event),
|
|
975
|
+
contractAddress: instance.address
|
|
976
|
+
};
|
|
977
|
+
}, fromCount);
|
|
978
|
+
}
|
|
979
|
+
exports.subscribeContractCreatedEvent = subscribeContractCreatedEvent;
|
|
980
|
+
function subscribeContractDestroyedEvent(instance, options, fromCount) {
|
|
981
|
+
return subscribeEventsFromContract(options, instance.address, Contract.ContractDestroyedEventIndex, (event) => {
|
|
982
|
+
return {
|
|
983
|
+
...decodeContractDestroyedEvent(event),
|
|
984
|
+
contractAddress: instance.address
|
|
985
|
+
};
|
|
986
|
+
}, fromCount);
|
|
987
|
+
}
|
|
988
|
+
exports.subscribeContractDestroyedEvent = subscribeContractDestroyedEvent;
|
|
989
|
+
function decodeEvent(contract, instance, event, targetEventIndex) {
|
|
990
|
+
if (event.eventIndex !== targetEventIndex &&
|
|
991
|
+
!(targetEventIndex >= 0 && targetEventIndex < contract.eventsSig.length)) {
|
|
992
|
+
throw new Error('Invalid event index: ' + event.eventIndex + ', expected: ' + targetEventIndex);
|
|
993
|
+
}
|
|
994
|
+
const eventSig = contract.eventsSig[`${targetEventIndex}`];
|
|
995
|
+
const fieldNames = eventSig.fieldNames;
|
|
996
|
+
const fieldTypes = eventSig.fieldTypes;
|
|
997
|
+
const fields = (0, api_1.fromApiVals)(event.fields, fieldNames, fieldTypes);
|
|
998
|
+
return {
|
|
999
|
+
contractAddress: instance.address,
|
|
1000
|
+
blockHash: event.blockHash,
|
|
1001
|
+
txId: event.txId,
|
|
1002
|
+
eventIndex: event.eventIndex,
|
|
1003
|
+
name: eventSig.name,
|
|
1004
|
+
fields: fields
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
exports.decodeEvent = decodeEvent;
|
|
1008
|
+
function subscribeContractEvent(contract, instance, options, eventName, fromCount) {
|
|
1009
|
+
const eventIndex = contract.eventsSig.findIndex((sig) => sig.name === eventName);
|
|
1010
|
+
return subscribeEventsFromContract(options, instance.address, eventIndex, (event) => decodeEvent(contract, instance, event, eventIndex), fromCount);
|
|
1011
|
+
}
|
|
1012
|
+
exports.subscribeContractEvent = subscribeContractEvent;
|
|
1013
|
+
function subscribeAllEvents(contract, instance, options, fromCount) {
|
|
1014
|
+
const messageCallback = (event) => {
|
|
1015
|
+
switch (event.eventIndex) {
|
|
1016
|
+
case Contract.ContractCreatedEventIndex: {
|
|
1017
|
+
return options.messageCallback({
|
|
1018
|
+
...decodeContractCreatedEvent(event),
|
|
1019
|
+
contractAddress: instance.address
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
case Contract.ContractDestroyedEventIndex: {
|
|
1023
|
+
return options.messageCallback({
|
|
1024
|
+
...decodeContractDestroyedEvent(event),
|
|
1025
|
+
contractAddress: instance.address
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
default:
|
|
1029
|
+
return options.messageCallback({
|
|
1030
|
+
...decodeEvent(contract, instance, event, event.eventIndex),
|
|
1031
|
+
contractAddress: instance.address
|
|
1032
|
+
});
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
const errorCallback = (err, subscription) => {
|
|
1036
|
+
return options.errorCallback(err, subscription);
|
|
1037
|
+
};
|
|
1038
|
+
const opt = {
|
|
1039
|
+
pollingInterval: options.pollingInterval,
|
|
1040
|
+
messageCallback: messageCallback,
|
|
1041
|
+
errorCallback: errorCallback
|
|
1042
|
+
};
|
|
1043
|
+
return (0, events_1.subscribeToEvents)(opt, instance.address, fromCount);
|
|
1044
|
+
}
|
|
1045
|
+
exports.subscribeAllEvents = subscribeAllEvents;
|
|
1046
|
+
async function callMethod(contract, instance, methodName, params) {
|
|
1047
|
+
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1048
|
+
const txId = params?.txId ?? randomTxId();
|
|
1049
|
+
const callParams = contract.contract.toApiCallContract({ ...params, txId: txId, args: params.args === undefined ? {} : params.args }, instance.groupIndex, instance.address, methodIndex);
|
|
1050
|
+
const result = await (0, global_1.getCurrentNodeProvider)().contracts.postContractsCallContract(callParams);
|
|
1051
|
+
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex);
|
|
1052
|
+
return callResult;
|
|
1053
|
+
}
|
|
1054
|
+
exports.callMethod = callMethod;
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
import { ExplorerProvider, NodeProvider } from '../api';
|
|
2
2
|
import { node } from '../api';
|
|
3
|
-
import { Account,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
get
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
import { Account, EnableOptionsBase, Destination, SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignExecuteScriptTxResult, SignMessageParams, SignMessageResult, SignTransferTxParams, SignTransferTxResult, SignUnsignedTxParams, SignUnsignedTxResult, SubmissionResult, SubmitTransactionParams, ExtSignTransferTxParams, ExtSignDeployContractTxParams, ExtSignExecuteScriptTxParams, ExtSignUnsignedTxParams, ExtSignMessageParams, KeyType } from './types';
|
|
4
|
+
export declare abstract class SignerProvider {
|
|
5
|
+
abstract get nodeProvider(): NodeProvider | undefined;
|
|
6
|
+
abstract get explorerProvider(): ExplorerProvider | undefined;
|
|
7
|
+
protected abstract unsafeGetSelectedAccount(): Promise<Account>;
|
|
8
|
+
getSelectedAccount(): Promise<Account>;
|
|
9
|
+
static validateAccount(account: Account): void;
|
|
10
|
+
abstract signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
|
|
11
|
+
abstract signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
12
|
+
abstract signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
|
|
13
|
+
abstract signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
14
|
+
abstract signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
15
|
+
abstract signMessage(params: SignMessageParams): Promise<SignMessageResult>;
|
|
15
16
|
}
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
export declare abstract class InteractiveSignerProvider<EnableOptions extends EnableOptionsBase = EnableOptionsBase> extends SignerProvider {
|
|
18
|
+
protected abstract unsafeEnable(opt?: EnableOptions): Promise<Account>;
|
|
19
|
+
enable(opt?: EnableOptions): Promise<Account>;
|
|
20
|
+
abstract disconnect(): Promise<void>;
|
|
21
|
+
abstract signAndSubmitTransferTx(params: ExtSignTransferTxParams): Promise<SignTransferTxResult>;
|
|
22
|
+
abstract signAndSubmitDeployContractTx(params: ExtSignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
23
|
+
abstract signAndSubmitExecuteScriptTx(params: ExtSignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
|
|
24
|
+
abstract signAndSubmitUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
25
|
+
abstract signUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>;
|
|
26
|
+
abstract signMessage(params: ExtSignMessageParams): Promise<SignMessageResult>;
|
|
25
27
|
}
|
|
26
|
-
export declare abstract class SignerProviderSimple extends
|
|
27
|
-
abstract get
|
|
28
|
-
abstract getSelectedAccount(): Promise<Account>;
|
|
29
|
-
getSelectedAddress(): Promise<Address>;
|
|
28
|
+
export declare abstract class SignerProviderSimple extends SignerProvider {
|
|
29
|
+
abstract get nodeProvider(): NodeProvider;
|
|
30
30
|
submitTransaction(params: SubmitTransactionParams): Promise<SubmissionResult>;
|
|
31
31
|
signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
|
|
32
32
|
signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
|
|
@@ -45,7 +45,7 @@ export declare abstract class SignerProviderSimple extends TransactionBuilder im
|
|
|
45
45
|
abstract signRaw(signerAddress: string, hexString: string): Promise<string>;
|
|
46
46
|
}
|
|
47
47
|
export declare abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
|
|
48
|
-
abstract
|
|
48
|
+
abstract setSelectedAccount(address: string): Promise<void>;
|
|
49
49
|
abstract getAccounts(): Promise<Account[]>;
|
|
50
50
|
getAccount(signerAddress: string): Promise<Account>;
|
|
51
51
|
getPublicKey(signerAddress: string): Promise<string>;
|
|
@@ -53,13 +53,12 @@ export declare abstract class SignerProviderWithMultipleAccounts extends SignerP
|
|
|
53
53
|
export declare abstract class SignerProviderWithCachedAccounts<T extends Account> extends SignerProviderWithMultipleAccounts {
|
|
54
54
|
private _selectedAccount;
|
|
55
55
|
protected readonly _accounts: Map<string, T>;
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
protected unsafeGetSelectedAccount(): Promise<T>;
|
|
57
|
+
setSelectedAccount(address: string): Promise<void>;
|
|
58
58
|
getAccounts(): Promise<T[]>;
|
|
59
59
|
getAccount(address: string): Promise<T>;
|
|
60
60
|
}
|
|
61
|
-
export declare function
|
|
62
|
-
export declare function verifySignedMessage(message: string, publicKey: string, signature: string): boolean;
|
|
61
|
+
export declare function verifySignedMessage(message: string, publicKey: string, signature: string, keyType: KeyType): boolean;
|
|
63
62
|
export declare function toApiDestination(data: Destination): node.Destination;
|
|
64
63
|
export declare function toApiDestinations(data: Destination[]): node.Destination[];
|
|
65
64
|
export declare function fromApiDestination(data: node.Destination): Destination;
|
|
@@ -43,18 +43,37 @@ 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.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.
|
|
47
|
-
const elliptic_1 = require("elliptic");
|
|
46
|
+
exports.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.SignerProviderWithCachedAccounts = exports.SignerProviderWithMultipleAccounts = exports.SignerProviderSimple = exports.InteractiveSignerProvider = exports.SignerProvider = void 0;
|
|
48
47
|
const api_1 = require("../api");
|
|
49
48
|
const utils = __importStar(require("../utils"));
|
|
50
49
|
const blakejs_1 = __importDefault(require("blakejs"));
|
|
51
50
|
const tx_builder_1 = require("./tx-builder");
|
|
52
|
-
const
|
|
53
|
-
class
|
|
54
|
-
async
|
|
55
|
-
const account = await this.
|
|
56
|
-
|
|
51
|
+
const utils_1 = require("../utils");
|
|
52
|
+
class SignerProvider {
|
|
53
|
+
async getSelectedAccount() {
|
|
54
|
+
const account = await this.unsafeGetSelectedAccount();
|
|
55
|
+
SignerProvider.validateAccount(account);
|
|
56
|
+
return account;
|
|
57
|
+
}
|
|
58
|
+
static validateAccount(account) {
|
|
59
|
+
const derivedAddress = (0, utils_1.addressFromPublicKey)(account.publicKey, account.keyType);
|
|
60
|
+
const derivedGroup = (0, utils_1.groupOfAddress)(derivedAddress);
|
|
61
|
+
if (derivedAddress !== account.address || derivedGroup !== account.group) {
|
|
62
|
+
throw Error(`Invalid accounot data: ${JSON.stringify(account)}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.SignerProvider = SignerProvider;
|
|
67
|
+
// Abstraction for interactive signer (e.g. WalletConnect instance, Extension wallet object)
|
|
68
|
+
class InteractiveSignerProvider extends SignerProvider {
|
|
69
|
+
async enable(opt) {
|
|
70
|
+
const account = await this.unsafeEnable(opt);
|
|
71
|
+
SignerProvider.validateAccount(account);
|
|
72
|
+
return account;
|
|
57
73
|
}
|
|
74
|
+
}
|
|
75
|
+
exports.InteractiveSignerProvider = InteractiveSignerProvider;
|
|
76
|
+
class SignerProviderSimple extends SignerProvider {
|
|
58
77
|
async submitTransaction(params) {
|
|
59
78
|
const data = { unsignedTx: params.unsignedTx, signature: params.signature };
|
|
60
79
|
return this.nodeProvider.transactions.postTransactionsSubmit(data);
|
|
@@ -90,7 +109,7 @@ class SignerProviderSimple extends tx_builder_1.TransactionBuilder {
|
|
|
90
109
|
return { signature, ...response };
|
|
91
110
|
}
|
|
92
111
|
async buildTransferTx(params) {
|
|
93
|
-
return
|
|
112
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildTransferTx(params, await this.getPublicKey(params.signerAddress));
|
|
94
113
|
}
|
|
95
114
|
async signDeployContractTx(params) {
|
|
96
115
|
const response = await this.buildDeployContractTx(params);
|
|
@@ -98,7 +117,7 @@ class SignerProviderSimple extends tx_builder_1.TransactionBuilder {
|
|
|
98
117
|
return { signature, ...response };
|
|
99
118
|
}
|
|
100
119
|
async buildDeployContractTx(params) {
|
|
101
|
-
return
|
|
120
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildDeployContractTx(params, await this.getPublicKey(params.signerAddress));
|
|
102
121
|
}
|
|
103
122
|
async signExecuteScriptTx(params) {
|
|
104
123
|
const response = await this.buildExecuteScriptTx(params);
|
|
@@ -106,12 +125,12 @@ class SignerProviderSimple extends tx_builder_1.TransactionBuilder {
|
|
|
106
125
|
return { signature, ...response };
|
|
107
126
|
}
|
|
108
127
|
async buildExecuteScriptTx(params) {
|
|
109
|
-
return
|
|
128
|
+
return tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildExecuteScriptTx(params, await this.getPublicKey(params.signerAddress));
|
|
110
129
|
}
|
|
111
130
|
// in general, wallet should show the decoded information to user for confirmation
|
|
112
131
|
// please overwrite this function for real wallet
|
|
113
132
|
async signUnsignedTx(params) {
|
|
114
|
-
const response = await this.buildUnsignedTx(params);
|
|
133
|
+
const response = await tx_builder_1.TransactionBuilder.from(this.nodeProvider).buildUnsignedTx(params);
|
|
115
134
|
const signature = await this.signRaw(params.signerAddress, response.txId);
|
|
116
135
|
return { signature, ...response };
|
|
117
136
|
}
|
|
@@ -146,7 +165,7 @@ class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccount
|
|
|
146
165
|
this._selectedAccount = undefined;
|
|
147
166
|
this._accounts = new Map();
|
|
148
167
|
}
|
|
149
|
-
|
|
168
|
+
unsafeGetSelectedAccount() {
|
|
150
169
|
if (this._selectedAccount === undefined) {
|
|
151
170
|
throw Error('No account is selected yet');
|
|
152
171
|
}
|
|
@@ -154,7 +173,7 @@ class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccount
|
|
|
154
173
|
return Promise.resolve(this._selectedAccount);
|
|
155
174
|
}
|
|
156
175
|
}
|
|
157
|
-
|
|
176
|
+
setSelectedAccount(address) {
|
|
158
177
|
const accountOpt = this._accounts.get(address);
|
|
159
178
|
if (accountOpt === undefined) {
|
|
160
179
|
throw Error('The address is not in the accounts');
|
|
@@ -176,23 +195,13 @@ class SignerProviderWithCachedAccounts extends SignerProviderWithMultipleAccount
|
|
|
176
195
|
}
|
|
177
196
|
}
|
|
178
197
|
exports.SignerProviderWithCachedAccounts = SignerProviderWithCachedAccounts;
|
|
179
|
-
function verifyHexString(hexString, publicKey, signature) {
|
|
180
|
-
try {
|
|
181
|
-
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
182
|
-
return key.verify(hexString, utils.signatureDecode(ec, signature));
|
|
183
|
-
}
|
|
184
|
-
catch (error) {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
exports.verifyHexString = verifyHexString;
|
|
189
198
|
function extendMessage(message) {
|
|
190
199
|
return 'Alephium Signed Message: ' + message;
|
|
191
200
|
}
|
|
192
|
-
function verifySignedMessage(message, publicKey, signature) {
|
|
201
|
+
function verifySignedMessage(message, publicKey, signature, keyType) {
|
|
193
202
|
const extendedMessage = extendMessage(message);
|
|
194
203
|
const messageHash = blakejs_1.default.blake2b(extendedMessage, undefined, 32);
|
|
195
|
-
return
|
|
204
|
+
return utils.verifySignature(utils.binToHex(messageHash), publicKey, signature, keyType);
|
|
196
205
|
}
|
|
197
206
|
exports.verifySignedMessage = verifySignedMessage;
|
|
198
207
|
function toApiDestination(data) {
|
|
@@ -2,13 +2,8 @@ import { NodeProvider } from '../api';
|
|
|
2
2
|
import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignExecuteScriptTxResult, SignTransferTxParams, SignTransferTxResult, SignUnsignedTxParams, SignUnsignedTxResult } from './types';
|
|
3
3
|
export declare abstract class TransactionBuilder {
|
|
4
4
|
abstract get nodeProvider(): NodeProvider;
|
|
5
|
-
static
|
|
6
|
-
|
|
7
|
-
buildTransferTx(params: SignTransferTxParams, publicKey: string): Promise<Omit<SignTransferTxResult, "signature">>;
|
|
8
|
-
buildDeployContractTx(params: SignDeployContractTxParams, publicKey: string): Promise<Omit<SignDeployContractTxResult, "signature">>;
|
|
9
|
-
buildExecuteScriptTx(params: SignExecuteScriptTxParams, publicKey: string): Promise<Omit<SignExecuteScriptTxResult, "signature">>;
|
|
10
|
-
buildUnsignedTx(params: SignUnsignedTxParams): Promise<Omit<SignUnsignedTxResult, "signature">>;
|
|
11
|
-
};
|
|
5
|
+
static from(nodeProvider: NodeProvider): TransactionBuilder;
|
|
6
|
+
static from(baseUrl: string, apiKey?: string): TransactionBuilder;
|
|
12
7
|
private static validatePublicKey;
|
|
13
8
|
buildTransferTx(params: SignTransferTxParams, publicKey: string): Promise<Omit<SignTransferTxResult, 'signature'>>;
|
|
14
9
|
buildDeployContractTx(params: SignDeployContractTxParams, publicKey: string): Promise<Omit<SignDeployContractTxResult, 'signature'>>;
|
|
@@ -23,25 +23,26 @@ const api_1 = require("../api");
|
|
|
23
23
|
const utils_1 = require("../utils");
|
|
24
24
|
const signer_1 = require("./signer");
|
|
25
25
|
class TransactionBuilder {
|
|
26
|
-
static
|
|
27
|
-
const nodeProvider = new api_1.NodeProvider(
|
|
26
|
+
static from(param0, param1) {
|
|
27
|
+
const nodeProvider = typeof param0 === 'string' ? new api_1.NodeProvider(param0, param1) : param0;
|
|
28
28
|
return new (class extends TransactionBuilder {
|
|
29
29
|
get nodeProvider() {
|
|
30
30
|
return nodeProvider;
|
|
31
31
|
}
|
|
32
32
|
})();
|
|
33
33
|
}
|
|
34
|
-
static validatePublicKey(params, publicKey) {
|
|
35
|
-
const address = (0, utils_1.addressFromPublicKey)(publicKey);
|
|
34
|
+
static validatePublicKey(params, publicKey, keyType) {
|
|
35
|
+
const address = (0, utils_1.addressFromPublicKey)(publicKey, keyType);
|
|
36
36
|
if (address !== params.signerAddress) {
|
|
37
37
|
throw new Error('Unmatched public key');
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
async buildTransferTx(params, publicKey) {
|
|
41
|
-
TransactionBuilder.validatePublicKey(params, publicKey);
|
|
41
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType ?? 'default');
|
|
42
42
|
const { destinations, gasPrice, ...rest } = params;
|
|
43
43
|
const data = {
|
|
44
44
|
fromPublicKey: publicKey,
|
|
45
|
+
fromPublicKeyType: params.signerKeyType,
|
|
45
46
|
destinations: (0, signer_1.toApiDestinations)(destinations),
|
|
46
47
|
gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
|
|
47
48
|
...rest
|
|
@@ -50,10 +51,11 @@ class TransactionBuilder {
|
|
|
50
51
|
return { ...response, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
51
52
|
}
|
|
52
53
|
async buildDeployContractTx(params, publicKey) {
|
|
53
|
-
TransactionBuilder.validatePublicKey(params, publicKey);
|
|
54
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType ?? 'default');
|
|
54
55
|
const { initialAttoAlphAmount, initialTokenAmounts, issueTokenAmount, gasPrice, ...rest } = params;
|
|
55
56
|
const data = {
|
|
56
57
|
fromPublicKey: publicKey,
|
|
58
|
+
fromPublicKeyType: params.signerKeyType,
|
|
57
59
|
initialAttoAlphAmount: (0, api_1.toApiNumber256Optional)(initialAttoAlphAmount),
|
|
58
60
|
initialTokenAmounts: (0, api_1.toApiTokens)(initialTokenAmounts),
|
|
59
61
|
issueTokenAmount: (0, api_1.toApiNumber256Optional)(issueTokenAmount),
|
|
@@ -65,10 +67,11 @@ class TransactionBuilder {
|
|
|
65
67
|
return { ...response, groupIndex: response.fromGroup, contractId, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
66
68
|
}
|
|
67
69
|
async buildExecuteScriptTx(params, publicKey) {
|
|
68
|
-
TransactionBuilder.validatePublicKey(params, publicKey);
|
|
70
|
+
TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType ?? 'default');
|
|
69
71
|
const { attoAlphAmount, tokens, gasPrice, ...rest } = params;
|
|
70
72
|
const data = {
|
|
71
73
|
fromPublicKey: publicKey,
|
|
74
|
+
fromPublicKeyType: params.signerKeyType,
|
|
72
75
|
attoAlphAmount: (0, api_1.toApiNumber256Optional)(attoAlphAmount),
|
|
73
76
|
tokens: (0, api_1.toApiTokens)(tokens),
|
|
74
77
|
gasPrice: (0, api_1.toApiNumber256Optional)(gasPrice),
|
|
@@ -9,16 +9,20 @@ export interface Destination {
|
|
|
9
9
|
lockTime?: number;
|
|
10
10
|
message?: string;
|
|
11
11
|
}
|
|
12
|
+
export declare type KeyType = 'default' | 'bip340-schnorr';
|
|
12
13
|
export interface Account {
|
|
14
|
+
keyType: KeyType;
|
|
13
15
|
address: string;
|
|
14
16
|
group: number;
|
|
15
17
|
publicKey: string;
|
|
16
18
|
}
|
|
17
19
|
export declare type SignerAddress = {
|
|
18
20
|
signerAddress: string;
|
|
21
|
+
signerKeyType?: KeyType;
|
|
19
22
|
};
|
|
20
23
|
export interface SignTransferTxParams {
|
|
21
24
|
signerAddress: string;
|
|
25
|
+
signerKeyType?: KeyType;
|
|
22
26
|
destinations: Destination[];
|
|
23
27
|
utxos?: OutputRef[];
|
|
24
28
|
gasAmount?: number;
|
|
@@ -35,6 +39,7 @@ export interface SignTransferTxResult {
|
|
|
35
39
|
}
|
|
36
40
|
export interface SignDeployContractTxParams {
|
|
37
41
|
signerAddress: string;
|
|
42
|
+
signerKeyType?: KeyType;
|
|
38
43
|
bytecode: string;
|
|
39
44
|
initialAttoAlphAmount?: Number256;
|
|
40
45
|
initialTokenAmounts?: Token[];
|
|
@@ -54,6 +59,7 @@ export interface SignDeployContractTxResult {
|
|
|
54
59
|
}
|
|
55
60
|
export interface SignExecuteScriptTxParams {
|
|
56
61
|
signerAddress: string;
|
|
62
|
+
signerKeyType?: KeyType;
|
|
57
63
|
bytecode: string;
|
|
58
64
|
attoAlphAmount?: Number256;
|
|
59
65
|
tokens?: Token[];
|
|
@@ -70,6 +76,7 @@ export interface SignExecuteScriptTxResult {
|
|
|
70
76
|
}
|
|
71
77
|
export interface SignUnsignedTxParams {
|
|
72
78
|
signerAddress: string;
|
|
79
|
+
signerKeyType?: KeyType;
|
|
73
80
|
unsignedTx: string;
|
|
74
81
|
}
|
|
75
82
|
export interface SignUnsignedTxResult {
|
|
@@ -83,6 +90,7 @@ export interface SignUnsignedTxResult {
|
|
|
83
90
|
}
|
|
84
91
|
export interface SignMessageParams {
|
|
85
92
|
signerAddress: string;
|
|
93
|
+
signerKeyType?: KeyType;
|
|
86
94
|
message: string;
|
|
87
95
|
}
|
|
88
96
|
export interface SignMessageResult {
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
1
|
+
import { KeyType } from '../signer';
|
|
2
|
+
export declare function transactionSign(txId: string, privateKey: string, keyType: KeyType): string;
|
|
3
|
+
export declare function transactionVerifySignature(txId: string, publicKey: string, signature: string, keyType: KeyType): boolean;
|