@alephium/web3 0.2.0-rc.32 → 0.2.0-rc.34

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.
@@ -1,18 +1,49 @@
1
1
  import { Api as NodeApi } from './api-alephium';
2
2
  import { Api as ExplorerApi } from './api-explorer';
3
- export declare class NodeProvider {
4
- wallets: NodeApi<string>['wallets'];
5
- infos: NodeApi<string>['infos'];
6
- blockflow: NodeApi<string>['blockflow'];
7
- addresses: NodeApi<string>['addresses'];
8
- transactions: NodeApi<string>['transactions'];
9
- contracts: NodeApi<string>['contracts'];
10
- multisig: NodeApi<string>['multisig'];
11
- utils: NodeApi<string>['utils'];
12
- miners: NodeApi<string>['miners'];
13
- events: NodeApi<string>['events'];
3
+ interface INodeProvider {
4
+ readonly wallets: NodeApi<string>['wallets'];
5
+ readonly infos: NodeApi<string>['infos'];
6
+ readonly blockflow: NodeApi<string>['blockflow'];
7
+ readonly addresses: NodeApi<string>['addresses'];
8
+ readonly transactions: NodeApi<string>['transactions'];
9
+ readonly contracts: NodeApi<string>['contracts'];
10
+ readonly multisig: NodeApi<string>['multisig'];
11
+ readonly utils: NodeApi<string>['utils'];
12
+ readonly miners: NodeApi<string>['miners'];
13
+ readonly events: NodeApi<string>['events'];
14
+ }
15
+ export declare class NodeProvider implements INodeProvider {
16
+ readonly wallets: NodeApi<string>['wallets'];
17
+ readonly infos: NodeApi<string>['infos'];
18
+ readonly blockflow: NodeApi<string>['blockflow'];
19
+ readonly addresses: NodeApi<string>['addresses'];
20
+ readonly transactions: NodeApi<string>['transactions'];
21
+ readonly contracts: NodeApi<string>['contracts'];
22
+ readonly multisig: NodeApi<string>['multisig'];
23
+ readonly utils: NodeApi<string>['utils'];
24
+ readonly miners: NodeApi<string>['miners'];
25
+ readonly events: NodeApi<string>['events'];
26
+ constructor(provider: INodeProvider);
14
27
  constructor(baseUrl: string, apiKey?: string);
15
- reset(baseUrl: string, apiKey?: string): void;
28
+ static Proxy(nodeProvider: NodeProvider): NodeProvider;
29
+ }
30
+ export interface RequestArguments {
31
+ path: string;
32
+ method: string;
33
+ params?: any;
34
+ }
35
+ export declare class RemoteNodeProvider implements INodeProvider {
36
+ readonly wallets: NodeApi<string>['wallets'];
37
+ readonly infos: NodeApi<string>['infos'];
38
+ readonly blockflow: NodeApi<string>['blockflow'];
39
+ readonly addresses: NodeApi<string>['addresses'];
40
+ readonly transactions: NodeApi<string>['transactions'];
41
+ readonly contracts: NodeApi<string>['contracts'];
42
+ readonly multisig: NodeApi<string>['multisig'];
43
+ readonly utils: NodeApi<string>['utils'];
44
+ readonly miners: NodeApi<string>['miners'];
45
+ readonly events: NodeApi<string>['events'];
46
+ constructor(request: (request: RequestArguments) => Promise<any>);
16
47
  }
17
48
  export declare class ExplorerProvider extends ExplorerApi<null> {
18
49
  constructor(baseUrl: string);
@@ -43,7 +43,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
43
43
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.explorer = exports.node = exports.ExplorerProvider = exports.NodeProvider = void 0;
46
+ exports.explorer = exports.node = exports.ExplorerProvider = exports.RemoteNodeProvider = exports.NodeProvider = void 0;
47
47
  const api_alephium_1 = require("./api-alephium");
48
48
  const api_explorer_1 = require("./api-explorer");
49
49
  function initializeNodeApi(baseUrl, apiKey) {
@@ -56,8 +56,14 @@ function initializeNodeApi(baseUrl, apiKey) {
56
56
  return nodeApi;
57
57
  }
58
58
  class NodeProvider {
59
- constructor(baseUrl, apiKey) {
60
- const nodeApi = initializeNodeApi(baseUrl, apiKey);
59
+ constructor(param0, apiKey) {
60
+ let nodeApi;
61
+ if (typeof param0 === 'string') {
62
+ nodeApi = initializeNodeApi(param0, apiKey);
63
+ }
64
+ else {
65
+ nodeApi = param0;
66
+ }
61
67
  this.wallets = nodeApi.wallets;
62
68
  this.infos = nodeApi.infos;
63
69
  this.blockflow = nodeApi.blockflow;
@@ -69,22 +75,27 @@ class NodeProvider {
69
75
  this.miners = nodeApi.miners;
70
76
  this.events = nodeApi.events;
71
77
  }
72
- // Have to duplicate the code above due to proxy pattern
73
- reset(baseUrl, apiKey) {
74
- const nodeApi = initializeNodeApi(baseUrl, apiKey);
75
- this.wallets = nodeApi.wallets;
76
- this.infos = nodeApi.infos;
77
- this.blockflow = nodeApi.blockflow;
78
- this.addresses = nodeApi.addresses;
79
- this.transactions = nodeApi.transactions;
80
- this.contracts = nodeApi.contracts;
81
- this.multisig = nodeApi.multisig;
82
- this.utils = nodeApi.utils;
83
- this.miners = nodeApi.miners;
84
- this.events = nodeApi.events;
78
+ // This can prevent the proxied node provider from being modified
79
+ static Proxy(nodeProvider) {
80
+ return new NodeProvider(nodeProvider);
85
81
  }
86
82
  }
87
83
  exports.NodeProvider = NodeProvider;
84
+ class RemoteNodeProvider {
85
+ constructor(request) {
86
+ const fakeNodeProvide = new NodeProvider('https://1.2.3.4:12973');
87
+ Object.assign(this, fakeNodeProvide); // Initialize the class
88
+ // Update class properties to forward requests
89
+ for (const [path, pathObject] of Object.entries(this)) {
90
+ for (const method of Object.keys(pathObject)) {
91
+ pathObject[`${method}`] = async (params) => {
92
+ return request({ path, method, params });
93
+ };
94
+ }
95
+ }
96
+ }
97
+ }
98
+ exports.RemoteNodeProvider = RemoteNodeProvider;
88
99
  // TODO: use proxy provider once the endpoints are refined.
89
100
  class ExplorerProvider extends api_explorer_1.Api {
90
101
  constructor(baseUrl) {
@@ -121,8 +121,8 @@ export declare class Contract extends Artifact {
121
121
  static ContractDestroyedEvent: EventSig;
122
122
  static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined): Promise<ContractEventByTxId>;
123
123
  fromTestContractResult(methodIndex: number, result: node.TestContractResult): Promise<TestContractResult>;
124
- paramsForDeployment(params: BuildDeployContractTx): Promise<SignDeployContractTxParams>;
125
- transactionForDeployment(signer: SignerProvider, params: Omit<BuildDeployContractTx, 'signerAddress'>): Promise<DeployContractTransaction>;
124
+ txParamsForDeployment(signer: SignerProvider, params: Omit<BuildDeployContractTx, 'signerAddress'>): Promise<SignDeployContractTxParams>;
125
+ deploy(signer: SignerProvider, params: Omit<BuildDeployContractTx, 'signerAddress'>): Promise<DeployContractTransaction>;
126
126
  buildByteCodeToDeploy(initialFields: Fields): string;
127
127
  }
128
128
  export declare class Script extends Artifact {
@@ -134,8 +134,8 @@ export declare class Script extends Artifact {
134
134
  static fromJson(artifact: any, bytecodeDebugPatch?: string): Script;
135
135
  static fromArtifactFile(path: string, bytecodeDebugPatch: string): Promise<Script>;
136
136
  toString(): string;
137
- paramsForDeployment(params: BuildExecuteScriptTx): Promise<SignExecuteScriptTxParams>;
138
- transactionForDeployment(signer: SignerProvider, params: Omit<BuildExecuteScriptTx, 'signerAddress'>): Promise<BuildScriptTxResult>;
137
+ txParamsForExecution(signer: SignerProvider, params: Omit<BuildExecuteScriptTx, 'signerAddress'>): Promise<SignExecuteScriptTxParams>;
138
+ execute(signer: SignerProvider, params: Omit<BuildExecuteScriptTx, 'signerAddress'>): Promise<BuildScriptTxResult>;
139
139
  buildByteCodeToDeploy(initialFields: Fields): string;
140
140
  }
141
141
  export interface Asset {
@@ -582,10 +582,10 @@ class Contract extends Artifact {
582
582
  debugMessages: result.debugMessages
583
583
  };
584
584
  }
585
- async paramsForDeployment(params) {
585
+ async txParamsForDeployment(signer, params) {
586
586
  const bytecode = this.buildByteCodeToDeploy(params.initialFields ? params.initialFields : {});
587
587
  const signerParams = {
588
- signerAddress: params.signerAddress,
588
+ signerAddress: (await signer.getSelectedAccount()).address,
589
589
  bytecode: bytecode,
590
590
  initialAttoAlphAmount: extractOptionalNumber256(params.initialAttoAlphAmount),
591
591
  issueTokenAmount: extractOptionalNumber256(params.issueTokenAmount),
@@ -595,13 +595,9 @@ class Contract extends Artifact {
595
595
  };
596
596
  return signerParams;
597
597
  }
598
- async transactionForDeployment(signer, params) {
599
- const signerParams = await this.paramsForDeployment({
600
- ...params,
601
- signerAddress: (await signer.getSelectedAccount()).address
602
- });
603
- const response = await signer.buildContractCreationTx(signerParams);
604
- return fromApiDeployContractUnsignedTx(response);
598
+ async deploy(signer, params) {
599
+ const signerParams = await this.txParamsForDeployment(signer, params);
600
+ return signer.signAndSubmitDeployContractTx(signerParams);
605
601
  }
606
602
  buildByteCodeToDeploy(initialFields) {
607
603
  return ralph.buildContractByteCode(this.bytecode, initialFields, this.fieldsSig);
@@ -652,9 +648,9 @@ class Script extends Artifact {
652
648
  };
653
649
  return JSON.stringify(object, null, 2);
654
650
  }
655
- async paramsForDeployment(params) {
651
+ async txParamsForExecution(signer, params) {
656
652
  const signerParams = {
657
- signerAddress: params.signerAddress,
653
+ signerAddress: (await signer.getSelectedAccount()).address,
658
654
  bytecode: this.buildByteCodeToDeploy(params.initialFields ? params.initialFields : {}),
659
655
  attoAlphAmount: extractOptionalNumber256(params.attoAlphAmount),
660
656
  tokens: (0, api_1.toApiTokens)(params.tokens),
@@ -663,12 +659,9 @@ class Script extends Artifact {
663
659
  };
664
660
  return signerParams;
665
661
  }
666
- async transactionForDeployment(signer, params) {
667
- const signerParams = await this.paramsForDeployment({
668
- ...params,
669
- signerAddress: (await signer.getSelectedAccount()).address
670
- });
671
- return await signer.buildScriptTx(signerParams);
662
+ async execute(signer, params) {
663
+ const signerParams = await this.txParamsForExecution(signer, params);
664
+ return await signer.signAndSubmitExecuteScriptTx(signerParams);
672
665
  }
673
666
  buildByteCodeToDeploy(initialFields) {
674
667
  return ralph.buildScriptByteCode(this.bytecodeTemplate, initialFields, this.fieldsSig);
@@ -758,8 +751,5 @@ function fromApiOutput(output) {
758
751
  throw new Error(`Unknown output type: ${output}`);
759
752
  }
760
753
  }
761
- function fromApiDeployContractUnsignedTx(result) {
762
- return { ...result, contractId: (0, utils_1.binToHex)((0, utils_1.contractIdFromAddress)(result.contractAddress)) };
763
- }
764
754
  (0, utils_1.assertType)();
765
755
  (0, utils_1.assertType)();
@@ -1,13 +1,6 @@
1
1
  import { NodeProvider, Number256, Token } from '../api';
2
2
  import { node } from '../api';
3
3
  export declare type OutputRef = node.OutputRef;
4
- export interface SignResult {
5
- fromGroup: number;
6
- toGroup: number;
7
- unsignedTx: string;
8
- txId: string;
9
- signature: string;
10
- }
11
4
  export interface Account {
12
5
  address: string;
13
6
  group: number;
@@ -16,8 +9,6 @@ export interface Account {
16
9
  export declare type SignerAddress = {
17
10
  signerAddress: string;
18
11
  };
19
- export declare type GetAccountsParams = undefined;
20
- export declare type GetAccountsResult = Account[];
21
12
  export interface SignTransferTxParams {
22
13
  signerAddress: string;
23
14
  destinations: Destination[];
@@ -31,6 +22,8 @@ export interface SignTransferTxResult {
31
22
  unsignedTx: string;
32
23
  txId: string;
33
24
  signature: string;
25
+ gasAmount: number;
26
+ gasPrice: Number256;
34
27
  }
35
28
  export interface SignDeployContractTxParams {
36
29
  signerAddress: string;
@@ -49,6 +42,8 @@ export interface SignDeployContractTxResult {
49
42
  signature: string;
50
43
  contractId: string;
51
44
  contractAddress: string;
45
+ gasAmount: number;
46
+ gasPrice: Number256;
52
47
  }
53
48
  export interface SignExecuteScriptTxParams {
54
49
  signerAddress: string;
@@ -64,6 +59,8 @@ export interface SignExecuteScriptTxResult {
64
59
  unsignedTx: string;
65
60
  txId: string;
66
61
  signature: string;
62
+ gasAmount: number;
63
+ gasPrice: Number256;
67
64
  }
68
65
  export interface SignUnsignedTxParams {
69
66
  signerAddress: string;
@@ -75,13 +72,8 @@ export interface SignUnsignedTxResult {
75
72
  unsignedTx: string;
76
73
  txId: string;
77
74
  signature: string;
78
- }
79
- export interface SignHexStringParams {
80
- signerAddress: string;
81
- hexString: string;
82
- }
83
- export interface SignHexStringResult {
84
- signature: string;
75
+ gasAmount: number;
76
+ gasPrice: Number256;
85
77
  }
86
78
  export interface SignMessageParams {
87
79
  signerAddress: string;
@@ -90,23 +82,34 @@ export interface SignMessageParams {
90
82
  export interface SignMessageResult {
91
83
  signature: string;
92
84
  }
93
- export interface SignerProviderWithoutNodeProvider {
85
+ export interface SubmitTransactionParams {
86
+ unsignedTx: string;
87
+ signature: string;
88
+ }
89
+ export interface SubmissionResult {
90
+ txId: string;
91
+ fromGroup: number;
92
+ toGroup: number;
93
+ }
94
+ export interface SignerProvider {
95
+ get nodeProvider(): NodeProvider | undefined;
94
96
  getSelectedAccount(): Promise<Account>;
95
- signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
96
- signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
97
- signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
97
+ signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
98
+ signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
99
+ signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
100
+ signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
98
101
  signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
99
- signHexString(params: SignHexStringParams): Promise<SignHexStringResult>;
100
102
  signMessage(params: SignMessageParams): Promise<SignMessageResult>;
101
103
  }
102
- export declare abstract class SignerProvider implements SignerProviderWithoutNodeProvider {
103
- abstract get nodeProvider(): NodeProvider;
104
+ export declare abstract class SignerProviderSimple implements SignerProvider {
105
+ abstract get nodeProvider(): NodeProvider | undefined;
104
106
  abstract getSelectedAccount(): Promise<Account>;
105
- submitTransaction(unsignedTx: string, signature: string): Promise<SubmissionResult>;
106
- signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SubmissionResult>;
107
- signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SubmissionResult>;
108
- signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SubmissionResult>;
109
- signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SubmissionResult>;
107
+ private getNodeProvider;
108
+ submitTransaction(params: SubmitTransactionParams): Promise<SubmissionResult>;
109
+ signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
110
+ signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>;
111
+ signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
112
+ signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
110
113
  private usePublicKey;
111
114
  signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>;
112
115
  buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult>;
@@ -115,27 +118,14 @@ export declare abstract class SignerProvider implements SignerProviderWithoutNod
115
118
  signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>;
116
119
  buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult>;
117
120
  signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>;
118
- protected handleSign(response: {
119
- fromGroup: number;
120
- toGroup: number;
121
- signerAddress: string;
122
- unsignedTx: string;
123
- txId: string;
124
- }): Promise<SignResult>;
125
- signHexString(params: SignHexStringParams): Promise<SignHexStringResult>;
126
121
  signMessage(params: SignMessageParams): Promise<SignMessageResult>;
127
122
  abstract signRaw(signerAddress: string, hexString: string): Promise<string>;
128
123
  }
129
- export declare abstract class SignerProviderWithMultipleAccounts extends SignerProvider {
124
+ export declare abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
130
125
  abstract getAccounts(): Promise<Account[]>;
131
126
  getAccount(signerAddress: string): Promise<Account>;
132
127
  abstract setSelectedAccount(address: string): Promise<void>;
133
128
  }
134
- export interface SubmissionResult {
135
- txId: string;
136
- fromGroup: number;
137
- toGroup: number;
138
- }
139
129
  export declare function verifyHexString(hexString: string, publicKey: string, signature: string): boolean;
140
130
  export declare function verifySignedMessage(message: string, publicKey: string, signature: string): boolean;
141
131
  export interface Destination {
@@ -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.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.verifyHexString = exports.SignerProviderWithMultipleAccounts = exports.SignerProvider = void 0;
46
+ exports.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.verifyHexString = exports.SignerProviderWithMultipleAccounts = exports.SignerProviderSimple = void 0;
47
47
  const elliptic_1 = require("elliptic");
48
48
  const api_1 = require("../api");
49
49
  const utils = __importStar(require("../utils"));
@@ -57,31 +57,38 @@ const ec = new elliptic_1.ec('secp256k1');
57
57
  (0, utils_1.assertType)();
58
58
  (0, utils_1.assertType)();
59
59
  (0, utils_1.assertType)();
60
+ utils_1.assertType;
60
61
  (0, utils_1.assertType)();
61
- (0, utils_1.assertType)();
62
- (0, utils_1.assertType)();
63
- (0, utils_1.assertType)();
64
- (0, utils_1.assertType)();
65
- class SignerProvider {
66
- async submitTransaction(unsignedTx, signature) {
67
- const params = { unsignedTx: unsignedTx, signature: signature };
68
- return this.nodeProvider.transactions.postTransactionsSubmit(params);
62
+ class SignerProviderSimple {
63
+ getNodeProvider() {
64
+ if (this.nodeProvider === undefined) {
65
+ throw Error('The signer does not contain a node provider');
66
+ }
67
+ return this.nodeProvider;
68
+ }
69
+ async submitTransaction(params) {
70
+ const data = { unsignedTx: params.unsignedTx, signature: params.signature };
71
+ return this.getNodeProvider().transactions.postTransactionsSubmit(data);
69
72
  }
70
73
  async signAndSubmitTransferTx(params) {
71
74
  const signResult = await this.signTransferTx(params);
72
- return this.submitTransaction(signResult.unsignedTx, signResult.signature);
75
+ await this.submitTransaction(signResult);
76
+ return signResult;
73
77
  }
74
78
  async signAndSubmitDeployContractTx(params) {
75
79
  const signResult = await this.signDeployContractTx(params);
76
- return this.submitTransaction(signResult.unsignedTx, signResult.signature);
80
+ await this.submitTransaction(signResult);
81
+ return signResult;
77
82
  }
78
83
  async signAndSubmitExecuteScriptTx(params) {
79
84
  const signResult = await this.signExecuteScriptTx(params);
80
- return this.submitTransaction(signResult.unsignedTx, signResult.signature);
85
+ await this.submitTransaction(signResult);
86
+ return signResult;
81
87
  }
82
88
  async signAndSubmitUnsignedTx(params) {
83
89
  const signResult = await this.signUnsignedTx(params);
84
- return this.submitTransaction(signResult.unsignedTx, signResult.signature);
90
+ await this.submitTransaction(signResult);
91
+ return signResult;
85
92
  }
86
93
  async usePublicKey(params) {
87
94
  const { signerAddress, ...restParams } = params;
@@ -95,7 +102,8 @@ class SignerProvider {
95
102
  }
96
103
  async signTransferTx(params) {
97
104
  const response = await this.buildTransferTx(params);
98
- return this.handleSign({ signerAddress: params.signerAddress, ...response });
105
+ const signature = await this.signRaw(params.signerAddress, response.txId);
106
+ return { ...response, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
99
107
  }
100
108
  async buildTransferTx(params) {
101
109
  const data = {
@@ -103,13 +111,13 @@ class SignerProvider {
103
111
  destinations: toApiDestinations(params.destinations),
104
112
  gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
105
113
  };
106
- return this.nodeProvider.transactions.postTransactionsBuild(data);
114
+ return this.getNodeProvider().transactions.postTransactionsBuild(data);
107
115
  }
108
116
  async signDeployContractTx(params) {
109
117
  const response = await this.buildContractCreationTx(params);
110
- const result = await this.handleSign({ signerAddress: params.signerAddress, ...response });
118
+ const signature = await this.signRaw(params.signerAddress, response.txId);
111
119
  const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress));
112
- return { ...result, contractId: contractId, contractAddress: response.contractAddress };
120
+ return { ...response, contractId, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
113
121
  }
114
122
  async buildContractCreationTx(params) {
115
123
  const data = {
@@ -119,48 +127,36 @@ class SignerProvider {
119
127
  issueTokenAmount: (0, api_1.toApiNumber256Optional)(params.issueTokenAmount),
120
128
  gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
121
129
  };
122
- return this.nodeProvider.contracts.postContractsUnsignedTxDeployContract(data);
130
+ return this.getNodeProvider().contracts.postContractsUnsignedTxDeployContract(data);
123
131
  }
124
132
  async signExecuteScriptTx(params) {
125
133
  const response = await this.buildScriptTx(params);
126
- return this.handleSign({ signerAddress: params.signerAddress, ...response });
134
+ const signature = await this.signRaw(params.signerAddress, response.txId);
135
+ return { ...response, signature, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
127
136
  }
128
137
  async buildScriptTx(params) {
129
138
  const data = {
130
139
  ...(await this.usePublicKey(params)),
131
140
  tokens: (0, api_1.toApiTokens)(params.tokens)
132
141
  };
133
- return this.nodeProvider.contracts.postContractsUnsignedTxExecuteScript(data);
142
+ return this.getNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data);
134
143
  }
135
144
  // in general, wallet should show the decoded information to user for confirmation
136
145
  // please overwrite this function for real wallet
137
146
  async signUnsignedTx(params) {
138
147
  const data = { unsignedTx: params.unsignedTx };
139
- const decoded = await this.nodeProvider.transactions.postTransactionsDecodeUnsignedTx(data);
140
- return this.handleSign({
148
+ const decoded = await this.getNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data);
149
+ const signature = await this.signRaw(params.signerAddress, decoded.unsignedTx.txId);
150
+ return {
141
151
  fromGroup: decoded.fromGroup,
142
152
  toGroup: decoded.toGroup,
143
- signerAddress: params.signerAddress,
144
153
  unsignedTx: params.unsignedTx,
145
- txId: decoded.unsignedTx.txId
146
- });
147
- }
148
- async handleSign(response) {
149
- // sign the tx
150
- const signature = await this.signRaw(response.signerAddress, response.txId);
151
- // return the signature back to the provider
152
- return {
153
- fromGroup: response.fromGroup,
154
- toGroup: response.toGroup,
155
- unsignedTx: response.unsignedTx,
156
- txId: response.txId,
157
- signature: signature
154
+ txId: decoded.unsignedTx.txId,
155
+ signature,
156
+ gasAmount: decoded.unsignedTx.gasAmount,
157
+ gasPrice: (0, api_1.fromApiNumber256)(decoded.unsignedTx.gasPrice)
158
158
  };
159
159
  }
160
- async signHexString(params) {
161
- const signature = await this.signRaw(params.signerAddress, params.hexString);
162
- return { signature: signature };
163
- }
164
160
  async signMessage(params) {
165
161
  const extendedMessage = extendMessage(params.message);
166
162
  const messageHash = blakejs_1.default.blake2b(extendedMessage, undefined, 32);
@@ -168,8 +164,8 @@ class SignerProvider {
168
164
  return { signature: signature };
169
165
  }
170
166
  }
171
- exports.SignerProvider = SignerProvider;
172
- class SignerProviderWithMultipleAccounts extends SignerProvider {
167
+ exports.SignerProviderSimple = SignerProviderSimple;
168
+ class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
173
169
  async getAccount(signerAddress) {
174
170
  const accounts = await this.getAccounts();
175
171
  const account = accounts.find((a) => a.address === signerAddress);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.2.0-rc.32",
3
+ "version": "0.2.0-rc.34",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
package/src/api/index.ts CHANGED
@@ -29,20 +29,40 @@ function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
29
29
  return nodeApi
30
30
  }
31
31
 
32
- export class NodeProvider {
33
- wallets: NodeApi<string>['wallets']
34
- infos: NodeApi<string>['infos']
35
- blockflow: NodeApi<string>['blockflow']
36
- addresses: NodeApi<string>['addresses']
37
- transactions: NodeApi<string>['transactions']
38
- contracts: NodeApi<string>['contracts']
39
- multisig: NodeApi<string>['multisig']
40
- utils: NodeApi<string>['utils']
41
- miners: NodeApi<string>['miners']
42
- events: NodeApi<string>['events']
43
-
44
- constructor(baseUrl: string, apiKey?: string) {
45
- const nodeApi = initializeNodeApi(baseUrl, apiKey)
32
+ interface INodeProvider {
33
+ readonly wallets: NodeApi<string>['wallets']
34
+ readonly infos: NodeApi<string>['infos']
35
+ readonly blockflow: NodeApi<string>['blockflow']
36
+ readonly addresses: NodeApi<string>['addresses']
37
+ readonly transactions: NodeApi<string>['transactions']
38
+ readonly contracts: NodeApi<string>['contracts']
39
+ readonly multisig: NodeApi<string>['multisig']
40
+ readonly utils: NodeApi<string>['utils']
41
+ readonly miners: NodeApi<string>['miners']
42
+ readonly events: NodeApi<string>['events']
43
+ }
44
+
45
+ export class NodeProvider implements INodeProvider {
46
+ readonly wallets: NodeApi<string>['wallets']
47
+ readonly infos: NodeApi<string>['infos']
48
+ readonly blockflow: NodeApi<string>['blockflow']
49
+ readonly addresses: NodeApi<string>['addresses']
50
+ readonly transactions: NodeApi<string>['transactions']
51
+ readonly contracts: NodeApi<string>['contracts']
52
+ readonly multisig: NodeApi<string>['multisig']
53
+ readonly utils: NodeApi<string>['utils']
54
+ readonly miners: NodeApi<string>['miners']
55
+ readonly events: NodeApi<string>['events']
56
+
57
+ constructor(provider: INodeProvider)
58
+ constructor(baseUrl: string, apiKey?: string)
59
+ constructor(param0: string | INodeProvider, apiKey?: string) {
60
+ let nodeApi: INodeProvider
61
+ if (typeof param0 === 'string') {
62
+ nodeApi = initializeNodeApi(param0, apiKey)
63
+ } else {
64
+ nodeApi = param0 as INodeProvider
65
+ }
46
66
 
47
67
  this.wallets = nodeApi.wallets
48
68
  this.infos = nodeApi.infos
@@ -56,20 +76,42 @@ export class NodeProvider {
56
76
  this.events = nodeApi.events
57
77
  }
58
78
 
59
- // Have to duplicate the code above due to proxy pattern
60
- reset(baseUrl: string, apiKey?: string): void {
61
- const nodeApi = initializeNodeApi(baseUrl, apiKey)
79
+ // This can prevent the proxied node provider from being modified
80
+ static Proxy(nodeProvider: NodeProvider): NodeProvider {
81
+ return new NodeProvider(nodeProvider)
82
+ }
83
+ }
62
84
 
63
- this.wallets = nodeApi.wallets
64
- this.infos = nodeApi.infos
65
- this.blockflow = nodeApi.blockflow
66
- this.addresses = nodeApi.addresses
67
- this.transactions = nodeApi.transactions
68
- this.contracts = nodeApi.contracts
69
- this.multisig = nodeApi.multisig
70
- this.utils = nodeApi.utils
71
- this.miners = nodeApi.miners
72
- this.events = nodeApi.events
85
+ export interface RequestArguments {
86
+ path: string
87
+ method: string
88
+ params?: any
89
+ }
90
+
91
+ export class RemoteNodeProvider implements INodeProvider {
92
+ readonly wallets!: NodeApi<string>['wallets']
93
+ readonly infos!: NodeApi<string>['infos']
94
+ readonly blockflow!: NodeApi<string>['blockflow']
95
+ readonly addresses!: NodeApi<string>['addresses']
96
+ readonly transactions!: NodeApi<string>['transactions']
97
+ readonly contracts!: NodeApi<string>['contracts']
98
+ readonly multisig!: NodeApi<string>['multisig']
99
+ readonly utils!: NodeApi<string>['utils']
100
+ readonly miners!: NodeApi<string>['miners']
101
+ readonly events!: NodeApi<string>['events']
102
+
103
+ constructor(request: (request: RequestArguments) => Promise<any>) {
104
+ const fakeNodeProvide = new NodeProvider('https://1.2.3.4:12973')
105
+ Object.assign(this, fakeNodeProvide) // Initialize the class
106
+
107
+ // Update class properties to forward requests
108
+ for (const [path, pathObject] of Object.entries(this)) {
109
+ for (const method of Object.keys(pathObject)) {
110
+ pathObject[`${method}`] = async (params: any): Promise<any> => {
111
+ return request({ path, method, params })
112
+ }
113
+ }
114
+ }
73
115
  }
74
116
  }
75
117