@cartridge/controller 0.1.47 → 0.1.48
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/lib/account.js +100 -189
- package/lib/account.js.map +1 -1
- package/lib/index.js +79 -145
- package/lib/index.js.map +1 -1
- package/lib/messenger.js +29 -47
- package/lib/messenger.js.map +1 -1
- package/lib/signer.js +49 -93
- package/lib/signer.js.map +1 -1
- package/lib/utils.js +2 -13
- package/lib/utils.js.map +1 -1
- package/lib/webauthn.js +74 -134
- package/lib/webauthn.js.map +1 -1
- package/package.json +1 -1
package/lib/account.js
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -23,56 +8,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
23
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
9
|
});
|
|
25
10
|
};
|
|
26
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
-
function step(op) {
|
|
31
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
-
while (_) try {
|
|
33
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
-
switch (op[0]) {
|
|
36
|
-
case 0: case 1: t = op; break;
|
|
37
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
-
default:
|
|
41
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
-
if (t[2]) _.ops.pop();
|
|
46
|
-
_.trys.pop(); continue;
|
|
47
|
-
}
|
|
48
|
-
op = body.call(thisArg, _);
|
|
49
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
54
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
55
13
|
};
|
|
56
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
_this.messenger = messenger;
|
|
71
|
-
_this._scopes = scopes;
|
|
15
|
+
const cuid_1 = __importDefault(require("cuid"));
|
|
16
|
+
const starknet_1 = require("starknet");
|
|
17
|
+
const number_1 = require("starknet/utils/number");
|
|
18
|
+
const query_string_1 = __importDefault(require("query-string"));
|
|
19
|
+
const signer_1 = require("./signer");
|
|
20
|
+
class CartridgeAccount extends starknet_1.Account {
|
|
21
|
+
constructor(address, scopes = [], messenger, options) {
|
|
22
|
+
super(starknet_1.defaultProvider, address, new signer_1.Signer(messenger, options));
|
|
23
|
+
this.url = "https://x.cartridge.gg";
|
|
24
|
+
this._scopes = [];
|
|
25
|
+
this.address = address;
|
|
26
|
+
this.messenger = messenger;
|
|
27
|
+
this._scopes = scopes;
|
|
72
28
|
if (options === null || options === void 0 ? void 0 : options.url) {
|
|
73
|
-
|
|
29
|
+
this.url = options.url;
|
|
74
30
|
}
|
|
75
|
-
return _this;
|
|
76
31
|
}
|
|
77
32
|
/**
|
|
78
33
|
* Deploys a given compiled contract (json) to starknet
|
|
@@ -84,32 +39,24 @@ var CartridgeAccount = /** @class */ (function (_super) {
|
|
|
84
39
|
* @param abi the abi of the contract
|
|
85
40
|
* @returns a confirmation of sending a transaction on the starknet contract
|
|
86
41
|
*/
|
|
87
|
-
|
|
88
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
id: id,
|
|
99
|
-
payload: payload,
|
|
100
|
-
abi: abi,
|
|
101
|
-
},
|
|
102
|
-
})];
|
|
103
|
-
case 1:
|
|
104
|
-
response = _a.sent();
|
|
105
|
-
if (response.error) {
|
|
106
|
-
throw new Error(response.error);
|
|
107
|
-
}
|
|
108
|
-
return [2 /*return*/, response.result];
|
|
109
|
-
}
|
|
42
|
+
deployContract(payload, abi) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
const id = (0, cuid_1.default)();
|
|
45
|
+
window.open(`${this.url}/deploy?origin=${encodeURIComponent(window.origin)}&id=${id}`, "_blank", "height=650,width=400");
|
|
46
|
+
const response = yield this.messenger.send({
|
|
47
|
+
method: "deploy-contract",
|
|
48
|
+
params: {
|
|
49
|
+
id,
|
|
50
|
+
payload,
|
|
51
|
+
abi,
|
|
52
|
+
},
|
|
110
53
|
});
|
|
54
|
+
if (response.error) {
|
|
55
|
+
throw new Error(response.error);
|
|
56
|
+
}
|
|
57
|
+
return response.result;
|
|
111
58
|
});
|
|
112
|
-
}
|
|
59
|
+
}
|
|
113
60
|
/**
|
|
114
61
|
* Estimate Fee for a method on starknet
|
|
115
62
|
*
|
|
@@ -121,41 +68,23 @@ var CartridgeAccount = /** @class */ (function (_super) {
|
|
|
121
68
|
*
|
|
122
69
|
* @returns response from addTransaction
|
|
123
70
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return [3 /*break*/, 3];
|
|
135
|
-
case 1: return [4 /*yield*/, this.getNonce()];
|
|
136
|
-
case 2:
|
|
137
|
-
_d = (_e.sent());
|
|
138
|
-
_e.label = 3;
|
|
139
|
-
case 3:
|
|
140
|
-
nonce = _c.apply(void 0, [_d]);
|
|
141
|
-
return [4 /*yield*/, this.messenger.send({
|
|
142
|
-
method: "estimate-fee",
|
|
143
|
-
params: {
|
|
144
|
-
calls: calls,
|
|
145
|
-
nonce: nonce,
|
|
146
|
-
blockIdentifier: blockIdentifier,
|
|
147
|
-
},
|
|
148
|
-
})];
|
|
149
|
-
case 4:
|
|
150
|
-
response = _e.sent();
|
|
151
|
-
if (response.error) {
|
|
152
|
-
throw new Error(response.error);
|
|
153
|
-
}
|
|
154
|
-
return [2 /*return*/, response.result];
|
|
155
|
-
}
|
|
71
|
+
estimateFee(calls, { nonce: providedNonce, blockIdentifier } = {}) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const nonce = (0, number_1.toBN)(providedNonce !== null && providedNonce !== void 0 ? providedNonce : (yield this.getNonce()));
|
|
74
|
+
const response = yield this.messenger.send({
|
|
75
|
+
method: "estimate-fee",
|
|
76
|
+
params: {
|
|
77
|
+
calls,
|
|
78
|
+
nonce,
|
|
79
|
+
blockIdentifier,
|
|
80
|
+
},
|
|
156
81
|
});
|
|
82
|
+
if (response.error) {
|
|
83
|
+
throw new Error(response.error);
|
|
84
|
+
}
|
|
85
|
+
return response.result;
|
|
157
86
|
});
|
|
158
|
-
}
|
|
87
|
+
}
|
|
159
88
|
/**
|
|
160
89
|
* Invoke execute function in account contract
|
|
161
90
|
*
|
|
@@ -168,52 +97,43 @@ var CartridgeAccount = /** @class */ (function (_super) {
|
|
|
168
97
|
*
|
|
169
98
|
* @returns response from addTransaction
|
|
170
99
|
*/
|
|
171
|
-
|
|
172
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
abis: abis,
|
|
181
|
-
transactionsDetail: transactionsDetail,
|
|
182
|
-
},
|
|
183
|
-
})];
|
|
184
|
-
case 1:
|
|
185
|
-
response = _a.sent();
|
|
186
|
-
if (response.result) {
|
|
187
|
-
return [2 /*return*/, response.result];
|
|
188
|
-
}
|
|
189
|
-
if (response.error && response.error !== "missing scopes") {
|
|
190
|
-
throw new Error(response.error);
|
|
191
|
-
}
|
|
192
|
-
id = (0, cuid_1.default)();
|
|
193
|
-
window.open("".concat(this.url, "/execute?").concat(query_string_1.default.stringify({
|
|
194
|
-
id: id,
|
|
195
|
-
origin: window.origin,
|
|
196
|
-
calls: JSON.stringify(calls),
|
|
197
|
-
})), "_blank", "height=650,width=400");
|
|
198
|
-
return [4 /*yield*/, this.messenger.send({
|
|
199
|
-
method: "execute",
|
|
200
|
-
params: {
|
|
201
|
-
id: id,
|
|
202
|
-
calls: calls,
|
|
203
|
-
abis: abis,
|
|
204
|
-
transactionsDetail: transactionsDetail,
|
|
205
|
-
},
|
|
206
|
-
})];
|
|
207
|
-
case 2:
|
|
208
|
-
response = _a.sent();
|
|
209
|
-
if (response.error) {
|
|
210
|
-
throw new Error(response.error);
|
|
211
|
-
}
|
|
212
|
-
return [2 /*return*/, response.result];
|
|
213
|
-
}
|
|
100
|
+
execute(calls, abis, transactionsDetail) {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
let response = yield this.messenger.send({
|
|
103
|
+
method: "execute",
|
|
104
|
+
params: {
|
|
105
|
+
calls,
|
|
106
|
+
abis,
|
|
107
|
+
transactionsDetail,
|
|
108
|
+
},
|
|
214
109
|
});
|
|
110
|
+
if (response.result) {
|
|
111
|
+
return response.result;
|
|
112
|
+
}
|
|
113
|
+
if (response.error && response.error !== "missing scopes") {
|
|
114
|
+
throw new Error(response.error);
|
|
115
|
+
}
|
|
116
|
+
const id = (0, cuid_1.default)();
|
|
117
|
+
window.open(`${this.url}/execute?${query_string_1.default.stringify({
|
|
118
|
+
id,
|
|
119
|
+
origin: window.origin,
|
|
120
|
+
calls: JSON.stringify(calls),
|
|
121
|
+
})}`, "_blank", "height=650,width=400");
|
|
122
|
+
response = yield this.messenger.send({
|
|
123
|
+
method: "execute",
|
|
124
|
+
params: {
|
|
125
|
+
id,
|
|
126
|
+
calls,
|
|
127
|
+
abis,
|
|
128
|
+
transactionsDetail,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
if (response.error) {
|
|
132
|
+
throw new Error(response.error);
|
|
133
|
+
}
|
|
134
|
+
return response.result;
|
|
215
135
|
});
|
|
216
|
-
}
|
|
136
|
+
}
|
|
217
137
|
/**
|
|
218
138
|
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
|
|
219
139
|
* This adds a message prefix so it cant be interchanged with transactions
|
|
@@ -222,36 +142,27 @@ var CartridgeAccount = /** @class */ (function (_super) {
|
|
|
222
142
|
* @returns the signature of the JSON object
|
|
223
143
|
* @throws {Error} if the JSON object is not a valid JSON
|
|
224
144
|
*/
|
|
225
|
-
|
|
226
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
params: {
|
|
240
|
-
id: id,
|
|
241
|
-
typedData: typedData,
|
|
242
|
-
},
|
|
243
|
-
})];
|
|
244
|
-
case 1:
|
|
245
|
-
response = _a.sent();
|
|
246
|
-
if (response.error) {
|
|
247
|
-
throw new Error(response.error);
|
|
248
|
-
}
|
|
249
|
-
return [2 /*return*/, response.result];
|
|
250
|
-
}
|
|
145
|
+
signMessage(typedData) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
const id = (0, cuid_1.default)();
|
|
148
|
+
window.open(`${this.url}/sign?${query_string_1.default.stringify({
|
|
149
|
+
id,
|
|
150
|
+
origin: window.origin,
|
|
151
|
+
typedData: JSON.stringify(typedData),
|
|
152
|
+
})}`, "_blank", "height=650,width=400");
|
|
153
|
+
const response = yield this.messenger.send({
|
|
154
|
+
method: "sign-message",
|
|
155
|
+
params: {
|
|
156
|
+
id,
|
|
157
|
+
typedData,
|
|
158
|
+
},
|
|
251
159
|
});
|
|
160
|
+
if (response.error) {
|
|
161
|
+
throw new Error(response.error);
|
|
162
|
+
}
|
|
163
|
+
return response.result;
|
|
252
164
|
});
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
}(starknet_1.Account));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
256
167
|
exports.default = CartridgeAccount;
|
|
257
168
|
//# sourceMappingURL=account.js.map
|
package/lib/account.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,gDAAwB;AACxB,uCAakB;AAClB,kDAA6C;AAC7C,gEAA8B;AAY9B,qCAAkC;AAElC,MAAM,gBAAiB,SAAQ,kBAAO;IAMpC,YACE,OAAe,EACf,SAAkB,EAAE,EACpB,SAAoB,EACpB,OAEC;QAED,KAAK,CAAC,0BAAe,EAAE,OAAO,EAAE,IAAI,eAAM,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;QAX1D,QAAG,GAAW,wBAAwB,CAAC;QACvC,YAAO,GAAY,EAAE,CAAC;QAW5B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QAEtB,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,EAAE;YAChB,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;SACxB;IACH,CAAC;IAED;;;;;;;;;OASG;IACG,cAAc,CAClB,OAA8B,EAC9B,GAAS;;YAET,MAAM,EAAE,GAAG,IAAA,cAAI,GAAE,CAAC;YAElB,MAAM,CAAC,IAAI,CACT,GAAG,IAAI,CAAC,GAAG,kBAAkB,kBAAkB,CAC7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M,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAsB;gBAC9D,MAAM,EAAE,cAAc;gBACtB,MAAM,EAAE;oBACN,EAAE;oBACF,SAAS;iBACV;aACF,CAAC,CAAC;YAEH,IAAI,QAAQ,CAAC,KAAK,EAAE;gBAClB,MAAM,IAAI,KAAK,CAAC,QAAQ,CAAC,KAAe,CAAC,CAAC;aAC3C;YAED,OAAO,QAAQ,CAAC,MAAO,CAAC;QAC1B,CAAC;KAAA;CACF;AAED,kBAAe,gBAAgB,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -22,46 +22,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
22
22
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
23
|
});
|
|
24
24
|
};
|
|
25
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
26
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
27
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
28
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
29
|
-
function step(op) {
|
|
30
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
31
|
-
while (_) try {
|
|
32
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
33
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
34
|
-
switch (op[0]) {
|
|
35
|
-
case 0: case 1: t = op; break;
|
|
36
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
37
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
38
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
39
|
-
default:
|
|
40
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
41
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
42
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
43
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
44
|
-
if (t[2]) _.ops.pop();
|
|
45
|
-
_.trys.pop(); continue;
|
|
46
|
-
}
|
|
47
|
-
op = body.call(thisArg, _);
|
|
48
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
49
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
53
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
54
27
|
};
|
|
55
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
56
29
|
exports.Messenger = void 0;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
30
|
+
const cuid_1 = __importDefault(require("cuid"));
|
|
31
|
+
const query_string_1 = __importDefault(require("query-string"));
|
|
32
|
+
const account_1 = __importDefault(require("./account"));
|
|
33
|
+
const messenger_1 = __importDefault(require("./messenger"));
|
|
61
34
|
exports.Messenger = messenger_1.default;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
var _this = this;
|
|
35
|
+
class Controller {
|
|
36
|
+
constructor(scopes, options) {
|
|
65
37
|
this.selector = "cartridge-messenger";
|
|
66
38
|
this.scopes = [];
|
|
67
39
|
this.url = "https://x.cartridge.gg";
|
|
@@ -73,27 +45,19 @@ var Controller = /** @class */ (function () {
|
|
|
73
45
|
this.url = options.url;
|
|
74
46
|
}
|
|
75
47
|
if (typeof document !== "undefined") {
|
|
76
|
-
this.ready_ = new Promise(
|
|
77
|
-
window.addEventListener("message",
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
_a.sent();
|
|
86
|
-
this.loading = false;
|
|
87
|
-
resolve(true);
|
|
88
|
-
_a.label = 2;
|
|
89
|
-
case 2: return [2 /*return*/];
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}); });
|
|
48
|
+
this.ready_ = new Promise((resolve, reject) => {
|
|
49
|
+
window.addEventListener("message", (e) => __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
if (e.data.target === "cartridge" &&
|
|
51
|
+
e.data.payload.method === "ready") {
|
|
52
|
+
yield this.probe();
|
|
53
|
+
this.loading = false;
|
|
54
|
+
resolve(true);
|
|
55
|
+
}
|
|
56
|
+
}));
|
|
93
57
|
});
|
|
94
58
|
}
|
|
95
59
|
if (typeof document !== "undefined" && !this.messenger) {
|
|
96
|
-
|
|
60
|
+
let iframe = document.getElementById(this.selector);
|
|
97
61
|
if (!!iframe) {
|
|
98
62
|
if (!this.messenger) {
|
|
99
63
|
this.messenger = new messenger_1.default(iframe.contentWindow, this.url);
|
|
@@ -102,7 +66,7 @@ var Controller = /** @class */ (function () {
|
|
|
102
66
|
else {
|
|
103
67
|
iframe = document.createElement("iframe");
|
|
104
68
|
iframe.id = this.selector;
|
|
105
|
-
iframe.src =
|
|
69
|
+
iframe.src = `${this.url}`;
|
|
106
70
|
iframe.style.opacity = "0";
|
|
107
71
|
iframe.style.height = "0";
|
|
108
72
|
iframe.style.width = "0";
|
|
@@ -116,109 +80,79 @@ var Controller = /** @class */ (function () {
|
|
|
116
80
|
}
|
|
117
81
|
}
|
|
118
82
|
}
|
|
119
|
-
|
|
120
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return [2 /*return*/, this.ready_];
|
|
125
|
-
});
|
|
83
|
+
ready() {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
if (!this.loading)
|
|
86
|
+
return Promise.resolve(true);
|
|
87
|
+
return this.ready_;
|
|
126
88
|
});
|
|
127
|
-
}
|
|
128
|
-
|
|
89
|
+
}
|
|
90
|
+
probe() {
|
|
129
91
|
var _a, _b;
|
|
130
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
this.account = new account_1.default(probe.result.address, probe.result.scopes, this.messenger, {
|
|
141
|
-
url: this.url,
|
|
142
|
-
});
|
|
143
|
-
return [2 /*return*/, this.account];
|
|
144
|
-
}
|
|
145
|
-
return [2 /*return*/];
|
|
146
|
-
}
|
|
147
|
-
});
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const probe = yield ((_a = this.messenger) === null || _a === void 0 ? void 0 : _a.send({
|
|
94
|
+
method: "probe",
|
|
95
|
+
}));
|
|
96
|
+
if (this.messenger && ((_b = probe === null || probe === void 0 ? void 0 : probe.result) === null || _b === void 0 ? void 0 : _b.address)) {
|
|
97
|
+
this.account = new account_1.default(probe.result.address, probe.result.scopes, this.messenger, {
|
|
98
|
+
url: this.url,
|
|
99
|
+
});
|
|
100
|
+
return this.account;
|
|
101
|
+
}
|
|
148
102
|
});
|
|
149
|
-
}
|
|
103
|
+
}
|
|
150
104
|
// Register a new device key.
|
|
151
|
-
|
|
105
|
+
register(address) {
|
|
152
106
|
var _a;
|
|
153
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
method: "register",
|
|
159
|
-
params: {
|
|
160
|
-
address: address
|
|
161
|
-
}
|
|
162
|
-
}))];
|
|
163
|
-
case 1:
|
|
164
|
-
register = _b.sent();
|
|
165
|
-
if (!register || register.error) {
|
|
166
|
-
throw new Error("registration error");
|
|
167
|
-
}
|
|
168
|
-
return [2 /*return*/, register.result];
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const register = yield ((_a = this.messenger) === null || _a === void 0 ? void 0 : _a.send({
|
|
109
|
+
method: "register",
|
|
110
|
+
params: {
|
|
111
|
+
address
|
|
169
112
|
}
|
|
170
|
-
});
|
|
113
|
+
}));
|
|
114
|
+
if (!register || register.error) {
|
|
115
|
+
throw new Error("registration error");
|
|
116
|
+
}
|
|
117
|
+
return register.result;
|
|
171
118
|
});
|
|
172
|
-
}
|
|
173
|
-
|
|
119
|
+
}
|
|
120
|
+
connect() {
|
|
174
121
|
var _a;
|
|
175
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (!!!document.hasStorageAccess) return [3 /*break*/, 3];
|
|
185
|
-
return [4 /*yield*/, document.hasStorageAccess()];
|
|
186
|
-
case 1:
|
|
187
|
-
ok = _b.sent();
|
|
188
|
-
if (!!ok) return [3 /*break*/, 3];
|
|
189
|
-
return [4 /*yield*/, document.requestStorageAccess()];
|
|
190
|
-
case 2:
|
|
191
|
-
_b.sent();
|
|
192
|
-
_b.label = 3;
|
|
193
|
-
case 3:
|
|
194
|
-
window.open("".concat(this.url, "/connect?").concat(query_string_1.default.stringify({
|
|
195
|
-
id: id,
|
|
196
|
-
origin: window.origin,
|
|
197
|
-
scopes: JSON.stringify(this.scopes),
|
|
198
|
-
})), "_blank", "height=650,width=400");
|
|
199
|
-
return [4 /*yield*/, ((_a = this.messenger) === null || _a === void 0 ? void 0 : _a.send({
|
|
200
|
-
method: "connect",
|
|
201
|
-
params: {
|
|
202
|
-
id: id,
|
|
203
|
-
scopes: this.scopes,
|
|
204
|
-
},
|
|
205
|
-
}))];
|
|
206
|
-
case 4:
|
|
207
|
-
response = _b.sent();
|
|
208
|
-
if (!this.messenger || !response || response.error || !response.result) {
|
|
209
|
-
console.error("not ready for connect");
|
|
210
|
-
return [2 /*return*/, null];
|
|
211
|
-
}
|
|
212
|
-
this.account = new account_1.default(response.result.address, response.result.scopes, this.messenger, {
|
|
213
|
-
url: this.url,
|
|
214
|
-
});
|
|
215
|
-
return [2 /*return*/, this.account];
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
const id = (0, cuid_1.default)();
|
|
124
|
+
if (this.account) {
|
|
125
|
+
return this.account;
|
|
126
|
+
}
|
|
127
|
+
if (!!document.hasStorageAccess) {
|
|
128
|
+
const ok = yield document.hasStorageAccess();
|
|
129
|
+
if (!ok) {
|
|
130
|
+
yield document.requestStorageAccess();
|
|
216
131
|
}
|
|
132
|
+
}
|
|
133
|
+
window.open(`${this.url}/connect?${query_string_1.default.stringify({
|
|
134
|
+
id,
|
|
135
|
+
origin: window.origin,
|
|
136
|
+
scopes: JSON.stringify(this.scopes),
|
|
137
|
+
})}`, "_blank", "height=650,width=400");
|
|
138
|
+
const response = yield ((_a = this.messenger) === null || _a === void 0 ? void 0 : _a.send({
|
|
139
|
+
method: "connect",
|
|
140
|
+
params: {
|
|
141
|
+
id,
|
|
142
|
+
scopes: this.scopes,
|
|
143
|
+
},
|
|
144
|
+
}));
|
|
145
|
+
if (!this.messenger || !response || response.error || !response.result) {
|
|
146
|
+
console.error("not ready for connect");
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
this.account = new account_1.default(response.result.address, response.result.scopes, this.messenger, {
|
|
150
|
+
url: this.url,
|
|
217
151
|
});
|
|
152
|
+
return this.account;
|
|
218
153
|
});
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
}());
|
|
154
|
+
}
|
|
155
|
+
}
|
|
222
156
|
exports.default = Controller;
|
|
223
157
|
__exportStar(require("./types"), exports);
|
|
224
158
|
//# sourceMappingURL=index.js.map
|