@massalabs/wallet-provider 1.2.1-dev.20230801093549 → 1.2.1-dev.20230803141012
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle.js +5662 -5204
- package/bundle.min.js +3 -0
- package/dist/cmd/account/Account.d.ts +103 -0
- package/dist/cmd/account/Account.d.ts.map +1 -0
- package/dist/cmd/account/Account.js +179 -0
- package/dist/cmd/account/Account.js.map +1 -0
- package/dist/cmd/account/AccountBalance.d.ts +14 -0
- package/dist/cmd/account/AccountBalance.d.ts.map +1 -0
- package/dist/cmd/account/AccountBalance.js +3 -0
- package/dist/cmd/account/AccountBalance.js.map +1 -0
- package/dist/cmd/account/AccountSign.d.ts +16 -0
- package/dist/cmd/account/AccountSign.d.ts.map +1 -0
- package/dist/cmd/account/AccountSign.js +3 -0
- package/dist/cmd/account/AccountSign.js.map +1 -0
- package/dist/cmd/account/IAccount.d.ts +20 -0
- package/dist/cmd/account/IAccount.d.ts.map +1 -0
- package/dist/cmd/account/IAccount.js +3 -0
- package/dist/cmd/account/IAccount.js.map +1 -0
- package/dist/cmd/account/IAccountCallSCRequest.d.ts +25 -0
- package/dist/cmd/account/IAccountCallSCRequest.d.ts.map +1 -0
- package/dist/cmd/account/IAccountCallSCRequest.js +3 -0
- package/dist/cmd/account/IAccountCallSCRequest.js.map +1 -0
- package/dist/cmd/account/IAccountDetails.d.ts +8 -0
- package/dist/cmd/account/IAccountDetails.d.ts.map +1 -0
- package/dist/cmd/account/IAccountDetails.js +3 -0
- package/dist/cmd/account/IAccountDetails.js.map +1 -0
- package/dist/cmd/account/IAccountRolls.d.ts +8 -0
- package/dist/cmd/account/IAccountRolls.d.ts.map +1 -0
- package/dist/cmd/account/IAccountRolls.js +3 -0
- package/dist/cmd/account/IAccountRolls.js.map +1 -0
- package/dist/cmd/account/IAccountSendTransaction.d.ts +9 -0
- package/dist/cmd/account/IAccountSendTransaction.d.ts.map +1 -0
- package/dist/cmd/account/IAccountSendTransaction.js +3 -0
- package/dist/cmd/account/IAccountSendTransaction.js.map +1 -0
- package/dist/cmd/account/index.d.ts +9 -0
- package/dist/cmd/account/index.d.ts.map +1 -0
- package/dist/cmd/account/index.js +6 -0
- package/dist/cmd/account/index.js.map +1 -0
- package/dist/cmd/connector/Connector.d.ts +87 -0
- package/dist/cmd/connector/Connector.d.ts.map +1 -0
- package/dist/cmd/connector/Connector.js +173 -0
- package/dist/cmd/connector/Connector.js.map +1 -0
- package/dist/cmd/connector/ICustomEventMessageRequest.d.ts +10 -0
- package/dist/cmd/connector/ICustomEventMessageRequest.d.ts.map +1 -0
- package/dist/cmd/connector/ICustomEventMessageRequest.js +3 -0
- package/dist/cmd/connector/ICustomEventMessageRequest.js.map +1 -0
- package/dist/cmd/connector/ICustomEventMessageResponse.d.ts +11 -0
- package/dist/cmd/connector/ICustomEventMessageResponse.d.ts.map +1 -0
- package/dist/cmd/connector/ICustomEventMessageResponse.js +3 -0
- package/dist/cmd/connector/ICustomEventMessageResponse.js.map +1 -0
- package/dist/cmd/connector/IRegisterEvent.d.ts +8 -0
- package/dist/cmd/connector/IRegisterEvent.d.ts.map +1 -0
- package/dist/cmd/connector/IRegisterEvent.js +3 -0
- package/dist/cmd/connector/IRegisterEvent.js.map +1 -0
- package/dist/cmd/connector/index.d.ts +5 -0
- package/dist/cmd/connector/index.d.ts.map +1 -0
- package/dist/cmd/connector/index.js +7 -0
- package/dist/cmd/connector/index.js.map +1 -0
- package/dist/cmd/index.d.ts +48 -0
- package/dist/cmd/index.d.ts.map +1 -0
- package/dist/cmd/index.js +89 -0
- package/dist/cmd/index.js.map +1 -0
- package/dist/cmd/massaStation/MassaStationAccount.d.ts +103 -0
- package/dist/cmd/massaStation/MassaStationAccount.d.ts.map +1 -0
- package/dist/cmd/massaStation/MassaStationAccount.js +312 -0
- package/dist/cmd/massaStation/MassaStationAccount.js.map +1 -0
- package/dist/cmd/massaStation/MassaStationDiscovery.d.ts +57 -0
- package/dist/cmd/massaStation/MassaStationDiscovery.d.ts.map +1 -0
- package/dist/cmd/massaStation/MassaStationDiscovery.js +80 -0
- package/dist/cmd/massaStation/MassaStationDiscovery.js.map +1 -0
- package/dist/cmd/massaStation/MassaStationProvider.d.ts +91 -0
- package/dist/cmd/massaStation/MassaStationProvider.d.ts.map +1 -0
- package/dist/cmd/massaStation/MassaStationProvider.js +193 -0
- package/dist/cmd/massaStation/MassaStationProvider.js.map +1 -0
- package/dist/cmd/massaStation/RequestHandler.d.ts +47 -0
- package/dist/cmd/massaStation/RequestHandler.d.ts.map +1 -0
- package/dist/cmd/massaStation/RequestHandler.js +133 -0
- package/dist/cmd/massaStation/RequestHandler.js.map +1 -0
- package/dist/cmd/provider/AccountDeletion.d.ts +24 -0
- package/dist/cmd/provider/AccountDeletion.d.ts.map +1 -0
- package/dist/cmd/provider/AccountDeletion.js +14 -0
- package/dist/cmd/provider/AccountDeletion.js.map +1 -0
- package/dist/cmd/provider/AccountImport.d.ts +26 -0
- package/dist/cmd/provider/AccountImport.d.ts.map +1 -0
- package/dist/cmd/provider/AccountImport.js +14 -0
- package/dist/cmd/provider/AccountImport.js.map +1 -0
- package/dist/cmd/provider/AccoutGeneration.d.ts +8 -0
- package/dist/cmd/provider/AccoutGeneration.d.ts.map +1 -0
- package/dist/cmd/provider/AccoutGeneration.js +3 -0
- package/dist/cmd/provider/AccoutGeneration.js.map +1 -0
- package/dist/cmd/provider/IProvider.d.ts +16 -0
- package/dist/cmd/provider/IProvider.d.ts.map +1 -0
- package/dist/cmd/provider/IProvider.js +3 -0
- package/dist/cmd/provider/IProvider.js.map +1 -0
- package/dist/cmd/provider/Provider.d.ts +68 -0
- package/dist/cmd/provider/Provider.d.ts.map +1 -0
- package/dist/cmd/provider/Provider.js +126 -0
- package/dist/cmd/provider/Provider.js.map +1 -0
- package/dist/cmd/provider/index.d.ts +5 -0
- package/dist/cmd/provider/index.d.ts.map +1 -0
- package/dist/cmd/provider/index.js +10 -0
- package/dist/cmd/provider/index.js.map +1 -0
- package/dist/cmd/utils/argsToBase64.d.ts +16 -0
- package/dist/cmd/utils/argsToBase64.d.ts.map +1 -0
- package/dist/cmd/utils/argsToBase64.js +25 -0
- package/dist/cmd/utils/argsToBase64.js.map +1 -0
- package/dist/cmd/utils/time.d.ts +77 -0
- package/dist/cmd/utils/time.d.ts.map +1 -0
- package/dist/cmd/utils/time.js +120 -0
- package/dist/cmd/utils/time.js.map +1 -0
- package/dist/connector/Connector.d.ts +2 -0
- package/dist/connector/Connector.d.ts.map +1 -1
- package/dist/connector/Connector.js +13 -9
- package/dist/esm/account/Account.d.ts +103 -0
- package/dist/esm/account/Account.d.ts.map +1 -0
- package/dist/esm/account/Account.js +178 -0
- package/dist/esm/account/Account.js.map +1 -0
- package/dist/esm/account/AccountBalance.d.ts +14 -0
- package/dist/esm/account/AccountBalance.d.ts.map +1 -0
- package/dist/esm/account/AccountBalance.js +2 -0
- package/dist/esm/account/AccountBalance.js.map +1 -0
- package/dist/esm/account/AccountSign.d.ts +16 -0
- package/dist/esm/account/AccountSign.d.ts.map +1 -0
- package/dist/esm/account/AccountSign.js +2 -0
- package/dist/esm/account/AccountSign.js.map +1 -0
- package/dist/esm/account/IAccount.d.ts +20 -0
- package/dist/esm/account/IAccount.d.ts.map +1 -0
- package/dist/esm/account/IAccount.js +2 -0
- package/dist/esm/account/IAccount.js.map +1 -0
- package/dist/esm/account/IAccountCallSCRequest.d.ts +25 -0
- package/dist/esm/account/IAccountCallSCRequest.d.ts.map +1 -0
- package/dist/esm/account/IAccountCallSCRequest.js +2 -0
- package/dist/esm/account/IAccountCallSCRequest.js.map +1 -0
- package/dist/esm/account/IAccountDetails.d.ts +8 -0
- package/dist/esm/account/IAccountDetails.d.ts.map +1 -0
- package/dist/esm/account/IAccountDetails.js +2 -0
- package/dist/esm/account/IAccountDetails.js.map +1 -0
- package/dist/esm/account/IAccountRolls.d.ts +8 -0
- package/dist/esm/account/IAccountRolls.d.ts.map +1 -0
- package/dist/esm/account/IAccountRolls.js +2 -0
- package/dist/esm/account/IAccountRolls.js.map +1 -0
- package/dist/esm/account/IAccountSendTransaction.d.ts +9 -0
- package/dist/esm/account/IAccountSendTransaction.d.ts.map +1 -0
- package/dist/esm/account/IAccountSendTransaction.js +2 -0
- package/dist/esm/account/IAccountSendTransaction.js.map +1 -0
- package/dist/esm/account/index.d.ts +9 -0
- package/dist/esm/account/index.d.ts.map +1 -0
- package/dist/esm/account/index.js +2 -0
- package/dist/esm/account/index.js.map +1 -0
- package/dist/esm/connector/Connector.d.ts +87 -0
- package/dist/esm/connector/Connector.d.ts.map +1 -0
- package/dist/esm/connector/Connector.js +172 -0
- package/dist/esm/connector/Connector.js.map +1 -0
- package/dist/esm/connector/ICustomEventMessageRequest.d.ts +10 -0
- package/dist/esm/connector/ICustomEventMessageRequest.d.ts.map +1 -0
- package/dist/esm/connector/ICustomEventMessageRequest.js +2 -0
- package/dist/esm/connector/ICustomEventMessageRequest.js.map +1 -0
- package/dist/esm/connector/ICustomEventMessageResponse.d.ts +11 -0
- package/dist/esm/connector/ICustomEventMessageResponse.d.ts.map +1 -0
- package/dist/esm/connector/ICustomEventMessageResponse.js +2 -0
- package/dist/esm/connector/ICustomEventMessageResponse.js.map +1 -0
- package/dist/esm/connector/IRegisterEvent.d.ts +8 -0
- package/dist/esm/connector/IRegisterEvent.d.ts.map +1 -0
- package/dist/esm/connector/IRegisterEvent.js +2 -0
- package/dist/esm/connector/IRegisterEvent.js.map +1 -0
- package/dist/esm/connector/index.d.ts +5 -0
- package/dist/esm/connector/index.d.ts.map +1 -0
- package/dist/esm/connector/index.js +2 -0
- package/dist/esm/connector/index.js.map +1 -0
- package/dist/esm/index.d.ts +48 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +79 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/massaStation/MassaStationAccount.d.ts +103 -0
- package/dist/esm/massaStation/MassaStationAccount.d.ts.map +1 -0
- package/dist/esm/massaStation/MassaStationAccount.js +311 -0
- package/dist/esm/massaStation/MassaStationAccount.js.map +1 -0
- package/dist/esm/massaStation/MassaStationDiscovery.d.ts +57 -0
- package/dist/esm/massaStation/MassaStationDiscovery.d.ts.map +1 -0
- package/dist/esm/massaStation/MassaStationDiscovery.js +76 -0
- package/dist/esm/massaStation/MassaStationDiscovery.js.map +1 -0
- package/dist/esm/massaStation/MassaStationProvider.d.ts +91 -0
- package/dist/esm/massaStation/MassaStationProvider.d.ts.map +1 -0
- package/dist/esm/massaStation/MassaStationProvider.js +190 -0
- package/dist/esm/massaStation/MassaStationProvider.js.map +1 -0
- package/dist/esm/massaStation/RequestHandler.d.ts +47 -0
- package/dist/esm/massaStation/RequestHandler.d.ts.map +1 -0
- package/dist/esm/massaStation/RequestHandler.js +126 -0
- package/dist/esm/massaStation/RequestHandler.js.map +1 -0
- package/dist/esm/provider/AccountDeletion.d.ts +24 -0
- package/dist/esm/provider/AccountDeletion.d.ts.map +1 -0
- package/dist/esm/provider/AccountDeletion.js +11 -0
- package/dist/esm/provider/AccountDeletion.js.map +1 -0
- package/dist/esm/provider/AccountImport.d.ts +26 -0
- package/dist/esm/provider/AccountImport.d.ts.map +1 -0
- package/dist/esm/provider/AccountImport.js +11 -0
- package/dist/esm/provider/AccountImport.js.map +1 -0
- package/dist/esm/provider/AccoutGeneration.d.ts +8 -0
- package/dist/esm/provider/AccoutGeneration.d.ts.map +1 -0
- package/dist/esm/provider/AccoutGeneration.js +2 -0
- package/dist/esm/provider/AccoutGeneration.js.map +1 -0
- package/dist/esm/provider/IProvider.d.ts +16 -0
- package/dist/esm/provider/IProvider.d.ts.map +1 -0
- package/dist/esm/provider/IProvider.js +2 -0
- package/dist/esm/provider/IProvider.js.map +1 -0
- package/dist/esm/provider/Provider.d.ts +68 -0
- package/dist/esm/provider/Provider.d.ts.map +1 -0
- package/dist/esm/provider/Provider.js +123 -0
- package/dist/esm/provider/Provider.js.map +1 -0
- package/dist/esm/provider/index.d.ts +5 -0
- package/dist/esm/provider/index.d.ts.map +1 -0
- package/dist/esm/provider/index.js +4 -0
- package/dist/esm/provider/index.js.map +1 -0
- package/dist/esm/utils/argsToBase64.d.ts +16 -0
- package/dist/esm/utils/argsToBase64.d.ts.map +1 -0
- package/dist/esm/utils/argsToBase64.js +20 -0
- package/dist/esm/utils/argsToBase64.js.map +1 -0
- package/dist/esm/utils/time.d.ts +77 -0
- package/dist/esm/utils/time.d.ts.map +1 -0
- package/dist/esm/utils/time.js +119 -0
- package/dist/esm/utils/time.js.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/massaStation/MassaStationDiscovery.d.ts +2 -16
- package/dist/massaStation/MassaStationDiscovery.d.ts.map +1 -1
- package/dist/massaStation/MassaStationDiscovery.js +4 -30
- package/package.json +11 -8
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MassaStationAccount = exports.Provider = exports.EAccountImportResponse = exports.EAccountDeletionResponse = exports.Account = exports.registerProvider = exports.providers = exports.AvailableCommands = void 0;
|
|
4
|
+
/** Polyfills */
|
|
5
|
+
const buffer_1 = require("buffer");
|
|
6
|
+
// Check if we are on browser
|
|
7
|
+
if (typeof window !== 'undefined') {
|
|
8
|
+
window.Buffer = buffer_1.Buffer;
|
|
9
|
+
}
|
|
10
|
+
const Connector_1 = require("./connector/Connector");
|
|
11
|
+
const Provider_1 = require("./provider/Provider");
|
|
12
|
+
const MassaStationProvider_1 = require("./massaStation/MassaStationProvider");
|
|
13
|
+
var AvailableCommands;
|
|
14
|
+
(function (AvailableCommands) {
|
|
15
|
+
AvailableCommands["ProviderListAccounts"] = "LIST_ACCOUNTS";
|
|
16
|
+
AvailableCommands["ProviderDeleteAccount"] = "DELETE_ACCOUNT";
|
|
17
|
+
AvailableCommands["ProviderImportAccount"] = "IMPORT_ACCOUNT";
|
|
18
|
+
AvailableCommands["ProviderGetNodesUrls"] = "GET_NODES_URLS";
|
|
19
|
+
AvailableCommands["AccountBalance"] = "ACCOUNT_BALANCE";
|
|
20
|
+
AvailableCommands["AccountSign"] = "ACCOUNT_SIGN";
|
|
21
|
+
AvailableCommands["ProviderGenerateNewAccount"] = "GENERATE_NEW_ACCOUNT";
|
|
22
|
+
AvailableCommands["AccountSellRolls"] = "ACCOUNT_SELL_ROLLS";
|
|
23
|
+
AvailableCommands["AccountBuyRolls"] = "ACCOUNT_BUY_ROLLS";
|
|
24
|
+
AvailableCommands["AccountSendTransaction"] = "ACCOUNT_SEND_TRANSACTION";
|
|
25
|
+
AvailableCommands["AccountCallSC"] = "ACCOUNT_CALL_SC";
|
|
26
|
+
})(AvailableCommands = exports.AvailableCommands || (exports.AvailableCommands = {}));
|
|
27
|
+
/**
|
|
28
|
+
* Get the list of providers that are available to interact with.
|
|
29
|
+
*
|
|
30
|
+
* @param retry - If true, will retry to get the list of providers if none are available.
|
|
31
|
+
* @param pollInterval - The timeout in milliseconds to wait between retries. default is 2000ms.
|
|
32
|
+
* @param timeout - The timeout in milliseconds to wait before giving up. default is 3000ms.
|
|
33
|
+
*
|
|
34
|
+
* @returns An array of providers.
|
|
35
|
+
*/
|
|
36
|
+
async function providers(retry = true, timeout = 3000, pollInterval = 500) {
|
|
37
|
+
if (timeout <= 0) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
await Connector_1.connector.startMassaStationDiscovery();
|
|
41
|
+
return new Promise((resolve) => {
|
|
42
|
+
let provider = [];
|
|
43
|
+
for (const providerName of Object.keys(Connector_1.connector.getWalletProviders())) {
|
|
44
|
+
if (providerName === MassaStationProvider_1.MASSA_STATION_PROVIDER_NAME) {
|
|
45
|
+
const p = new MassaStationProvider_1.MassaStationProvider();
|
|
46
|
+
provider.push(p);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const p = new Provider_1.Provider(providerName);
|
|
50
|
+
provider.push(p);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// If no providers are available, wait and try again
|
|
54
|
+
if (retry && provider.length === 0) {
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
providers(retry, timeout - pollInterval, pollInterval).then(resolve);
|
|
57
|
+
}, pollInterval);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
resolve(provider);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
exports.providers = providers;
|
|
65
|
+
/**
|
|
66
|
+
* Manually register a provider to interact with.
|
|
67
|
+
*
|
|
68
|
+
* @param name - The name of the provider.
|
|
69
|
+
* @param id - The id of the HTML element that is used to communicate with the provider.
|
|
70
|
+
*/
|
|
71
|
+
function registerProvider(name, id = Connector_1.MASSA_WINDOW_OBJECT) {
|
|
72
|
+
const registerEvent = new CustomEvent('register', {
|
|
73
|
+
detail: { providerName: name },
|
|
74
|
+
});
|
|
75
|
+
const element = document.getElementById(id);
|
|
76
|
+
if (element) {
|
|
77
|
+
element.dispatchEvent(registerEvent);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.registerProvider = registerProvider;
|
|
81
|
+
var account_1 = require("./account");
|
|
82
|
+
Object.defineProperty(exports, "Account", { enumerable: true, get: function () { return account_1.Account; } });
|
|
83
|
+
var provider_1 = require("./provider");
|
|
84
|
+
Object.defineProperty(exports, "EAccountDeletionResponse", { enumerable: true, get: function () { return provider_1.EAccountDeletionResponse; } });
|
|
85
|
+
Object.defineProperty(exports, "EAccountImportResponse", { enumerable: true, get: function () { return provider_1.EAccountImportResponse; } });
|
|
86
|
+
Object.defineProperty(exports, "Provider", { enumerable: true, get: function () { return provider_1.Provider; } });
|
|
87
|
+
var MassaStationAccount_1 = require("./massaStation/MassaStationAccount");
|
|
88
|
+
Object.defineProperty(exports, "MassaStationAccount", { enumerable: true, get: function () { return MassaStationAccount_1.MassaStationAccount; } });
|
|
89
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"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ClB;SACF;QAED,oDAAoD;QACpD,IAAI,KAAK,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;YAClC,UAAU,CAAC,GAAG,EAAE;gBACd,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,EAAE,YAAY,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACvE,CAAC,EAAE,YAAY,CAAC,CAAC;SAClB;aAAM;YACL,OAAO,CAAC,QAAQ,CAAC,CAAC;SACnB;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAjCD,8BAiCC;AAED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,IAAY,EAAE,EAAE,GAAG,+BAAmB;IACrE,MAAM,aAAa,GAAG,IAAI,WAAW,CAAC,UAAU,EAAE;QAChD,MAAM,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE;KAC/B,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;IAC5C,IAAI,OAAO,EAAE;QACX,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC;KACtC;AACH,CAAC;AARD,4CAQC;AAID,qCAWmB;AAJjB,kGAAA,OAAO,OAAA;AAMT,uCASoB;AARlB,oHAAA,wBAAwB,OAAA;AACxB,kHAAA,sBAAsB,OAAA;AAMtB,oGAAA,QAAQ,OAAA;AAKV,0EAAyE;AAAhE,0HAAA,mBAAmB,OAAA","sourcesContent":["/** Polyfills */\nimport { Buffer } from 'buffer';\n\ndeclare global {\n interface Window {\n Buffer: typeof Buffer;\n }\n}\n\n// Check if we are on browser\nif (typeof window !== 'undefined') {\n window.Buffer = Buffer;\n}\n\nimport { MASSA_WINDOW_OBJECT, connector } from './connector/Connector';\nimport { IProvider } from './provider/IProvider';\nimport { Provider } from './provider/Provider';\nimport {\n MASSA_STATION_PROVIDER_NAME,\n MassaStationProvider,\n} from './massaStation/MassaStationProvider';\n\nexport enum AvailableCommands {\n ProviderListAccounts = 'LIST_ACCOUNTS',\n ProviderDeleteAccount = 'DELETE_ACCOUNT',\n ProviderImportAccount = 'IMPORT_ACCOUNT',\n ProviderGetNodesUrls = 'GET_NODES_URLS',\n AccountBalance = 'ACCOUNT_BALANCE',\n AccountSign = 'ACCOUNT_SIGN',\n ProviderGenerateNewAccount = 'GENERATE_NEW_ACCOUNT',\n AccountSellRolls = 'ACCOUNT_SELL_ROLLS',\n AccountBuyRolls = 'ACCOUNT_BUY_ROLLS',\n AccountSendTransaction = 'ACCOUNT_SEND_TRANSACTION',\n AccountCallSC = 'ACCOUNT_CALL_SC',\n}\n\nexport interface ITransactionDetails {\n operationId: string;\n}\n\n/**\n * Get the list of providers that are available to interact with.\n *\n * @param retry - If true, will retry to get the list of providers if none are available.\n * @param pollInterval - The timeout in milliseconds to wait between retries. default is 2000ms.\n * @param timeout - The timeout in milliseconds to wait before giving up. default is 3000ms.\n *\n * @returns An array of providers.\n */\nexport async function providers(\n retry = true,\n timeout = 3000,\n pollInterval = 500,\n): Promise<IProvider[]> {\n if (timeout <= 0) {\n return [];\n }\n\n await connector.startMassaStationDiscovery();\n\n return new Promise((resolve) => {\n let provider: IProvider[] = [];\n\n for (const providerName of Object.keys(connector.getWalletProviders())) {\n if (providerName === MASSA_STATION_PROVIDER_NAME) {\n const p = new MassaStationProvider();\n provider.push(p);\n } else {\n const p = new Provider(providerName);\n provider.push(p);\n }\n }\n\n // If no providers are available, wait and try again\n if (retry && provider.length === 0) {\n setTimeout(() => {\n providers(retry, timeout - pollInterval, pollInterval).then(resolve);\n }, pollInterval);\n } else {\n resolve(provider);\n }\n });\n}\n\n/**\n * Manually register a provider to interact with.\n *\n * @param name - The name of the provider.\n * @param id - The id of the HTML element that is used to communicate with the provider.\n */\nexport function registerProvider(name: string, id = MASSA_WINDOW_OBJECT): void {\n const registerEvent = new CustomEvent('register', {\n detail: { providerName: name },\n });\n const element = document.getElementById(id);\n if (element) {\n element.dispatchEvent(registerEvent);\n }\n}\n\nexport { AllowedRequests, AllowedResponses } from './connector';\n\nexport {\n IAccountDetails,\n IAccountBalanceRequest,\n IAccountBalanceResponse,\n IAccountSignRequest,\n IAccountSignResponse,\n IAccount,\n Account,\n IAccountRollsRequest,\n IAccountSendTransactionRequest,\n IAccountCallSCRequest,\n} from './account';\n\nexport {\n EAccountDeletionResponse,\n EAccountImportResponse,\n IAccountDeletionRequest,\n IAccountDeletionResponse,\n IAccountImportRequest,\n IAccountImportResponse,\n IProvider,\n Provider,\n} from './provider';\n\nexport { IMassaStationWallet } from './massaStation/MassaStationProvider';\n\nexport { MassaStationAccount } from './massaStation/MassaStationAccount';\n"]}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { IAccountBalanceResponse, IAccountDetails, IAccountSignResponse, ITransactionDetails } from '..';
|
|
2
|
+
import { IAccount } from '../account/IAccount';
|
|
3
|
+
import { Args, IContractReadOperationResponse } from '@massalabs/web3-utils';
|
|
4
|
+
/**
|
|
5
|
+
* This module contains the MassaStationAccount class. It is responsible for representing an account in
|
|
6
|
+
* the MassaStation wallet.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This class provides methods to interact with MassaStation account's {@link balance} and to {@link sign} messages.
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
export declare class MassaStationAccount implements IAccount {
|
|
13
|
+
private _providerName;
|
|
14
|
+
private _address;
|
|
15
|
+
private _name;
|
|
16
|
+
/**
|
|
17
|
+
* This constructor takes an object of type IAccountDetails and a providerName string as its arguments.
|
|
18
|
+
*
|
|
19
|
+
* @param address - The address of the account.
|
|
20
|
+
* @param name - The name of the account.
|
|
21
|
+
* @param providerName - The name of the provider.
|
|
22
|
+
* @returns An instance of the Account class.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* - The Account constructor takes an object of type IAccountDetails and a providerName string as its arguments.
|
|
26
|
+
* - The IAccountDetails object contains the account's address and name.
|
|
27
|
+
* - The providerName string identifies the provider that is used to interact with the blockchain.
|
|
28
|
+
*/
|
|
29
|
+
constructor({ address, name }: IAccountDetails, providerName: string);
|
|
30
|
+
/**
|
|
31
|
+
* @returns The address of the account.
|
|
32
|
+
*/
|
|
33
|
+
address(): string;
|
|
34
|
+
/**
|
|
35
|
+
* @returns The name of the account.
|
|
36
|
+
*/
|
|
37
|
+
name(): string;
|
|
38
|
+
/**
|
|
39
|
+
* @returns The name of the provider.
|
|
40
|
+
*/
|
|
41
|
+
providerName(): string;
|
|
42
|
+
/**
|
|
43
|
+
* This method aims to retrieve the account's balance.
|
|
44
|
+
*
|
|
45
|
+
* @returns A promise that resolves to an object of type IAccountBalanceResponse. It contains the account's balance.
|
|
46
|
+
*/
|
|
47
|
+
balance(): Promise<IAccountBalanceResponse>;
|
|
48
|
+
/**
|
|
49
|
+
* This method aims to sign a message.
|
|
50
|
+
*
|
|
51
|
+
* @param data - The message to be signed.
|
|
52
|
+
* @returns An IAccountSignResponse object. It contains the signature of the message.
|
|
53
|
+
*/
|
|
54
|
+
sign(data: Uint8Array | string): Promise<IAccountSignResponse>;
|
|
55
|
+
/**
|
|
56
|
+
* This method aims to buy rolls on behalf of the sender.
|
|
57
|
+
*
|
|
58
|
+
* @param amount - The amount of rolls to be bought.
|
|
59
|
+
* @param fee - The fee to be paid for the transaction execution by the node.
|
|
60
|
+
* @returns An ITransactionDetails object. It contains the operationId on the network.
|
|
61
|
+
*/
|
|
62
|
+
buyRolls(amount: bigint, fee: bigint): Promise<ITransactionDetails>;
|
|
63
|
+
/**
|
|
64
|
+
* This method aims to sell rolls on behalf of the sender.
|
|
65
|
+
*
|
|
66
|
+
* @param amount - The amount of rolls to be sold.
|
|
67
|
+
* @param fee - The fee to be paid for the transaction execution by the node.
|
|
68
|
+
* @returns An ITransactionDetails object. It contains the operationId on the network.
|
|
69
|
+
*/
|
|
70
|
+
sellRolls(amount: bigint, fee: bigint): Promise<ITransactionDetails>;
|
|
71
|
+
/**
|
|
72
|
+
* This method aims to transfer MAS on behalf of the sender to a recipient.
|
|
73
|
+
*
|
|
74
|
+
* @param amount - The amount of MAS (in the smallest unit) to be transferred.
|
|
75
|
+
* @param fee - The fee to be paid for the transaction execution (in the smallest unit).
|
|
76
|
+
* @returns An ITransactionDetails object. It contains the operationId on the network.
|
|
77
|
+
*/
|
|
78
|
+
sendTransaction(amount: bigint, recipientAddress: string, fee: bigint): Promise<ITransactionDetails>;
|
|
79
|
+
/**
|
|
80
|
+
* This method aims to interact with a smart contract deployed on the MASSA blockchain.
|
|
81
|
+
*
|
|
82
|
+
* @remarks
|
|
83
|
+
* If dryRun.dryRun is true, the method will dry run the smart contract call and return an
|
|
84
|
+
* IContractReadOperationResponse object which contains all the information about the dry run
|
|
85
|
+
* (state changes, gas used, etc.)
|
|
86
|
+
*
|
|
87
|
+
* @param contractAddress - The address of the smart contract.
|
|
88
|
+
* @param functionName - The name of the function to be called.
|
|
89
|
+
* @param parameter - The parameters as an Args object to be passed to the function.
|
|
90
|
+
* @param amount - The amount of MASSA coins to be sent to the contract (in the smallest unit).
|
|
91
|
+
* @param fee - The fee to be paid for the transaction execution (in the smallest unit).
|
|
92
|
+
* @param maxGas - The maximum amount of gas to be used for the transaction execution.
|
|
93
|
+
* @param nonPersistentExecution - The dryRun object to be passed to the function.
|
|
94
|
+
*
|
|
95
|
+
* @returns if 'nonPersistentExecution' is true, it returns an IContractReadOperationResponse object.
|
|
96
|
+
* Otherwise, it returns an ITransactionDetails object which contains the operationId on the network.
|
|
97
|
+
*
|
|
98
|
+
*/
|
|
99
|
+
callSC(contractAddress: string, functionName: string, parameter: Uint8Array | Args, amount: bigint, fee: bigint, maxGas: bigint, nonPersistentExecution?: boolean): Promise<ITransactionDetails | IContractReadOperationResponse>;
|
|
100
|
+
getNodeUrlFromMassaStation(): Promise<string>;
|
|
101
|
+
nonPersistentCallSC(contractAddress: string, functionName: string, parameter: Uint8Array | Args, amount: bigint, fee: bigint, maxGas: bigint): Promise<IContractReadOperationResponse>;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=MassaStationAccount.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MassaStationAccount.d.ts","sourceRoot":"","sources":["../../../src/massaStation/MassaStationAccount.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAM/C,OAAO,EACL,IAAI,EAEJ,8BAA8B,EAC/B,MAAM,uBAAuB,CAAC;AAoC/B;;;;;;;GAOG;AACH,qBAAa,mBAAoB,YAAW,QAAQ;IAClD,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,KAAK,CAAS;IAEtB;;;;;;;;;;;;OAYG;gBACgB,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM;IAM3E;;OAEG;IACI,OAAO,IAAI,MAAM;IAIxB;;OAEG;IACI,IAAI,IAAI,MAAM;IAIrB;;OAEG;IACI,YAAY,IAAI,MAAM;IAI7B;;;;OAIG;IACU,OAAO,IAAI,OAAO,CAAC,uBAAuB,CAAC;IAqBxD;;;;;OAKG;IACU,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAoB3E;;;;;;OAMG;IACU,QAAQ,CACnB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,mBAAmB,CAAC;IAoB/B;;;;;;OAMG;IACU,SAAS,CACpB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,mBAAmB,CAAC;IAoB/B;;;;;;OAMG;IACG,eAAe,CACnB,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,MAAM,EACxB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,mBAAmB,CAAC;IAsB/B;;;;;;;;;;;;;;;;;;;OAmBG;IACU,MAAM,CACjB,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,UAAU,GAAG,IAAI,EAC5B,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,EACd,sBAAsB,UAAQ,GAC7B,OAAO,CAAC,mBAAmB,GAAG,8BAA8B,CAAC;IAyCnD,0BAA0B,IAAI,OAAO,CAAC,MAAM,CAAC;IAoB7C,mBAAmB,CAC9B,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,UAAU,GAAG,IAAI,EAC5B,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,8BAA8B,CAAC;CAgE3C"}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MassaStationAccount = void 0;
|
|
4
|
+
const RequestHandler_1 = require("./RequestHandler");
|
|
5
|
+
const MassaStationProvider_1 = require("./MassaStationProvider");
|
|
6
|
+
const argsToBase64_1 = require("../utils/argsToBase64");
|
|
7
|
+
/**
|
|
8
|
+
* The maximum allowed gas for a read operation
|
|
9
|
+
*/
|
|
10
|
+
const MAX_READ_BLOCK_GAS = BigInt(4294967295);
|
|
11
|
+
/**
|
|
12
|
+
* This module contains the MassaStationAccount class. It is responsible for representing an account in
|
|
13
|
+
* the MassaStation wallet.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* This class provides methods to interact with MassaStation account's {@link balance} and to {@link sign} messages.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
class MassaStationAccount {
|
|
20
|
+
/**
|
|
21
|
+
* This constructor takes an object of type IAccountDetails and a providerName string as its arguments.
|
|
22
|
+
*
|
|
23
|
+
* @param address - The address of the account.
|
|
24
|
+
* @param name - The name of the account.
|
|
25
|
+
* @param providerName - The name of the provider.
|
|
26
|
+
* @returns An instance of the Account class.
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* - The Account constructor takes an object of type IAccountDetails and a providerName string as its arguments.
|
|
30
|
+
* - The IAccountDetails object contains the account's address and name.
|
|
31
|
+
* - The providerName string identifies the provider that is used to interact with the blockchain.
|
|
32
|
+
*/
|
|
33
|
+
constructor({ address, name }, providerName) {
|
|
34
|
+
this._address = address;
|
|
35
|
+
this._name = name;
|
|
36
|
+
this._providerName = providerName;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @returns The address of the account.
|
|
40
|
+
*/
|
|
41
|
+
address() {
|
|
42
|
+
return this._address;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* @returns The name of the account.
|
|
46
|
+
*/
|
|
47
|
+
name() {
|
|
48
|
+
return this._name;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @returns The name of the provider.
|
|
52
|
+
*/
|
|
53
|
+
providerName() {
|
|
54
|
+
return this._providerName;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* This method aims to retrieve the account's balance.
|
|
58
|
+
*
|
|
59
|
+
* @returns A promise that resolves to an object of type IAccountBalanceResponse. It contains the account's balance.
|
|
60
|
+
*/
|
|
61
|
+
async balance() {
|
|
62
|
+
let signOpResponse = null;
|
|
63
|
+
try {
|
|
64
|
+
signOpResponse = await (0, RequestHandler_1.getRequest)(`${MassaStationProvider_1.MASSA_STATION_URL}massa/addresses?attributes=balance&addresses=${this._address}`);
|
|
65
|
+
}
|
|
66
|
+
catch (ex) {
|
|
67
|
+
console.error(`MassaStation account balance error`);
|
|
68
|
+
throw ex;
|
|
69
|
+
}
|
|
70
|
+
if (signOpResponse.isError || signOpResponse.error) {
|
|
71
|
+
throw signOpResponse.error;
|
|
72
|
+
}
|
|
73
|
+
const balance = signOpResponse.result.addressesAttributes[this._address].balance;
|
|
74
|
+
return {
|
|
75
|
+
finalBalance: balance.final,
|
|
76
|
+
candidateBalance: balance.pending,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* This method aims to sign a message.
|
|
81
|
+
*
|
|
82
|
+
* @param data - The message to be signed.
|
|
83
|
+
* @returns An IAccountSignResponse object. It contains the signature of the message.
|
|
84
|
+
*/
|
|
85
|
+
async sign(data) {
|
|
86
|
+
let signOpResponse = null;
|
|
87
|
+
try {
|
|
88
|
+
signOpResponse = await (0, RequestHandler_1.postRequest)(`${MassaStationProvider_1.MASSA_STATION_ACCOUNTS_URL}/${this._name}/sign`, {
|
|
89
|
+
operation: data,
|
|
90
|
+
batch: false,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch (ex) {
|
|
94
|
+
console.error(`MassaStation account signing error`);
|
|
95
|
+
throw ex;
|
|
96
|
+
}
|
|
97
|
+
if (signOpResponse.isError || signOpResponse.error) {
|
|
98
|
+
throw signOpResponse.error;
|
|
99
|
+
}
|
|
100
|
+
return signOpResponse.result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* This method aims to buy rolls on behalf of the sender.
|
|
104
|
+
*
|
|
105
|
+
* @param amount - The amount of rolls to be bought.
|
|
106
|
+
* @param fee - The fee to be paid for the transaction execution by the node.
|
|
107
|
+
* @returns An ITransactionDetails object. It contains the operationId on the network.
|
|
108
|
+
*/
|
|
109
|
+
async buyRolls(amount, fee) {
|
|
110
|
+
let buyRollsOpResponse = null;
|
|
111
|
+
const url = `${MassaStationProvider_1.MASSA_STATION_ACCOUNTS_URL}/${this._name}/rolls`;
|
|
112
|
+
const body = {
|
|
113
|
+
fee: fee.toString(),
|
|
114
|
+
amount: amount.toString(),
|
|
115
|
+
side: 'buy',
|
|
116
|
+
};
|
|
117
|
+
try {
|
|
118
|
+
buyRollsOpResponse = await (0, RequestHandler_1.postRequest)(url, body);
|
|
119
|
+
}
|
|
120
|
+
catch (ex) {
|
|
121
|
+
console.error(`MassaStation account: error while buying rolls: ${ex}`);
|
|
122
|
+
throw ex;
|
|
123
|
+
}
|
|
124
|
+
if (buyRollsOpResponse.isError || buyRollsOpResponse.error) {
|
|
125
|
+
throw buyRollsOpResponse.error;
|
|
126
|
+
}
|
|
127
|
+
return buyRollsOpResponse.result;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* This method aims to sell rolls on behalf of the sender.
|
|
131
|
+
*
|
|
132
|
+
* @param amount - The amount of rolls to be sold.
|
|
133
|
+
* @param fee - The fee to be paid for the transaction execution by the node.
|
|
134
|
+
* @returns An ITransactionDetails object. It contains the operationId on the network.
|
|
135
|
+
*/
|
|
136
|
+
async sellRolls(amount, fee) {
|
|
137
|
+
let sellRollsOpResponse = null;
|
|
138
|
+
const url = `${MassaStationProvider_1.MASSA_STATION_ACCOUNTS_URL}/${this._name}/rolls`;
|
|
139
|
+
const body = {
|
|
140
|
+
fee: fee.toString(),
|
|
141
|
+
amount: amount.toString(),
|
|
142
|
+
side: 'sell',
|
|
143
|
+
};
|
|
144
|
+
try {
|
|
145
|
+
sellRollsOpResponse = await (0, RequestHandler_1.postRequest)(url, body);
|
|
146
|
+
}
|
|
147
|
+
catch (ex) {
|
|
148
|
+
console.error(`MassaStation account: error while selling rolls: ${ex}`);
|
|
149
|
+
throw ex;
|
|
150
|
+
}
|
|
151
|
+
if (sellRollsOpResponse.isError || sellRollsOpResponse.error) {
|
|
152
|
+
throw sellRollsOpResponse.error;
|
|
153
|
+
}
|
|
154
|
+
return sellRollsOpResponse.result;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* This method aims to transfer MAS on behalf of the sender to a recipient.
|
|
158
|
+
*
|
|
159
|
+
* @param amount - The amount of MAS (in the smallest unit) to be transferred.
|
|
160
|
+
* @param fee - The fee to be paid for the transaction execution (in the smallest unit).
|
|
161
|
+
* @returns An ITransactionDetails object. It contains the operationId on the network.
|
|
162
|
+
*/
|
|
163
|
+
async sendTransaction(amount, recipientAddress, fee) {
|
|
164
|
+
let sendTxOpResponse = null;
|
|
165
|
+
const url = `${MassaStationProvider_1.MASSA_STATION_ACCOUNTS_URL}/${this._name}/transfer`;
|
|
166
|
+
const body = {
|
|
167
|
+
fee: fee.toString(),
|
|
168
|
+
amount: amount.toString(),
|
|
169
|
+
recipientAddress: recipientAddress,
|
|
170
|
+
};
|
|
171
|
+
try {
|
|
172
|
+
sendTxOpResponse = await (0, RequestHandler_1.postRequest)(url, body);
|
|
173
|
+
}
|
|
174
|
+
catch (ex) {
|
|
175
|
+
console.error(`MassaStation account: error while sending transaction: ${ex}`);
|
|
176
|
+
throw ex;
|
|
177
|
+
}
|
|
178
|
+
if (sendTxOpResponse.isError || sendTxOpResponse.error) {
|
|
179
|
+
throw sendTxOpResponse.error;
|
|
180
|
+
}
|
|
181
|
+
return sendTxOpResponse.result;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* This method aims to interact with a smart contract deployed on the MASSA blockchain.
|
|
185
|
+
*
|
|
186
|
+
* @remarks
|
|
187
|
+
* If dryRun.dryRun is true, the method will dry run the smart contract call and return an
|
|
188
|
+
* IContractReadOperationResponse object which contains all the information about the dry run
|
|
189
|
+
* (state changes, gas used, etc.)
|
|
190
|
+
*
|
|
191
|
+
* @param contractAddress - The address of the smart contract.
|
|
192
|
+
* @param functionName - The name of the function to be called.
|
|
193
|
+
* @param parameter - The parameters as an Args object to be passed to the function.
|
|
194
|
+
* @param amount - The amount of MASSA coins to be sent to the contract (in the smallest unit).
|
|
195
|
+
* @param fee - The fee to be paid for the transaction execution (in the smallest unit).
|
|
196
|
+
* @param maxGas - The maximum amount of gas to be used for the transaction execution.
|
|
197
|
+
* @param nonPersistentExecution - The dryRun object to be passed to the function.
|
|
198
|
+
*
|
|
199
|
+
* @returns if 'nonPersistentExecution' is true, it returns an IContractReadOperationResponse object.
|
|
200
|
+
* Otherwise, it returns an ITransactionDetails object which contains the operationId on the network.
|
|
201
|
+
*
|
|
202
|
+
*/
|
|
203
|
+
async callSC(contractAddress, functionName, parameter, amount, fee, maxGas, nonPersistentExecution = false) {
|
|
204
|
+
if (nonPersistentExecution) {
|
|
205
|
+
return this.nonPersistentCallSC(contractAddress, functionName, parameter, amount, fee, maxGas);
|
|
206
|
+
}
|
|
207
|
+
// convert parameter to base64
|
|
208
|
+
let args = '';
|
|
209
|
+
if (parameter instanceof Uint8Array) {
|
|
210
|
+
args = (0, argsToBase64_1.uint8ArrayToBase64)(parameter);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
args = (0, argsToBase64_1.argsToBase64)(parameter);
|
|
214
|
+
}
|
|
215
|
+
let CallSCOpResponse = null;
|
|
216
|
+
const url = `${MassaStationProvider_1.MASSA_STATION_URL}cmd/executeFunction`;
|
|
217
|
+
const body = {
|
|
218
|
+
nickname: this._name,
|
|
219
|
+
name: functionName,
|
|
220
|
+
at: contractAddress,
|
|
221
|
+
args: args,
|
|
222
|
+
coins: Number(amount),
|
|
223
|
+
};
|
|
224
|
+
try {
|
|
225
|
+
CallSCOpResponse = await (0, RequestHandler_1.postRequest)(url, body);
|
|
226
|
+
}
|
|
227
|
+
catch (ex) {
|
|
228
|
+
console.log(`MassaStation account: error while interacting with smart contract: ${ex}`);
|
|
229
|
+
throw ex;
|
|
230
|
+
}
|
|
231
|
+
if (CallSCOpResponse.isError || CallSCOpResponse.error) {
|
|
232
|
+
throw CallSCOpResponse.error;
|
|
233
|
+
}
|
|
234
|
+
return CallSCOpResponse.result;
|
|
235
|
+
}
|
|
236
|
+
async getNodeUrlFromMassaStation() {
|
|
237
|
+
// get the node url from MassaStation
|
|
238
|
+
let nodesResponse = null;
|
|
239
|
+
let node = '';
|
|
240
|
+
try {
|
|
241
|
+
nodesResponse = await (0, RequestHandler_1.getRequest)(`${MassaStationProvider_1.MASSA_STATION_URL}massa/node`);
|
|
242
|
+
if (nodesResponse.isError || nodesResponse.error) {
|
|
243
|
+
throw nodesResponse.error.message;
|
|
244
|
+
}
|
|
245
|
+
// transform nodesResponse.result to a json and then get the "url" property
|
|
246
|
+
const nodes = nodesResponse.result;
|
|
247
|
+
node = nodes.url;
|
|
248
|
+
}
|
|
249
|
+
catch (ex) {
|
|
250
|
+
throw new Error(`MassaStation nodes retrieval error: ${ex}`);
|
|
251
|
+
}
|
|
252
|
+
return node;
|
|
253
|
+
}
|
|
254
|
+
async nonPersistentCallSC(contractAddress, functionName, parameter, amount, fee, maxGas) {
|
|
255
|
+
const node = await this.getNodeUrlFromMassaStation();
|
|
256
|
+
// Gas amount check
|
|
257
|
+
if (maxGas > MAX_READ_BLOCK_GAS) {
|
|
258
|
+
throw new Error(`
|
|
259
|
+
The gas submitted ${maxGas.toString()} exceeds the max. allowed block gas of
|
|
260
|
+
${MAX_READ_BLOCK_GAS.toString()}
|
|
261
|
+
`);
|
|
262
|
+
}
|
|
263
|
+
// convert parameter to an array of numbers
|
|
264
|
+
let argumentArray = [];
|
|
265
|
+
if (parameter instanceof Uint8Array) {
|
|
266
|
+
argumentArray = Array.from(parameter);
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
argumentArray = Array.from(parameter.serialize());
|
|
270
|
+
}
|
|
271
|
+
// setup the request body
|
|
272
|
+
const data = {
|
|
273
|
+
max_gas: Number(maxGas),
|
|
274
|
+
target_address: contractAddress,
|
|
275
|
+
target_function: functionName,
|
|
276
|
+
parameter: argumentArray,
|
|
277
|
+
caller_address: this._address,
|
|
278
|
+
};
|
|
279
|
+
const body = [
|
|
280
|
+
{
|
|
281
|
+
jsonrpc: '2.0',
|
|
282
|
+
method: 'execute_read_only_call',
|
|
283
|
+
params: [[data]],
|
|
284
|
+
id: 0,
|
|
285
|
+
},
|
|
286
|
+
];
|
|
287
|
+
// returns operation ids
|
|
288
|
+
let jsonRpcCallResult = [];
|
|
289
|
+
try {
|
|
290
|
+
let resp = await (0, RequestHandler_1.postRequest)(node, body);
|
|
291
|
+
if (resp.isError || resp.error) {
|
|
292
|
+
throw resp.error.message;
|
|
293
|
+
}
|
|
294
|
+
jsonRpcCallResult = resp.result;
|
|
295
|
+
}
|
|
296
|
+
catch (ex) {
|
|
297
|
+
throw new Error(`MassaStation account: error while interacting with smart contract: ${ex}`);
|
|
298
|
+
}
|
|
299
|
+
if (jsonRpcCallResult.length <= 0) {
|
|
300
|
+
throw new Error(`Read operation bad response. No results array in json rpc response. Inspect smart contract`);
|
|
301
|
+
}
|
|
302
|
+
if (jsonRpcCallResult[0].result.Error) {
|
|
303
|
+
throw new Error(jsonRpcCallResult[0].result.Error);
|
|
304
|
+
}
|
|
305
|
+
return {
|
|
306
|
+
returnValue: new Uint8Array(jsonRpcCallResult[0].result[0].result.Ok),
|
|
307
|
+
info: jsonRpcCallResult[0],
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
exports.MassaStationAccount = MassaStationAccount;
|
|
312
|
+
//# sourceMappingURL=MassaStationAccount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MassaStationAccount.js","sourceRoot":"","sources":["../../../src/massaStation/MassaStationAccount.ts"],"names":[],"mappings":";;;AAOA,qDAAgF;AAChF,iEAGgC;AAOhC,wDAAyE;AAEzE;;GAEG;AACH,MAAM,kBAAkB,GAAG,MAAM,CAAC,UAAa,CAAC,CAAC;AA6BjD;;;;;;;GAOG;AACH,MAAa,mBAAmB;IAK9B;;;;;;;;;;;;OAYG;IACH,YAAmB,EAAE,OAAO,EAAE,IAAI,EAAmB,EAAE,YAAoB;QACzE,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;IACpC,CAAC;IAED;;OAEG;IACI,OAAO;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;OAEG;IACI,IAAI;QACT,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED;;OAEG;IACI,YAAY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K,CAAC,OAAO,CAAC;aAC1B;YACD,iBAAiB,GAAG,IAAI,CAAC,MAAM,CAAC;SACjC;QAAC,OAAO,EAAE,EAAE;YACX,MAAM,IAAI,KAAK,CACb,sEAAsE,EAAE,EAAE,CAC3E,CAAC;SACH;QACD,IAAI,iBAAiB,CAAC,MAAM,IAAI,CAAC,EAAE;YACjC,MAAM,IAAI,KAAK,CACb,4FAA4F,CAC7F,CAAC;SACH;QACD,IAAI,iBAAiB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;SACpD;QACD,OAAO;YACL,WAAW,EAAE,IAAI,UAAU,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;YACrE,IAAI,EAAE,iBAAiB,CAAC,CAAC,CAAC;SAC3B,CAAC;IACJ,CAAC;CACF;AA9VD,kDA8VC","sourcesContent":["import {\n IAccountBalanceResponse,\n IAccountDetails,\n IAccountSignResponse,\n ITransactionDetails,\n} from '..';\nimport { IAccount } from '../account/IAccount';\nimport { JsonRpcResponseData, getRequest, postRequest } from './RequestHandler';\nimport {\n MASSA_STATION_URL,\n MASSA_STATION_ACCOUNTS_URL,\n} from './MassaStationProvider';\nimport {\n Args,\n IContractReadOperationData,\n IContractReadOperationResponse,\n} from '@massalabs/web3-utils';\n\nimport { argsToBase64, uint8ArrayToBase64 } from '../utils/argsToBase64';\n\n/**\n * The maximum allowed gas for a read operation\n */\nconst MAX_READ_BLOCK_GAS = BigInt(4_294_967_295);\n\n/**\n * This interface represents the payload returned by making a call to MassaStation's sign\n * operation `/signOperation` url.\n */\ninterface ISignOperation {\n operation: string;\n batch?: boolean;\n correlationId?: string;\n}\n\n/**\n * This interface represents the the individual wallet's final and pending balances returned by MassaStation\n */\ninterface IBalance {\n final: string;\n pending: string;\n}\n\n/**\n * This interface represents the payload returned by making a call to MassaStation's get balances url.\n */\ninterface IAddressesBalances {\n addressesAttributes: {\n [key: string]: { balance: IBalance };\n };\n}\n\n/**\n * This module contains the MassaStationAccount class. It is responsible for representing an account in\n * the MassaStation wallet.\n *\n * @remarks\n * This class provides methods to interact with MassaStation account's {@link balance} and to {@link sign} messages.\n *\n */\nexport class MassaStationAccount implements IAccount {\n private _providerName: string;\n private _address: string;\n private _name: string;\n\n /**\n * This constructor takes an object of type IAccountDetails and a providerName string as its arguments.\n *\n * @param address - The address of the account.\n * @param name - The name of the account.\n * @param providerName - The name of the provider.\n * @returns An instance of the Account class.\n *\n * @remarks\n * - The Account constructor takes an object of type IAccountDetails and a providerName string as its arguments.\n * - The IAccountDetails object contains the account's address and name.\n * - The providerName string identifies the provider that is used to interact with the blockchain.\n */\n public constructor({ address, name }: IAccountDetails, providerName: string) {\n this._address = address;\n this._name = name;\n this._providerName = providerName;\n }\n\n /**\n * @returns The address of the account.\n */\n public address(): string {\n return this._address;\n }\n\n /**\n * @returns The name of the account.\n */\n public name(): string {\n return this._name;\n }\n\n /**\n * @returns The name of the provider.\n */\n public providerName(): string {\n return this._providerName;\n }\n\n /**\n * This method aims to retrieve the account's balance.\n *\n * @returns A promise that resolves to an object of type IAccountBalanceResponse. It contains the account's balance.\n */\n public async balance(): Promise<IAccountBalanceResponse> {\n let signOpResponse: JsonRpcResponseData<IAddressesBalances> = null;\n try {\n signOpResponse = await getRequest<IAddressesBalances>(\n `${MASSA_STATION_URL}massa/addresses?attributes=balance&addresses=${this._address}`,\n );\n } catch (ex) {\n console.error(`MassaStation account balance error`);\n throw ex;\n }\n if (signOpResponse.isError || signOpResponse.error) {\n throw signOpResponse.error;\n }\n const balance: IBalance =\n signOpResponse.result.addressesAttributes[this._address].balance;\n return {\n finalBalance: balance.final,\n candidateBalance: balance.pending,\n };\n }\n\n /**\n * This method aims to sign a message.\n *\n * @param data - The message to be signed.\n * @returns An IAccountSignResponse object. It contains the signature of the message.\n */\n public async sign(data: Uint8Array | string): Promise<IAccountSignResponse> {\n let signOpResponse: JsonRpcResponseData<IAccountSignResponse> = null;\n try {\n signOpResponse = await postRequest<IAccountSignResponse>(\n `${MASSA_STATION_ACCOUNTS_URL}/${this._name}/sign`,\n {\n operation: data,\n batch: false,\n } as ISignOperation,\n );\n } catch (ex) {\n console.error(`MassaStation account signing error`);\n throw ex;\n }\n if (signOpResponse.isError || signOpResponse.error) {\n throw signOpResponse.error;\n }\n return signOpResponse.result;\n }\n\n /**\n * This method aims to buy rolls on behalf of the sender.\n *\n * @param amount - The amount of rolls to be bought.\n * @param fee - The fee to be paid for the transaction execution by the node.\n * @returns An ITransactionDetails object. It contains the operationId on the network.\n */\n public async buyRolls(\n amount: bigint,\n fee: bigint,\n ): Promise<ITransactionDetails> {\n let buyRollsOpResponse: JsonRpcResponseData<ITransactionDetails> = null;\n const url = `${MASSA_STATION_ACCOUNTS_URL}/${this._name}/rolls`;\n const body = {\n fee: fee.toString(),\n amount: amount.toString(),\n side: 'buy',\n };\n try {\n buyRollsOpResponse = await postRequest<ITransactionDetails>(url, body);\n } catch (ex) {\n console.error(`MassaStation account: error while buying rolls: ${ex}`);\n throw ex;\n }\n if (buyRollsOpResponse.isError || buyRollsOpResponse.error) {\n throw buyRollsOpResponse.error;\n }\n return buyRollsOpResponse.result;\n }\n\n /**\n * This method aims to sell rolls on behalf of the sender.\n *\n * @param amount - The amount of rolls to be sold.\n * @param fee - The fee to be paid for the transaction execution by the node.\n * @returns An ITransactionDetails object. It contains the operationId on the network.\n */\n public async sellRolls(\n amount: bigint,\n fee: bigint,\n ): Promise<ITransactionDetails> {\n let sellRollsOpResponse: JsonRpcResponseData<ITransactionDetails> = null;\n const url = `${MASSA_STATION_ACCOUNTS_URL}/${this._name}/rolls`;\n const body = {\n fee: fee.toString(),\n amount: amount.toString(),\n side: 'sell',\n };\n try {\n sellRollsOpResponse = await postRequest<ITransactionDetails>(url, body);\n } catch (ex) {\n console.error(`MassaStation account: error while selling rolls: ${ex}`);\n throw ex;\n }\n if (sellRollsOpResponse.isError || sellRollsOpResponse.error) {\n throw sellRollsOpResponse.error;\n }\n return sellRollsOpResponse.result;\n }\n\n /**\n * This method aims to transfer MAS on behalf of the sender to a recipient.\n *\n * @param amount - The amount of MAS (in the smallest unit) to be transferred.\n * @param fee - The fee to be paid for the transaction execution (in the smallest unit).\n * @returns An ITransactionDetails object. It contains the operationId on the network.\n */\n async sendTransaction(\n amount: bigint,\n recipientAddress: string,\n fee: bigint,\n ): Promise<ITransactionDetails> {\n let sendTxOpResponse: JsonRpcResponseData<ITransactionDetails> = null;\n const url = `${MASSA_STATION_ACCOUNTS_URL}/${this._name}/transfer`;\n const body = {\n fee: fee.toString(),\n amount: amount.toString(),\n recipientAddress: recipientAddress,\n };\n try {\n sendTxOpResponse = await postRequest<ITransactionDetails>(url, body);\n } catch (ex) {\n console.error(\n `MassaStation account: error while sending transaction: ${ex}`,\n );\n throw ex;\n }\n if (sendTxOpResponse.isError || sendTxOpResponse.error) {\n throw sendTxOpResponse.error;\n }\n return sendTxOpResponse.result;\n }\n\n /**\n * This method aims to interact with a smart contract deployed on the MASSA blockchain.\n *\n * @remarks\n * If dryRun.dryRun is true, the method will dry run the smart contract call and return an\n * IContractReadOperationResponse object which contains all the information about the dry run\n * (state changes, gas used, etc.)\n *\n * @param contractAddress - The address of the smart contract.\n * @param functionName - The name of the function to be called.\n * @param parameter - The parameters as an Args object to be passed to the function.\n * @param amount - The amount of MASSA coins to be sent to the contract (in the smallest unit).\n * @param fee - The fee to be paid for the transaction execution (in the smallest unit).\n * @param maxGas - The maximum amount of gas to be used for the transaction execution.\n * @param nonPersistentExecution - The dryRun object to be passed to the function.\n *\n * @returns if 'nonPersistentExecution' is true, it returns an IContractReadOperationResponse object.\n * Otherwise, it returns an ITransactionDetails object which contains the operationId on the network.\n *\n */\n public async callSC(\n contractAddress: string,\n functionName: string,\n parameter: Uint8Array | Args,\n amount: bigint,\n fee: bigint,\n maxGas: bigint,\n nonPersistentExecution = false,\n ): Promise<ITransactionDetails | IContractReadOperationResponse> {\n if (nonPersistentExecution) {\n return this.nonPersistentCallSC(\n contractAddress,\n functionName,\n parameter,\n amount,\n fee,\n maxGas,\n );\n }\n // convert parameter to base64\n let args = '';\n if (parameter instanceof Uint8Array) {\n args = uint8ArrayToBase64(parameter);\n } else {\n args = argsToBase64(parameter);\n }\n let CallSCOpResponse: JsonRpcResponseData<ITransactionDetails> = null;\n const url = `${MASSA_STATION_URL}cmd/executeFunction`;\n const body = {\n nickname: this._name,\n name: functionName,\n at: contractAddress,\n args: args,\n coins: Number(amount),\n };\n try {\n CallSCOpResponse = await postRequest<ITransactionDetails>(url, body);\n } catch (ex) {\n console.log(\n `MassaStation account: error while interacting with smart contract: ${ex}`,\n );\n throw ex;\n }\n if (CallSCOpResponse.isError || CallSCOpResponse.error) {\n throw CallSCOpResponse.error;\n }\n return CallSCOpResponse.result;\n }\n\n public async getNodeUrlFromMassaStation(): Promise<string> {\n // get the node url from MassaStation\n let nodesResponse: JsonRpcResponseData<unknown> = null;\n let node = '';\n try {\n nodesResponse = await getRequest<unknown>(\n `${MASSA_STATION_URL}massa/node`,\n );\n if (nodesResponse.isError || nodesResponse.error) {\n throw nodesResponse.error.message;\n }\n // transform nodesResponse.result to a json and then get the \"url\" property\n const nodes = nodesResponse.result as { url: string };\n node = nodes.url;\n } catch (ex) {\n throw new Error(`MassaStation nodes retrieval error: ${ex}`);\n }\n return node;\n }\n\n public async nonPersistentCallSC(\n contractAddress: string,\n functionName: string,\n parameter: Uint8Array | Args,\n amount: bigint,\n fee: bigint,\n maxGas: bigint,\n ): Promise<IContractReadOperationResponse> {\n const node = await this.getNodeUrlFromMassaStation();\n // Gas amount check\n if (maxGas > MAX_READ_BLOCK_GAS) {\n throw new Error(\n `\n The gas submitted ${maxGas.toString()} exceeds the max. allowed block gas of \n ${MAX_READ_BLOCK_GAS.toString()}\n `,\n );\n }\n\n // convert parameter to an array of numbers\n let argumentArray = [];\n if (parameter instanceof Uint8Array) {\n argumentArray = Array.from(parameter);\n } else {\n argumentArray = Array.from(parameter.serialize());\n }\n // setup the request body\n const data = {\n max_gas: Number(maxGas),\n target_address: contractAddress,\n target_function: functionName,\n parameter: argumentArray,\n caller_address: this._address,\n };\n const body = [\n {\n jsonrpc: '2.0',\n method: 'execute_read_only_call',\n params: [[data]],\n id: 0,\n },\n ];\n // returns operation ids\n let jsonRpcCallResult: Array<IContractReadOperationData> = [];\n try {\n let resp = await postRequest<Array<IContractReadOperationData>>(\n node,\n body,\n );\n if (resp.isError || resp.error) {\n throw resp.error.message;\n }\n jsonRpcCallResult = resp.result;\n } catch (ex) {\n throw new Error(\n `MassaStation account: error while interacting with smart contract: ${ex}`,\n );\n }\n if (jsonRpcCallResult.length <= 0) {\n throw new Error(\n `Read operation bad response. No results array in json rpc response. Inspect smart contract`,\n );\n }\n if (jsonRpcCallResult[0].result.Error) {\n throw new Error(jsonRpcCallResult[0].result.Error);\n }\n return {\n returnValue: new Uint8Array(jsonRpcCallResult[0].result[0].result.Ok),\n info: jsonRpcCallResult[0],\n };\n }\n}\n"]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file defines a TypeScript class named MassaStationDiscovery.
|
|
3
|
+
* The class is being used to recursively search for a connection to MassaStation's server
|
|
4
|
+
* and if so report this via emitting messages.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* - If you are only looking to use our library, the connector MassaStationDiscovery will not be useful to you.
|
|
8
|
+
* - If you want to work on this repo, you will probably be interested in this object
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
/// <reference types="node" />
|
|
12
|
+
import { EventEmitter } from 'events';
|
|
13
|
+
/**
|
|
14
|
+
* Url used for the MassaStation discovery and pinging the MassaStation server's index.html
|
|
15
|
+
*/
|
|
16
|
+
export declare const MASSA_STATION_DISCOVERY_URL = "https://station.massa/web/index";
|
|
17
|
+
/**
|
|
18
|
+
* A message emitted on successful MassaStation discovery
|
|
19
|
+
*/
|
|
20
|
+
export declare const ON_MASSA_STATION_DISCOVERED = "ON_MASSA_STATION_DISCOVERED";
|
|
21
|
+
/**
|
|
22
|
+
* A message emitted on MassaStation disconnect
|
|
23
|
+
*/
|
|
24
|
+
export declare const ON_MASSA_STATION_DISCONNECTED = "ON_MASSA_STATION_DISCONNECTED";
|
|
25
|
+
/**
|
|
26
|
+
* This file defines a TypeScript class named MassaStation.
|
|
27
|
+
* The class is being used to recursively ping MassaStation's server
|
|
28
|
+
* and if a response is received emit a message so MassaStation can be enlisted as
|
|
29
|
+
* a wallet provider in the `Connector` class.
|
|
30
|
+
*/
|
|
31
|
+
export declare class MassaStationDiscovery extends EventEmitter {
|
|
32
|
+
private isDiscovered;
|
|
33
|
+
/**
|
|
34
|
+
* MassaStation constructor
|
|
35
|
+
*
|
|
36
|
+
* @param pollIntervalMillis - Polling interval defined in milliseconds
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* - It creates a timeout using the given `pollIntervalMillis` argument on every trigger of which
|
|
40
|
+
* the MassaStation pinging is triggered and if a successful response is fetched,
|
|
41
|
+
* a message `ON_MASSA_STATION_DISCOVERED` is emitted that MassaStation has been discovered
|
|
42
|
+
* as a wallet provider upon which the `Connector` class will enlist MassaStation as a wallet provider
|
|
43
|
+
* - If once found, but then disconnected the following message `ON_MASSA_STATION_DISCONNECTED` is being emitted
|
|
44
|
+
* so that the `Connector` class delists MassaStation as a wallet provider
|
|
45
|
+
*
|
|
46
|
+
* @returns An instance of the MassaStation class.
|
|
47
|
+
*
|
|
48
|
+
*/
|
|
49
|
+
constructor();
|
|
50
|
+
/**
|
|
51
|
+
* A method to start listening for a connection to MassaStation's server.
|
|
52
|
+
*
|
|
53
|
+
* @returns void
|
|
54
|
+
*/
|
|
55
|
+
startListening(): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=MassaStationDiscovery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MassaStationDiscovery.d.ts","sourceRoot":"","sources":["../../../src/massaStation/MassaStationDiscovery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC;;GAEG;AACH,eAAO,MAAM,2BAA2B,oCAAoC,CAAC;AAE7E;;GAEG;AACH,eAAO,MAAM,2BAA2B,gCAAgC,CAAC;AAEzE;;GAEG;AACH,eAAO,MAAM,6BAA6B,kCAAkC,CAAC;AAE7E;;;;;GAKG;AACH,qBAAa,qBAAsB,SAAQ,YAAY;IACrD,OAAO,CAAC,YAAY,CAAS;IAE7B;;;;;;;;;;;;;;;OAeG;;IAOH;;;;OAIG;IACU,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;CAkB7C"}
|