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

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,10 +107,13 @@ 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
118
  private defaultSignerAddress;
116
119
  submitTransaction(unsignedTx: string, signerAddress?: string): Promise<SubmissionResult>;
@@ -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) {
@@ -82,12 +81,14 @@ class SignerWithNodeProvider {
82
81
  return (await this.getAccounts())[0].address;
83
82
  }
84
83
  async submitTransaction(unsignedTx, signerAddress) {
85
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx });
84
+ const decoded = await __1.web3
85
+ .getCurrentNodeProvider()
86
+ .transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx });
86
87
  const txId = decoded.unsignedTx.txId;
87
88
  const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress();
88
89
  const signature = await this.signRaw(address, txId);
89
90
  const params = { unsignedTx: unsignedTx, signature: signature };
90
- return this.provider.transactions.postTransactionsSubmit(params);
91
+ return __1.web3.getCurrentNodeProvider().transactions.postTransactionsSubmit(params);
91
92
  }
92
93
  shouldSubmitTx(params) {
93
94
  return this.alwaysSubmitTx || (params.submitTx ? params.submitTx : true);
@@ -113,7 +114,7 @@ class SignerWithNodeProvider {
113
114
  destinations: toApiDestinations(params.destinations),
114
115
  gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
115
116
  };
116
- return this.provider.transactions.postTransactionsBuild(data);
117
+ return __1.web3.getCurrentNodeProvider().transactions.postTransactionsBuild(data);
117
118
  }
118
119
  async signDeployContractTx(params) {
119
120
  const response = await this.buildContractCreationTx(params);
@@ -129,7 +130,7 @@ class SignerWithNodeProvider {
129
130
  issueTokenAmount: (0, api_1.toApiNumber256Optional)(params.issueTokenAmount),
130
131
  gasPrice: (0, api_1.toApiNumber256Optional)(params.gasPrice)
131
132
  };
132
- return this.provider.contracts.postContractsUnsignedTxDeployContract(data);
133
+ return __1.web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxDeployContract(data);
133
134
  }
134
135
  async signExecuteScriptTx(params) {
135
136
  const response = await this.buildScriptTx(params);
@@ -140,13 +141,13 @@ class SignerWithNodeProvider {
140
141
  ...(await this.usePublicKey(params)),
141
142
  tokens: (0, api_1.toApiTokens)(params.tokens)
142
143
  };
143
- return this.provider.contracts.postContractsUnsignedTxExecuteScript(data);
144
+ return __1.web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data);
144
145
  }
145
146
  // in general, wallet should show the decoded information to user for confirmation
146
147
  // please overwrite this function for real wallet
147
148
  async signUnsignedTx(params) {
148
149
  const data = { unsignedTx: params.unsignedTx };
149
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx(data);
150
+ const decoded = await __1.web3.getCurrentNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data);
150
151
  return this.handleSign({
151
152
  fromGroup: decoded.fromGroup,
152
153
  toGroup: decoded.toGroup,
@@ -161,7 +162,7 @@ class SignerWithNodeProvider {
161
162
  const signature = await this.signRaw(response.signerAddress, response.txId);
162
163
  // submit the tx if required
163
164
  if (submitTx) {
164
- await this.provider.transactions.postTransactionsSubmit({
165
+ await __1.web3.getCurrentNodeProvider().transactions.postTransactionsSubmit({
165
166
  unsignedTx: response.unsignedTx,
166
167
  signature: signature
167
168
  });
@@ -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.22",
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,8 +176,13 @@ 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
 
@@ -188,13 +191,15 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
188
191
  }
189
192
 
190
193
  async submitTransaction(unsignedTx: string, signerAddress?: string): Promise<SubmissionResult> {
191
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx })
194
+ const decoded = await web3
195
+ .getCurrentNodeProvider()
196
+ .transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx })
192
197
  const txId = decoded.unsignedTx.txId
193
198
 
194
199
  const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress()
195
200
  const signature = await this.signRaw(address, txId)
196
201
  const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
197
- return this.provider.transactions.postTransactionsSubmit(params)
202
+ return web3.getCurrentNodeProvider().transactions.postTransactionsSubmit(params)
198
203
  }
199
204
 
200
205
  private shouldSubmitTx(params: SubmitTx): boolean {
@@ -225,7 +230,7 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
225
230
  destinations: toApiDestinations(params.destinations),
226
231
  gasPrice: toApiNumber256Optional(params.gasPrice)
227
232
  }
228
- return this.provider.transactions.postTransactionsBuild(data)
233
+ return web3.getCurrentNodeProvider().transactions.postTransactionsBuild(data)
229
234
  }
230
235
 
231
236
  async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
@@ -246,7 +251,7 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
246
251
  issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
247
252
  gasPrice: toApiNumber256Optional(params.gasPrice)
248
253
  }
249
- return this.provider.contracts.postContractsUnsignedTxDeployContract(data)
254
+ return web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxDeployContract(data)
250
255
  }
251
256
 
252
257
  async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
@@ -259,14 +264,14 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
259
264
  ...(await this.usePublicKey(params)),
260
265
  tokens: toApiTokens(params.tokens)
261
266
  }
262
- return this.provider.contracts.postContractsUnsignedTxExecuteScript(data)
267
+ return web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data)
263
268
  }
264
269
 
265
270
  // in general, wallet should show the decoded information to user for confirmation
266
271
  // please overwrite this function for real wallet
267
272
  async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
268
273
  const data = { unsignedTx: params.unsignedTx }
269
- const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx(data)
274
+ const decoded = await web3.getCurrentNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data)
270
275
  return this.handleSign(
271
276
  {
272
277
  fromGroup: decoded.fromGroup,
@@ -287,7 +292,7 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
287
292
  const signature = await this.signRaw(response.signerAddress, response.txId)
288
293
  // submit the tx if required
289
294
  if (submitTx) {
290
- await this.provider.transactions.postTransactionsSubmit({
295
+ await web3.getCurrentNodeProvider().transactions.postTransactionsSubmit({
291
296
  unsignedTx: response.unsignedTx,
292
297
  signature: signature
293
298
  })
@@ -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