@alephium/web3 0.5.0-rc.1 → 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.
Files changed (39) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.LICENSE.txt +2 -0
  3. package/dist/alephium-web3.min.js.map +1 -1
  4. package/dist/src/api/api-alephium.d.ts +57 -20
  5. package/dist/src/api/api-alephium.js +57 -15
  6. package/dist/src/api/index.d.ts +1 -0
  7. package/dist/src/api/index.js +1 -0
  8. package/dist/src/api/types.d.ts +1 -1
  9. package/dist/src/api/types.js +6 -2
  10. package/dist/src/constants.d.ts +1 -0
  11. package/dist/src/constants.js +2 -1
  12. package/dist/src/contract/contract.d.ts +9 -4
  13. package/dist/src/contract/contract.js +31 -14
  14. package/dist/src/signer/signer.d.ts +29 -30
  15. package/dist/src/signer/signer.js +34 -25
  16. package/dist/src/signer/tx-builder.d.ts +2 -7
  17. package/dist/src/signer/tx-builder.js +10 -7
  18. package/dist/src/signer/types.d.ts +8 -0
  19. package/dist/src/transaction/sign-verify.d.ts +3 -2
  20. package/dist/src/transaction/sign-verify.js +4 -14
  21. package/dist/src/utils/index.d.ts +1 -0
  22. package/dist/src/utils/index.js +1 -0
  23. package/dist/src/utils/sign.d.ts +3 -0
  24. package/dist/src/utils/sign.js +89 -0
  25. package/dist/src/utils/utils.d.ts +4 -3
  26. package/dist/src/utils/utils.js +25 -10
  27. package/package.json +2 -1
  28. package/src/api/api-alephium.ts +88 -32
  29. package/src/api/index.ts +2 -0
  30. package/src/api/types.ts +12 -2
  31. package/src/constants.ts +1 -0
  32. package/src/contract/contract.ts +49 -20
  33. package/src/signer/signer.ts +69 -55
  34. package/src/signer/tx-builder.ts +13 -7
  35. package/src/signer/types.ts +10 -2
  36. package/src/transaction/sign-verify.ts +10 -15
  37. package/src/utils/index.ts +1 -0
  38. package/src/utils/sign.ts +66 -0
  39. package/src/utils/utils.ts +26 -10
@@ -145,9 +145,11 @@ export interface BrokerInfo {
145
145
  };
146
146
  }
