@alephium/web3 0.2.0-rc.21 → 0.2.0-rc.23

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.
@@ -598,7 +598,7 @@ class Contract extends Artifact {
598
598
  async transactionForDeployment(signer, params) {
599
599
  const signerParams = await this.paramsForDeployment({
600
600
  ...params,
601
- signerAddress: (await signer.getAccounts())[0].address
601
+ signerAddress: (await signer.getActiveAccount()).address
602
602
  });
603
603
  const response = await signer.buildContractCreationTx(signerParams);
604
604
  return fromApiDeployContractUnsignedTx(response);
@@ -666,7 +666,7 @@ class Script extends Artifact {
666
666
  async transactionForDeployment(signer, params) {
667
667
  const signerParams = await this.paramsForDeployment({
668
668
  ...params,
669
- signerAddress: (await signer.getAccounts())[0].address
669
+ signerAddress: (await signer.getActiveAccount()).address
670
670
  });
671
671
  return await signer.buildScriptTx(signerParams);
672
672
  }
@@ -18,6 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.subscribeToEvents = exports.EventSubscription = void 0;
21
+ const __1 = require("..");
21
22
  const utils_1 = require("../utils");
22
23
  class EventSubscription extends utils_1.Subscription {
23
24
  constructor(options, contractAddress, fromCount) {
@@ -37,7 +38,7 @@ class EventSubscription extends utils_1.Subscription {
37
38
  }
38
39
  async polling() {
39
40
  try {
40
- const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
41
+ const events = await __1.web3.getCurrentNodeProvider().events.getEventsContractContractaddress(this.contractAddress, {
41
42
  start: this.fromCount
42
43
  });
43
44
  if (this.cancelled) {
@@ -1,4 +1,4 @@
1
- import { NodeProvider, Number256, Token } from '../api';
1
+ import { Number256, Token } from '../api';
2
2
  import { node } from '../api';
3
3
  export declare type OutputRef = node.OutputRef;
4
4
  export interface SignResult {
@@ -107,12 +107,14 @@ export interface SignerProvider {
107
107
  signMessage(params: SignMessageParams): Promise<SignMessageResult>;
108
108
  }
109
109
  export declare abstract class SignerWithNodeProvider implements SignerProvider {
110
- readonly provider: NodeProvider;
111
110
  alwaysSubmitTx: boolean;
112
111
  abstract getAccounts(): Promise<Account[]>;
113
112
  getAccount(signerAddress: string): Promise<Account>;
113
+ abstract setActiveAccount(addressIndex: number): Promise<void>;
114
+ abstract setActiveAccount(address: string): Promise<void>;
115
+ abstract setActiveAccount(input: string | number): Promise<void>;
116
+ abstract getActiveAccount(): Promise<Account>;
114
117
  constructor(alwaysSubmitTx: boolean);
115
- private defaultSignerAddress;
116
118
  submitTransaction(unsignedTx: string, signerAddress?: string): Promise<SubmissionResult>;
117
119
  private shouldSubmitTx;
118
120
  private usePublicKey;
@@ -49,7 +49,7 @@ const api_1 = require("../api");
49
49
  const utils = __importStar(require("../utils"));
50
50
  const utils_1 = require("../utils");
51
51
  const blakejs_1 = __importDefault(require("blakejs"));
52
- const global_1 = require("../global");
52
+ const __1 = require("..");
53
53
  const ec = new elliptic_1.ec('secp256k1');
54
54
  (0, utils_1.assertType)();
55
55
  (0, utils_1.assertType)();
@@ -65,7 +65,6 @@ const ec = new elliptic_1.ec('secp256k1');
65
65
  (0, utils_1.assertType)();
66
66
  class SignerWithNodeProvider {
67
67
  constructor(alwaysSubmitTx) {
68
- this.provider = (0, global_1.getCurrentNodeProvider)();
69
68
  this.alwaysSubmitTx = alwaysSubmitTx;
70
69
  }
71
70
  async getAccount(signerAddress) {
@@ -78,16 +77,15 @@ class SignerWithNodeProvider {
78
77
  return account;
79
78
  }
80
79
  }
81
- async defaultSignerAddress() {
82
- return (await this.getAccounts())[0].address;
83
- }
84
80
  async submitTransaction(unsignedTx, signerAddress) {
85
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx });
81
+ const decoded = await __1.web3
82
+ .getCurrentNodeProvider()
83
+ .transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx });
86
84
  const txId = decoded.unsignedTx.txId;
87
- const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress();
85
+ const address = typeof signerAddress !== 'undefined' ? signerAddress : (await this.getActiveAccount()).address;
88
86
  const signature = await this.signRaw(address, txId);
89
87
  const params = { unsignedTx: unsignedTx, signature: signature };
90
- return this.provider.transactions.postTransactionsSubmit(params);
88
+ return __1.web3.getCurrentNodeProvider().transactions.postTransactionsSubmit(params);
91
89
  }
92
90
  shouldSubmitTx(params) {
93
91
  return this.alwaysSubmitTx || (params.submitTx ? params.submitTx : true);
@@ -113,7 +111,7 @@ class SignerWithNodeProvider {
113
111
  destinations: toApiDestinations(params.destinations),
114
112
  gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
115
113
  };
116
- return this.provider.transactions.postTransactionsBuild(data);
114
+ return __1.web3.getCurrentNodeProvider().transactions.postTransactionsBuild(data);
117
115
  }
118
116
  async signDeployContractTx(params) {
119
117
  const response = await this.buildContractCreationTx(params);
@@ -129,7 +127,7 @@ class SignerWithNodeProvider {
129
127
  issueTokenAmount: (0, api_1.toApiNumber256Optional)(params.issueTokenAmount),
130
128
  gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
131
129
  };
132
- return this.provider.contracts.postContractsUnsignedTxDeployContract(data);
130
+ return __1.web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxDeployContract(data);
133
131
  }
134
132
  async signExecuteScriptTx(params) {
135
133
  const response = await this.buildScriptTx(params);
@@ -140,13 +138,13 @@ class SignerWithNodeProvider {
140
138
  ...(await this.usePublicKey(params)),
141
139
  tokens: (0, api_1.toApiTokens)(params.tokens)
142
140
  };
143
- return this.provider.contracts.postContractsUnsignedTxExecuteScript(data);
141
+ return __1.web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data);
144
142
  }
145
143
  // in general, wallet should show the decoded information to user for confirmation
146
144
  // please overwrite this function for real wallet
147
145
  async signUnsignedTx(params) {
148
146
  const data = { unsignedTx: params.unsignedTx };
149
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx(data);
147
+ const decoded = await __1.web3.getCurrentNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data);
150
148
  return this.handleSign({
151
149
  fromGroup: decoded.fromGroup,
152
150
  toGroup: decoded.toGroup,
@@ -161,7 +159,7 @@ class SignerWithNodeProvider {
161
159
  const signature = await this.signRaw(response.signerAddress, response.txId);
162
160
  // submit the tx if required
163
161
  if (submitTx) {
164
- await this.provider.transactions.postTransactionsSubmit({
162
+ await __1.web3.getCurrentNodeProvider().transactions.postTransactionsSubmit({
165
163
  unsignedTx: response.unsignedTx,
166
164
  signature: signature
167
165
  });
@@ -18,6 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.subscribeToTxStatus = exports.TxStatusSubscription = void 0;
21
+ const __1 = require("..");
21
22
  const utils_1 = require("../utils");
22
23
  class TxStatusSubscription extends utils_1.Subscription {
23
24
  constructor(options, txId, fromGroup, toGroup) {
@@ -29,7 +30,7 @@ class TxStatusSubscription extends utils_1.Subscription {
29
30
  }
30
31
  async polling() {
31
32
  try {
32
- const txStatus = await this.provider.transactions.getTransactionsStatus({
33
+ const txStatus = await __1.web3.getCurrentNodeProvider().transactions.getTransactionsStatus({
33
34
  txId: this.txId,
34
35
  fromGroup: this.fromGroup,
35
36
  toGroup: this.toGroup
@@ -1,5 +1,4 @@
1
1
  import EventEmitter from 'eventemitter3';
2
- import { NodeProvider } from '../api';
3
2
  declare type MessageCallback<Message> = (message: Message) => Promise<void>;
4
3
  declare type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>;
5
4
  export interface SubscribeOptions<Message> {
@@ -8,7 +7,6 @@ export interface SubscribeOptions<Message> {
8
7
  errorCallback: ErrorCallback<Message>;
9
8
  }
10
9
  export declare abstract class Subscription<Message> {
11
- provider: NodeProvider;
12
10
  pollingInterval: number;
13
11
  protected messageCallback: MessageCallback<Message>;
14
12
  protected errorCallback: ErrorCallback<Message>;
@@ -22,10 +22,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.Subscription = void 0;
24
24
  const eventemitter3_1 = __importDefault(require("eventemitter3"));
25
- const global_1 = require("../global");
26
25
  class Subscription {
27
26
  constructor(options) {
28
- this.provider = (0, global_1.getCurrentNodeProvider)();
29
27
  this.pollingInterval = options.pollingInterval;
30
28
  this.messageCallback = options.messageCallback;
31
29
  this.errorCallback = options.errorCallback;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.2.0-rc.21",
3
+ "version": "0.2.0-rc.23",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -828,7 +828,7 @@ export class Contract extends Artifact {
828
828
  ): Promise<DeployContractTransaction> {
829
829
  const signerParams = await this.paramsForDeployment({
830
830
  ...params,
831
- signerAddress: (await signer.getAccounts())[0].address
831
+ signerAddress: (await signer.getActiveAccount()).address
832
832
  })
833
833
  const response = await signer.buildContractCreationTx(signerParams)
834
834
  return fromApiDeployContractUnsignedTx(response)
@@ -914,7 +914,7 @@ export class Script extends Artifact {
914
914
  ): Promise<BuildScriptTxResult> {
915
915
  const signerParams = await this.paramsForDeployment({
916
916
  ...params,
917
- signerAddress: (await signer.getAccounts())[0].address
917
+ signerAddress: (await signer.getActiveAccount()).address
918
918
  })
919
919
  return await signer.buildScriptTx(signerParams)
920
920
  }
@@ -16,6 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
+ import { web3 } from '..'
19
20
  import { node } from '../api'
20
21
  import { Subscription, SubscribeOptions } from '../utils'
21
22
 
@@ -44,7 +45,7 @@ export class EventSubscription extends Subscription<node.ContractEvent> {
44
45
 
45
46
  override async polling(): Promise<void> {
46
47
  try {
47
- const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
48
+ const events = await web3.getCurrentNodeProvider().events.getEventsContractContractaddress(this.contractAddress, {
48
49
  start: this.fromCount
49
50
  })
50
51
  if (this.cancelled) {
@@ -20,7 +20,6 @@ import { ec as EC } from 'elliptic'
20
20
  import {
21
21
  fromApiNumber256,
22
22
  fromApiTokens,
23
- NodeProvider,
24
23
  Number256,
25
24
  toApiNumber256,
26
25
  toApiNumber256Optional,
@@ -31,7 +30,7 @@ import { node } from '../api'
31
30
  import * as utils from '../utils'
32
31
  import { Eq, assertType } from '../utils'
33
32
  import blake from 'blakejs'
34
- import { getCurrentNodeProvider } from '../global'
33
+ import { web3 } from '..'
35
34
 
36
35
  export type OutputRef = node.OutputRef
37
36
 
@@ -163,7 +162,6 @@ export interface SignerProvider {
163
162
  }
164
163
 
165
164
  export abstract class SignerWithNodeProvider implements SignerProvider {
166
- readonly provider: NodeProvider
167
165
  alwaysSubmitTx: boolean
168
166
 
169
167
  abstract getAccounts(): Promise<Account[]>
@@ -178,23 +176,26 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
178
176
  }
179
177
  }
180
178
 
179
+ abstract setActiveAccount(addressIndex: number): Promise<void>
180
+ abstract setActiveAccount(address: string): Promise<void>
181
+ abstract setActiveAccount(input: string | number): Promise<void>
182
+
183
+ abstract getActiveAccount(): Promise<Account>
184
+
181
185
  constructor(alwaysSubmitTx: boolean) {
182
- this.provider = getCurrentNodeProvider()
183
186
  this.alwaysSubmitTx = alwaysSubmitTx
184
187
  }
185
188
 
186
- private async defaultSignerAddress(): Promise<string> {
187
- return (await this.getAccounts())[0].address
188
- }
189
-
190
189
  async submitTransaction(unsignedTx: string, signerAddress?: string): Promise<SubmissionResult> {
191
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx })
190
+ const decoded = await web3
191
+ .getCurrentNodeProvider()
192
+ .transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx })
192
193
  const txId = decoded.unsignedTx.txId
193
194
 
194
- const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress()
195
+ const address = typeof signerAddress !== 'undefined' ? signerAddress : (await this.getActiveAccount()).address
195
196
  const signature = await this.signRaw(address, txId)
196
197
  const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
197
- return this.provider.transactions.postTransactionsSubmit(params)
198
+ return web3.getCurrentNodeProvider().transactions.postTransactionsSubmit(params)
198
199
  }
199
200
 
200
201
  private shouldSubmitTx(params: SubmitTx): boolean {
@@ -225,7 +226,7 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
225
226
  destinations: toApiDestinations(params.destinations),
226
227
  gasPrice: toApiNumber256Optional(params.gasPrice)
227
228
  }
228
- return this.provider.transactions.postTransactionsBuild(data)
229
+ return web3.getCurrentNodeProvider().transactions.postTransactionsBuild(data)
229
230
  }
230
231
 
231
232
  async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
@@ -246,7 +247,7 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
246
247
  issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
247
248
  gasPrice: toApiNumber256Optional(params.gasPrice)
248
249
  }
249
- return this.provider.contracts.postContractsUnsignedTxDeployContract(data)
250
+ return web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxDeployContract(data)
250
251
  }
251
252
 
252
253
  async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
@@ -259,14 +260,14 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
259
260
  ...(await this.usePublicKey(params)),
260
261
  tokens: toApiTokens(params.tokens)
261
262
  }
262
- return this.provider.contracts.postContractsUnsignedTxExecuteScript(data)
263
+ return web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data)
263
264
  }
264
265
 
265
266
  // in general, wallet should show the decoded information to user for confirmation
266
267
  // please overwrite this function for real wallet
267
268
  async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
268
269
  const data = { unsignedTx: params.unsignedTx }
269
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx(data)
270
+ const decoded = await web3.getCurrentNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data)
270
271
  return this.handleSign(
271
272
  {
272
273
  fromGroup: decoded.fromGroup,
@@ -287,7 +288,7 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
287
288
  const signature = await this.signRaw(response.signerAddress, response.txId)
288
289
  // submit the tx if required
289
290
  if (submitTx) {
290
- await this.provider.transactions.postTransactionsSubmit({
291
+ await web3.getCurrentNodeProvider().transactions.postTransactionsSubmit({
291
292
  unsignedTx: response.unsignedTx,
292
293
  signature: signature
293
294
  })
@@ -16,6 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
+ import { web3 } from '..'
19
20
  import { node } from '../api'
20
21
  import { Subscription, SubscribeOptions } from '../utils'
21
22
 
@@ -37,7 +38,7 @@ export class TxStatusSubscription extends Subscription<TxStatus> {
37
38
 
38
39
  override async polling(): Promise<void> {
39
40
  try {
40
- const txStatus = await this.provider.transactions.getTransactionsStatus({
41
+ const txStatus = await web3.getCurrentNodeProvider().transactions.getTransactionsStatus({
41
42
  txId: this.txId,
42
43
  fromGroup: this.fromGroup,
43
44
  toGroup: this.toGroup
@@ -30,7 +30,6 @@ export interface SubscribeOptions<Message> {
30
30
  }
31
31
 
32
32
  export abstract class Subscription<Message> {
33
- provider: NodeProvider
34
33
  pollingInterval: number
35
34
 
36
35
  protected messageCallback: MessageCallback<Message>
@@ -40,7 +39,6 @@ export abstract class Subscription<Message> {
40
39
  protected cancelled: boolean
41
40
 
42
41
  constructor(options: SubscribeOptions<Message>) {
43
- this.provider = getCurrentNodeProvider()
44
42
  this.pollingInterval = options.pollingInterval
45
43
  this.messageCallback = options.messageCallback
46
44
  this.errorCallback = options.errorCallback