@cartridge/controller 0.1.46 → 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.d.ts +2 -2
- package/lib/account.js +100 -189
- package/lib/account.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/lib/index.js +83 -149
- package/lib/index.js.map +1 -1
- package/lib/messenger.d.ts +1 -1
- package/lib/messenger.js +29 -47
- package/lib/messenger.js.map +1 -1
- package/lib/signer.d.ts +1 -1
- package/lib/signer.js +49 -93
- package/lib/signer.js.map +1 -1
- package/lib/utils.d.ts +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 -2
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
|
-
|
|
61
|
-
exports.Messenger =
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
var _this = this;
|
|
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"));
|
|
34
|
+
exports.Messenger = messenger_1.default;
|
|
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,36 +45,28 @@ 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
|
-
this.messenger = new
|
|
63
|
+
this.messenger = new messenger_1.default(iframe.contentWindow, this.url);
|
|
100
64
|
}
|
|
101
65
|
}
|
|
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";
|
|
@@ -112,113 +76,83 @@ var Controller = /** @class */ (function () {
|
|
|
112
76
|
iframe.sandbox.add("allow-storage-access-by-user-activation");
|
|
113
77
|
}
|
|
114
78
|
document.body.appendChild(iframe);
|
|
115
|
-
this.messenger = new
|
|
79
|
+
this.messenger = new messenger_1.default(iframe.contentWindow, this.url);
|
|
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_js_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_js_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
|
-
__exportStar(require("./types
|
|
157
|
+
__exportStar(require("./types"), exports);
|
|
224
158
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAwB;AACxB,gEAA8B;AAG9B,wDAAgC;AAChC,4DAAiD;AAgKxC,oBAhKF,mBAAS,CAgKE;AA7JlB,MAAM,UAAU;IASd,YACE,MAAgB,EAChB,OAGC;QAbK,aAAQ,GAAG,qBAAqB,CAAC;QAEjC,WAAM,GAAY,EAAE,CAAC;QACrB,QAAG,GAAW,wBAAwB,CAAC;QACvC,YAAO,GAAG,IAAI,CAAC;QAWrB,IAAI,MAAM,EAAE;YACV,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;SACtB;QAED,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,EAAE;YAChB,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;SACxB;QAED,IAAI,OAAO,QAAQ,KAAK,WAAW,EAAE;YACnC,IAAI,CAAC,MAAM,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBAC5C,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAO,CAAC,EAAE,EAAE;oBAC7C,IACE,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,WAAW;wBAC7B,C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,kBAAe,UAAU,CAAC;AAG1B,0CAAwB"}
|
package/lib/messenger.d.ts
CHANGED
package/lib/messenger.js
CHANGED
|
@@ -1,29 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
16
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (origin === void 0) { origin = "https://x.cartridge.gg"; }
|
|
21
|
-
var _this = this;
|
|
6
|
+
const cuid_1 = __importDefault(require("cuid"));
|
|
7
|
+
class Messenger {
|
|
8
|
+
constructor(target, origin = "https://x.cartridge.gg") {
|
|
22
9
|
this.pending = [];
|
|
23
|
-
this.defaultHandler =
|
|
10
|
+
this.defaultHandler = (e) => {
|
|
24
11
|
if (e.data.target === "cartridge" &&
|
|
25
12
|
e.data.type === "request") {
|
|
26
|
-
|
|
13
|
+
this.pending.push(e);
|
|
27
14
|
}
|
|
28
15
|
};
|
|
29
16
|
this.target = target;
|
|
@@ -34,48 +21,44 @@ var Messenger = /** @class */ (function () {
|
|
|
34
21
|
}
|
|
35
22
|
window.addEventListener("message", this.defaultHandler);
|
|
36
23
|
}
|
|
37
|
-
|
|
24
|
+
onRequest(cb) {
|
|
38
25
|
window.removeEventListener("message", this.defaultHandler);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
payload: __assign({ origin: window.origin }, response),
|
|
50
|
-
}, { targetOrigin: origin });
|
|
51
|
-
};
|
|
26
|
+
const onResponse = ({ origin, source, data: { id }, }) => (response) => {
|
|
27
|
+
if (!source) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
source.postMessage({
|
|
31
|
+
id,
|
|
32
|
+
target: "cartridge",
|
|
33
|
+
type: "response",
|
|
34
|
+
payload: Object.assign({ origin: window.origin }, response),
|
|
35
|
+
}, { targetOrigin: origin });
|
|
52
36
|
};
|
|
53
|
-
for (
|
|
54
|
-
|
|
37
|
+
for (let i = 0; i < this.pending.length; i++) {
|
|
38
|
+
const { data: { payload }, } = this.pending[i];
|
|
55
39
|
cb(payload, onResponse(this.pending[i]));
|
|
56
40
|
}
|
|
57
|
-
window.addEventListener("message",
|
|
58
|
-
|
|
41
|
+
window.addEventListener("message", (e) => {
|
|
42
|
+
const { data: { type, target, payload }, } = e;
|
|
59
43
|
if (target === "cartridge" &&
|
|
60
44
|
type === "request") {
|
|
61
45
|
cb(payload, onResponse(e));
|
|
62
46
|
}
|
|
63
47
|
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
48
|
+
}
|
|
49
|
+
send(request) {
|
|
50
|
+
const id = (0, cuid_1.default)();
|
|
67
51
|
if (!this.target) {
|
|
68
52
|
throw new Error("read only");
|
|
69
53
|
}
|
|
70
54
|
this.target.postMessage({
|
|
71
|
-
id
|
|
55
|
+
id,
|
|
72
56
|
target: "cartridge",
|
|
73
57
|
type: "request",
|
|
74
|
-
payload:
|
|
58
|
+
payload: Object.assign({ origin: window.origin }, request),
|
|
75
59
|
}, this.origin);
|
|
76
|
-
return new Promise(
|
|
77
|
-
|
|
78
|
-
var data = _a.data;
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
const handler = ({ data }) => {
|
|
79
62
|
if (data.target === "cartridge" &&
|
|
80
63
|
data.type === "response" &&
|
|
81
64
|
id === data.id) {
|
|
@@ -85,8 +68,7 @@ var Messenger = /** @class */ (function () {
|
|
|
85
68
|
};
|
|
86
69
|
window.addEventListener("message", handler);
|
|
87
70
|
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
91
73
|
exports.default = Messenger;
|
|
92
74
|
//# sourceMappingURL=messenger.js.map
|
package/lib/messenger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messenger.js","sourceRoot":"","sources":["../src/messenger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"messenger.js","sourceRoot":"","sources":["../src/messenger.ts"],"names":[],"mappings":";;;;;AAAA,gDAAwB;AAWxB,MAAM,SAAS;IAcb,YAAY,MAAqB,EAAE,SAAiB,wBAAwB;QAXpE,YAAO,GAAqC,EAAE,CAAA;QAE9C,mBAAc,GAAG,CAAC,CAAiC,EAAE,EAAE;YAC7D,IACE,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,WAAW;gBAC7B,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,EACzB;gBACA,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;aACrB;QACH,CAAC,CAAA;QAGC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,IACE,OAAO,QAAQ,KAAK,WAAW;YAC/B,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW,CAAC,KAAK,MAAM,EAClD;YACA,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;SACjD;QAED,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,cAAc,CAAC,CAAA;IACzD,CAAC;IAED,SAAS,CACP,EAAmE;QAEnE,MAAM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AAE;YAChB,MAAM,IAAI,KAAK,CAAC,WAAW,CAAC,CAAC;SAC9B;QAED,IAAI,CAAC,MAAM,CAAC,WAAW,CACrB;YACE,EAAE;YACF,MAAM,EAAE,WAAW;YACnB,IAAI,EAAE,SAAS;YACf,OAAO,kBACL,MAAM,EAAE,MAAM,CAAC,MAAM,IAClB,OAAO,CACX;SACF,EACD,IAAI,CAAC,MAAM,CACZ,CAAC;QAEF,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,MAAM,OAAO,GAAG,CAAC,EAAE,IAAI,EAA4B,EAAE,EAAE;gBACrD,IACE,IAAI,CAAC,MAAM,KAAK,WAAW;oBAC3B,IAAI,CAAC,IAAI,KAAK,UAAU;oBACxB,EAAE,KAAK,IAAI,CAAC,EAAE,EACd;oBACA,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;oBACtB,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;iBAChD;YACH,CAAC,CAAC;YACF,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,kBAAe,SAAS,CAAC"}
|
package/lib/signer.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Abi, Invocation, InvocationsSignerDetails, SignerInterface, Signature, typedData } from "starknet";
|
|
2
|
-
import Messenger from "./messenger
|
|
2
|
+
import Messenger from "./messenger";
|
|
3
3
|
export declare class Signer implements SignerInterface {
|
|
4
4
|
private messenger;
|
|
5
5
|
private url;
|
package/lib/signer.js
CHANGED
|
@@ -8,42 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
13
|
};
|
|
41
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
15
|
exports.Signer = void 0;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
16
|
+
const query_string_1 = __importDefault(require("query-string"));
|
|
17
|
+
const cuid_1 = __importDefault(require("cuid"));
|
|
18
|
+
class Signer {
|
|
19
|
+
constructor(messenger, options) {
|
|
47
20
|
this.url = "https://cartridge.gg";
|
|
48
21
|
this.messenger = messenger;
|
|
49
22
|
if (options === null || options === void 0 ? void 0 : options.url) {
|
|
@@ -55,9 +28,9 @@ var Signer = /** @class */ (function () {
|
|
|
55
28
|
*
|
|
56
29
|
* @returns public key of signer as hex string with 0x prefix
|
|
57
30
|
*/
|
|
58
|
-
|
|
31
|
+
getPubKey() {
|
|
59
32
|
return Promise.resolve("");
|
|
60
|
-
}
|
|
33
|
+
}
|
|
61
34
|
/**
|
|
62
35
|
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
|
|
63
36
|
* This adds a message prefix so it cant be interchanged with transactions
|
|
@@ -67,36 +40,28 @@ var Signer = /** @class */ (function () {
|
|
|
67
40
|
* @returns the signature of the JSON object
|
|
68
41
|
* @throws {Error} if the JSON object is not a valid JSON
|
|
69
42
|
*/
|
|
70
|
-
|
|
71
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
id: id,
|
|
86
|
-
account: account,
|
|
87
|
-
typedData: typedData,
|
|
88
|
-
},
|
|
89
|
-
})];
|
|
90
|
-
case 1:
|
|
91
|
-
response = _a.sent();
|
|
92
|
-
if (response.error) {
|
|
93
|
-
throw new Error(response.error);
|
|
94
|
-
}
|
|
95
|
-
return [2 /*return*/, response.result];
|
|
96
|
-
}
|
|
43
|
+
signMessage(typedData, account) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
const id = (0, cuid_1.default)();
|
|
46
|
+
window.open(`${this.url}/sign?${query_string_1.default.stringify({
|
|
47
|
+
id,
|
|
48
|
+
origin: window.origin,
|
|
49
|
+
message: JSON.stringify(typedData.message),
|
|
50
|
+
})}`, "_blank", "height=650,width=400");
|
|
51
|
+
const response = yield this.messenger.send({
|
|
52
|
+
method: "sign-message",
|
|
53
|
+
params: {
|
|
54
|
+
id,
|
|
55
|
+
account,
|
|
56
|
+
typedData,
|
|
57
|
+
},
|
|
97
58
|
});
|
|
59
|
+
if (response.error) {
|
|
60
|
+
throw new Error(response.error);
|
|
61
|
+
}
|
|
62
|
+
return response.result;
|
|
98
63
|
});
|
|
99
|
-
}
|
|
64
|
+
}
|
|
100
65
|
/**
|
|
101
66
|
* Signs a transaction with the starknet private key and returns the signature
|
|
102
67
|
*
|
|
@@ -109,39 +74,30 @@ var Signer = /** @class */ (function () {
|
|
|
109
74
|
*
|
|
110
75
|
* @returns signature
|
|
111
76
|
*/
|
|
112
|
-
|
|
113
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
transactions: transactions,
|
|
130
|
-
abis: abis,
|
|
131
|
-
transactionsDetail: transactionsDetail,
|
|
132
|
-
},
|
|
133
|
-
})];
|
|
134
|
-
case 1:
|
|
135
|
-
response = _a.sent();
|
|
136
|
-
if (response.error) {
|
|
137
|
-
throw new Error(response.error);
|
|
138
|
-
}
|
|
139
|
-
return [2 /*return*/, response.result];
|
|
140
|
-
}
|
|
77
|
+
signTransaction(transactions, transactionsDetail, abis) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const id = (0, cuid_1.default)();
|
|
80
|
+
const calls = Array.isArray(transactions) ? transactions : [transactions];
|
|
81
|
+
window.open(`${this.url}/sign?${query_string_1.default.stringify({
|
|
82
|
+
id,
|
|
83
|
+
origin: window.origin,
|
|
84
|
+
calls: JSON.stringify(calls),
|
|
85
|
+
})}`, "_blank", "height=650,width=400");
|
|
86
|
+
const response = yield this.messenger.send({
|
|
87
|
+
method: "sign-transaction",
|
|
88
|
+
params: {
|
|
89
|
+
id,
|
|
90
|
+
transactions,
|
|
91
|
+
abis,
|
|
92
|
+
transactionsDetail,
|
|
93
|
+
},
|
|
141
94
|
});
|
|
95
|
+
if (response.error) {
|
|
96
|
+
throw new Error(response.error);
|
|
97
|
+
}
|
|
98
|
+
return response.result;
|
|
142
99
|
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
}());
|
|
100
|
+
}
|
|
101
|
+
}
|
|
146
102
|
exports.Signer = Signer;
|
|
147
103
|
//# sourceMappingURL=signer.js.map
|