147
147
  export interface BuildDeployContractTx {
148
- /** @format public-key */
148
+ /** @format hex-string */
149
149
  fromPublicKey: string;
150
150
  /** @format hex-string */
151
+ fromPublicKeyType?: string;
152
+ /** @format hex-string */
151
153
  bytecode: string;
152
154
  /** @format uint256 */
153
155
  initialAttoAlphAmount?: string;
@@ -177,9 +179,11 @@ export interface BuildDeployContractTxResult {
177
179
  contractAddress: string;
178
180
  }
179
181
  export interface BuildExecuteScriptTx {
180
- /** @format public-key */
182
+ /** @format hex-string */
181
183
  fromPublicKey: string;
182
184
  /** @format hex-string */
185
+ fromPublicKeyType?: string;
186
+ /** @format hex-string */
183
187
  bytecode: string;
184
188
  /** @format uint256 */
185
189
  attoAlphAmount?: string;
@@ -249,8 +253,10 @@ export interface BuildSweepAddressTransactionsResult {
249
253
  toGroup: number;
250
254
  }
251
255
  export interface BuildTransaction {
252
- /** @format public-key */
256
+ /** @format hex-string */
253
257
  fromPublicKey: string;
258
+ /** @format hex-string */
259
+ fromPublicKeyType?: string;
254
260
  destinations: Destination[];
255
261
  utxos?: OutputRef[];
256
262
  /** @format gas */
@@ -519,6 +525,13 @@ export interface InternalServerError {
519
525
  export interface MemPooled {
520
526
  type: string;
521
527
  }
528
+ export interface MempoolTransactions {
529
+ /** @format int32 */
530
+ fromGroup: number;
531
+ /** @format int32 */
532
+ toGroup: number;
533
+ transactions: TransactionTemplate[];
534
+ }
522
535
  export interface MinerAddresses {
523
536
  addresses: string[];
524
537
  }
@@ -756,13 +769,6 @@ export interface Unban {
756
769
  peers: string[];
757
770
  type: string;
758
771
  }
759
- export interface UnconfirmedTransactions {
760
- /** @format int32 */
761
- fromGroup: number;
762
- /** @format int32 */
763
- toGroup: number;
764
- unconfirmedTransactions: TransactionTemplate[];
765
- }
766
772
  export interface Unreachable {
767
773
  peers: string[];
768
774
  type: string;
@@ -910,7 +916,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
910
916
  }
911
917
  /**
912
918
  * @title Alephium API
913
- * @version 1.7.0
919
+ * @version 1.7.1
914
920
  * @baseUrl ../
915
921
  */
916
922
  export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -1323,15 +1329,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
1323
1329
  getAddressesAddressGroup: (address: string, params?: RequestParams) => Promise<Group>;
1324
1330
  };
1325
1331
  transactions: {
1326
- /**
1327
- * No description
1328
- *
1329
- * @tags Transactions
1330
- * @name GetTransactionsUnconfirmed
1331
- * @summary List unconfirmed transactions
1332
- * @request GET:/transactions/unconfirmed
1333
- */
1334
- getTransactionsUnconfirmed: (params?: RequestParams) => Promise<UnconfirmedTransactions[]>;
1335
1332
  /**
1336
1333
  * No description
1337
1334
  *
@@ -1394,6 +1391,46 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
1394
1391
  toGroup?: number;
1395
1392
  }, params?: RequestParams) => Promise<Confirmed | MemPooled | TxNotFound>;
1396
1393
  };
1394
+ mempool: {
1395
+ /**
1396
+ * No description
1397
+ *
1398
+ * @tags Mempool
1399
+ * @name GetMempoolTransactions
1400
+ * @summary List mempool transactions
1401
+ * @request GET:/mempool/transactions
1402
+ */
1403
+ getMempoolTransactions: (params?: RequestParams) => Promise<MempoolTransactions[]>;
1404
+ /**
1405
+ * No description
1406
+ *
1407
+ * @tags Mempool
1408
+ * @name DeleteMempoolTransactions
1409
+ * @summary Remove all transactions from mempool
1410
+ * @request DELETE:/mempool/transactions
1411
+ */
1412
+ deleteMempoolTransactions: (params?: RequestParams) => Promise<void>;
1413
+ /**
1414
+ * No description
1415
+ *
1416
+ * @tags Mempool
1417
+ * @name PutMempoolTransactionsRebroadcast
1418
+ * @summary Rebroadcase a mempool transaction to the network
1419
+ * @request PUT:/mempool/transactions/rebroadcast
1420
+ */
1421
+ putMempoolTransactionsRebroadcast: (query: {
1422
+ txId: string;
1423
+ }, params?: RequestParams) => Promise<void>;
1424
+ /**
1425
+ * No description
1426
+ *
1427
+ * @tags Mempool
1428
+ * @name PutMempoolTransactionsValidate
1429
+ * @summary Validate all mempool transactions and remove invalid ones
1430
+ * @request PUT:/mempool/transactions/validate
1431
+ */
1432
+ putMempoolTransactionsValidate: (params?: RequestParams) => Promise<void>;
1433
+ };
1397
1434
  contracts: {
1398
1435
  /**
1399
1436
  * No description
@@ -149,7 +149,7 @@ class HttpClient {
149
149
  exports.HttpClient = HttpClient;
150
150
  /**
151
151
  * @title Alephium API
152
- * @version 1.7.0
152
+ * @version 1.7.1
153
153
  * @baseUrl ../
154
154
  */
155
155
  class Api extends HttpClient {
@@ -778,20 +778,6 @@ class Api extends HttpClient {
778
778
  }).then(utils_1.convertHttpResponse)
779
779
  };
780
780
  this.transactions = {
781
- /**
782
- * No description
783
- *
784
- * @tags Transactions
785
- * @name GetTransactionsUnconfirmed
786
- * @summary List unconfirmed transactions
787
- * @request GET:/transactions/unconfirmed
788
- */
789
- getTransactionsUnconfirmed: (params = {}) => this.request({
790
- path: `/transactions/unconfirmed`,
791
- method: 'GET',
792
- format: 'json',
793
- ...params
794
- }).then(utils_1.convertHttpResponse),
795
781
  /**
796
782
  * No description
797
783
  *
@@ -887,6 +873,62 @@ class Api extends HttpClient {
887
873
  ...params
888
874
  }).then(utils_1.convertHttpResponse)
889
875
  };
876
+ this.mempool = {
877
+ /**
878
+ * No description
879
+ *
880
+ * @tags Mempool
881
+ * @name GetMempoolTransactions
882
+ * @summary List mempool transactions
883
+ * @request GET:/mempool/transactions
884
+ */
885
+ getMempoolTransactions: (params = {}) => this.request({
886
+ path: `/mempool/transactions`,
887
+ method: 'GET',
888
+ format: 'json',
889
+ ...params
890
+ }).then(utils_1.convertHttpResponse),
891
+ /**
892
+ * No description
893
+ *
894
+ * @tags Mempool
895
+ * @name DeleteMempoolTransactions
896
+ * @summary Remove all transactions from mempool
897
+ * @request DELETE:/mempool/transactions
898
+ */
899
+ deleteMempoolTransactions: (params = {}) => this.request({
900
+ path: `/mempool/transactions`,
901
+ method: 'DELETE',
902
+ ...params
903
+ }).then(utils_1.convertHttpResponse),
904
+ /**
905
+ * No description
906
+ *
907
+ * @tags Mempool
908
+ * @name PutMempoolTransactionsRebroadcast
909
+ * @summary Rebroadcase a mempool transaction to the network
910
+ * @request PUT:/mempool/transactions/rebroadcast
911
+ */
912
+ putMempoolTransactionsRebroadcast: (query, params = {}) => this.request({
913
+ path: `/mempool/transactions/rebroadcast`,
914
+ method: 'PUT',
915
+ query: query,
916
+ ...params
917
+ }).then(utils_1.convertHttpResponse),
918
+ /**
919
+ * No description
920
+ *
921
+ * @tags Mempool
922
+ * @name PutMempoolTransactionsValidate
923
+ * @summary Validate all mempool transactions and remove invalid ones
924
+ * @request PUT:/mempool/transactions/validate
925
+ */
926
+ putMempoolTransactionsValidate: (params = {}) => this.request({
927
+ path: `/mempool/transactions/validate`,
928
+ method: 'PUT',
929
+ ...params
930
+ }).then(utils_1.convertHttpResponse)
931
+ };
890
932
  this.contracts = {
891
933
  /**
892
934
  * No description
@@ -11,6 +11,7 @@ export declare class NodeProvider {
11
11
  readonly blockflow: NodeApi<string>['blockflow'];
12
12
  readonly addresses: NodeApi<string>['addresses'];
13
13
  readonly transactions: NodeApi<string>['transactions'];
14
+ readonly mempool: NodeApi<string>['mempool'];
14
15
  readonly contracts: NodeApi<string>['contracts'];
15
16
  readonly multisig: NodeApi<string>['multisig'];
16
17
  readonly utils: NodeApi<string>['utils'];
@@ -90,6 +90,7 @@ class NodeProvider {
90
90
  this.blockflow = { ...nodeApi.blockflow };
91
91
  this.addresses = { ...nodeApi.addresses };
92
92
  this.transactions = { ...nodeApi.transactions };
93
+ this.mempool = { ...nodeApi.mempool };
93
94
  this.contracts = { ...nodeApi.contracts };
94
95
  this.multisig = { ...nodeApi.multisig };
95
96
  this.utils = { ...nodeApi.utils };
@@ -18,7 +18,7 @@ export declare function toApiByteVec(v: Val): string;
18
18
  export declare function toApiAddress(v: Val): string;
19
19
  export declare function toApiArray(tpe: string, v: Val): node.Val;
20
20
  export declare function toApiVal(v: Val, tpe: string): node.Val;
21
- export declare function fromApiVals(vals: node.Val[], names: string[], types: string[]): NamedVals;
21
+ export declare function fromApiVals(vals: node.Val[], names: string[], types: string[], optionalNames?: string[], optionalTypes?: string[]): NamedVals;
22
22
  export declare function fromApiArray(vals: node.Val[], types: string[]): Val[];
23
23
  export declare function fromApiVal(v: node.Val, tpe: string): Val;
24
24
  export declare function typeLength(tpe: string): number;
@@ -165,7 +165,7 @@ function _fromApiVal(vals, valIndex, tpe) {
165
165
  }
166
166
  }
167
167
  }
168
- function fromApiVals(vals, names, types) {
168
+ function fromApiVals(vals, names, types, optionalNames = [], optionalTypes = []) {
169
169
  let valIndex = 0;
170
170
  const result = {};
171
171
  types.forEach((currentType, index) => {
@@ -174,7 +174,11 @@ function fromApiVals(vals, names, types) {
174
174
  valIndex = nextIndex;
175
175
  result[`${currentName}`] = val;
176
176
  });
177
- return result;
177
+ if (valIndex === vals.length) {
178
+ return result;
179
+ }
180
+ const optionalFields = fromApiVals(vals.slice(valIndex), optionalNames, optionalTypes);
181
+ return { ...result, ...optionalFields };
178
182
  }
179
183
  exports.fromApiVals = fromApiVals;
180
184
  function fromApiArray(vals, types) {
@@ -2,3 +2,4 @@ export declare const TOTAL_NUMBER_OF_GROUPS = 4;
2
2
  export declare const MIN_UTXO_SET_AMOUNT: bigint;
3
3
  export declare const ALPH_TOKEN_ID: string;
4
4
  export declare const ONE_ALPH: bigint;
5
+ export declare const DUST_AMOUNT: bigint;
@@ -17,8 +17,9 @@ 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.ONE_ALPH = exports.ALPH_TOKEN_ID = exports.MIN_UTXO_SET_AMOUNT = exports.TOTAL_NUMBER_OF_GROUPS = void 0;
20
+ exports.DUST_AMOUNT = exports.ONE_ALPH = exports.ALPH_TOKEN_ID = exports.MIN_UTXO_SET_AMOUNT = exports.TOTAL_NUMBER_OF_GROUPS = void 0;
21
21
  exports.TOTAL_NUMBER_OF_GROUPS = 4;
22
22
  exports.MIN_UTXO_SET_AMOUNT = BigInt(1000000000000);
23
23
  exports.ALPH_TOKEN_ID = ''.padStart(64, '0');
24
24
  exports.ONE_ALPH = 10n ** 18n;
25
+ exports.DUST_AMOUNT = 10n ** 15n;
@@ -123,9 +123,9 @@ export declare class Contract extends Artifact {
123
123
  fromApiContractState(state: node.ContractState): ContractState<Fields>;
124
124
  static fromApiContractState(state: node.ContractState): ContractState;
125
125
  static ContractCreatedEventIndex: number;
126
- static ContractCreatedEvent: EventSig;
126
+ static ContractCreatedEvent: SystemEventSig;
127
127
  static ContractDestroyedEventIndex: number;
128
- static ContractDestroyedEvent: EventSig;
128
+ static ContractDestroyedEvent: SystemEventSig;
129
129
  static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined, txId: string): ContractEvent;
130
130
  fromApiTestContractResult(methodName: string, result: node.TestContractResult, txId: string): TestContractResult<unknown>;
131
131
  txParamsForDeployment<P extends Fields>(signer: SignerProvider, params: DeployContractParams<P>): Promise<SignDeployContractTxParams>;
@@ -257,11 +257,16 @@ export interface CallContractResult<R> {
257
257
  txOutputs: Output[];
258
258
  events: ContractEvent[];
259
259
  }
260
+ export interface SystemEventSig extends EventSig {
261
+ optionalFieldNames?: string[];
262
+ optionalFieldTypes?: string[];
263
+ }
260
264
  export declare type ContractCreatedEvent = ContractEvent<{
261
- address: HexString;
265
+ address: Address;
266
+ parentAddress?: Address;
262
267
  }>;
263
268
  export declare type ContractDestroyedEvent = ContractEvent<{
264
- address: HexString;
269
+ address: Address;
265
270
  }>;
266
271
  export declare function decodeContractCreatedEvent(event: node.ContractEvent): Omit<ContractCreatedEvent, 'contractAddress'>;
267
272
  export declare function decodeContractDestroyedEvent(event: node.ContractEvent): Omit<ContractDestroyedEvent, 'contractAddress'>;
@@ -579,24 +579,29 @@ class Contract extends Artifact {
579
579
  return contract.fromApiContractState(state);
580
580
  }
581
581
  static fromApiEvent(event, codeHash, txId) {
582
- let eventSig;
582
+ let fields;
583
+ let name;
583
584
  if (event.eventIndex == Contract.ContractCreatedEventIndex) {
584
- eventSig = this.ContractCreatedEvent;
585
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractCreatedEvent);
586
+ name = Contract.ContractCreatedEvent.name;
585
587
  }
586
588
  else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
587
- eventSig = this.ContractDestroyedEvent;
589
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractDestroyedEvent);
590
+ name = Contract.ContractDestroyedEvent.name;
588
591
  }
589
592
  else {
590
593
  const contract = Project.currentProject.contractByCodeHash(codeHash);
591
- eventSig = contract.eventsSig[event.eventIndex];
594
+ const eventSig = contract.eventsSig[event.eventIndex];
595
+ fields = fromApiEventFields(event.fields, eventSig);
596
+ name = eventSig.name;
592
597
  }
593
598
  return {
594
599
  txId: txId,
595
600
  blockHash: event.blockHash,
596
601
  contractAddress: event.contractAddress,
597
- name: eventSig.name,
602
+ name: name,
598
603
  eventIndex: event.eventIndex,
599
- fields: fromApiEventFields(event.fields, eventSig)
604
+ fields: fields
600
605
  };
601
606
  }
602
607
  fromApiTestContractResult(methodName, result, txId) {
@@ -620,8 +625,10 @@ class Contract extends Artifact {
620
625
  }
621
626
  async txParamsForDeployment(signer, params) {
622
627
  const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {});
628
+ const selectedAccount = await signer.getSelectedAccount();
623
629
  const signerParams = {
624
- signerAddress: await signer.getSelectedAddress(),
630
+ signerAddress: selectedAccount.address,
631
+ signerKeyType: selectedAccount.keyType,
625
632
  bytecode: bytecode,
626
633
  initialAttoAlphAmount: params?.initialAttoAlphAmount,
627
634
  issueTokenAmount: params?.issueTokenAmount,
@@ -678,7 +685,9 @@ Contract.ContractCreatedEventIndex = -1;
678
685
  Contract.ContractCreatedEvent = {
679
686
  name: 'ContractCreated',
680
687
  fieldNames: ['address'],
681
- fieldTypes: ['Address']
688
+ fieldTypes: ['Address'],
689
+ optionalFieldNames: ['parentAddress'],
690
+ optionalFieldTypes: ['Address']
682
691
  };
683
692
  Contract.ContractDestroyedEventIndex = -2;
684
693
  Contract.ContractDestroyedEvent = {
@@ -723,8 +732,10 @@ class Script extends Artifact {
723
732
  return JSON.stringify(object, null, 2);
724
733
  }
725
734
  async txParamsForExecution(signer, params) {
735
+ const selectedAccount = await signer.getSelectedAccount();
726
736
  const signerParams = {
727
- signerAddress: await signer.getSelectedAddress(),
737
+ signerAddress: selectedAccount.address,
738
+ signerKeyType: selectedAccount.keyType,
728
739
  bytecode: this.buildByteCodeToDeploy(params.initialFields ?? {}),
729
740
  attoAlphAmount: params.attoAlphAmount,
730
741
  tokens: params.tokens,
@@ -758,6 +769,9 @@ function fromApiFields(immFields, mutFields, fieldsSig) {
758
769
  function fromApiEventFields(vals, eventSig) {
759
770
  return (0, api_1.fromApiVals)(vals, eventSig.fieldNames, eventSig.fieldTypes);
760
771
  }
772
+ function fromApiSystemEventFields(vals, systemEventSig) {
773
+ return (0, api_1.fromApiVals)(vals, systemEventSig.fieldNames, systemEventSig.fieldTypes, systemEventSig.optionalFieldNames ?? [], systemEventSig.optionalFieldTypes ?? []);
774
+ }
761
775
  function toApiAsset(asset) {
762
776
  return {
763
777
  attoAlphAmount: (0, api_1.toApiNumber256)(asset.alphAmount),
@@ -872,25 +886,28 @@ class ContractFactory {
872
886
  }
873
887
  }
874
888
  exports.ContractFactory = ContractFactory;
875
- function decodeFields(event, eventSig, eventIndex) {
889
+ function decodeSystemEvent(event, systemEventSig, eventIndex) {
876
890
  if (event.eventIndex !== eventIndex) {
877
891
  throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`);
878
892
  }
879
- return (0, api_1.fromApiVals)(event.fields, eventSig.fieldNames, eventSig.fieldTypes);
893
+ return fromApiSystemEventFields(event.fields, systemEventSig);
880
894
  }
881
895
  function decodeContractCreatedEvent(event) {
882
- const fields = decodeFields(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
896
+ const fields = decodeSystemEvent(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
883
897
  return {
884
898
  blockHash: event.blockHash,
885
899
  txId: event.txId,
886
900
  eventIndex: event.eventIndex,
887
901
  name: Contract.ContractCreatedEvent.name,
888
- fields: { address: fields['address'] }
902
+ fields: {
903
+ address: fields['address'],
904
+ parentAddress: fields['parentAddress'] === undefined ? undefined : fields['parentAddress']
905
+ }
889
906
  };
890
907
  }
891
908
  exports.decodeContractCreatedEvent = decodeContractCreatedEvent;
892
909
  function decodeContractDestroyedEvent(event) {
893
- const fields = decodeFields(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
910
+ const fields = decodeSystemEvent(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
894
911
  return {
895
912
  blockHash: event.blockHash,
896
913
  txId: event.txId,
@@ -1,32 +1,32 @@
1
1
  import { ExplorerProvider, NodeProvider } from '../api';
2
2
  import { node } from '../api';
3
- import { Account, Address, EnableOptionsBase, Destination, SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignExecuteScriptTxResult, SignMessageParams, SignMessageResult, SignTransferTxParams, SignTransferTxResult, SignUnsignedTxParams, SignUnsignedTxResult, SubmissionResult, SubmitTransactionParams, ExtSignTransferTxParams, ExtSignDeployContractTxParams, ExtSignExecuteScriptTxParams, ExtSignUnsignedTxParams, ExtSignMessageParams } from './types';
4
- import { TransactionBuilder } from './tx-builder';
5
- export interface SignerProvider {
6
- get nodeProvider(): NodeProvider | undefined;
7
- get explorerProvider(): ExplorerProvider | undefined;
8
- getSelectedAddress(): Promise<Address>;
9
- signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
10
- signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
11
- signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
12
- signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
13
- signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
14
- signMessage(params: SignMessageParams): Promise<SignMessageResult>;
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 interface InteractiveSignerProvider<EnableOptions extends EnableOptionsBase = EnableOptionsBase> extends SignerProvider {
17
- enable(opt?: EnableOptions): Promise<Address>;
18
- disconnect(): Promise<void>;
19
- signAndSubmitTransferTx(params: ExtSignTransferTxParams): Promise<SignTransferTxResult>;
20
- signAndSubmitDeployContractTx(params: ExtSignDeployContractTxParams): Promise<SignDeployContractTxResult>;
21
- signAndSubmitExecuteScriptTx(params: ExtSignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
22
- signAndSubmitUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>;
23
- signUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>;
24
- signMessage(params: ExtSignMessageParams): Promise<SignMessageResult>;
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 TransactionBuilder implements SignerProvider {
27
- abstract get explorerProvider(): ExplorerProvider | undefined;
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 setSelectedAddress(address: string): Promise<void>;
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
- getSelectedAccount(): Promise<T>;
57
- setSelectedAddress(address: string): Promise<void>;
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 verifyHexString(hexString: string, publicKey: string, signature: string): boolean;
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.verifyHexString = exports.SignerProviderWithCachedAccounts = exports.SignerProviderWithMultipleAccounts = exports.SignerProviderSimple = void 0;
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 ec = new elliptic_1.ec('secp256k1');
53
- class SignerProviderSimple extends tx_builder_1.TransactionBuilder {
54
- async getSelectedAddress() {
55
- const account = await this.getSelectedAccount();
56
- return account.address;
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 super.buildTransferTx(params, await this.getPublicKey(params.signerAddress));
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 super.buildDeployContractTx(params, await this.getPublicKey(params.signerAddress));
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 super.buildExecuteScriptTx(params, await this.getPublicKey(params.signerAddress));
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
- getSelectedAccount() {
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
- setSelectedAddress(address) {
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 verifyHexString(utils.binToHex(messageHash), publicKey, signature);
204
+ return utils.verifySignature(utils.binToHex(messageHash), publicKey, signature, keyType);
196
205
  }
197
206
  exports.verifySignedMessage = verifySignedMessage;
198
207
  function toApiDestination(data) {