@cartridge/controller 0.1.0 → 0.1.1

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.
@@ -0,0 +1,185 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import cuid from "cuid";
38
+ import { Account } from "./account";
39
+ import { Messenger } from "./messenger";
40
+ import qs from "query-string";
41
+ var Cartridge = /** @class */ (function () {
42
+ function Cartridge(scopes, options) {
43
+ var _this = this;
44
+ this.selector = "cartridge-messenger";
45
+ this.scopes = [];
46
+ this.url = "https://cartridge.gg";
47
+ this.origin = "https://cartridge.gg";
48
+ this.loading = true;
49
+ if (scopes) {
50
+ this.scopes = scopes;
51
+ }
52
+ if (options === null || options === void 0 ? void 0 : options.url) {
53
+ this.url = options.url;
54
+ }
55
+ if (options === null || options === void 0 ? void 0 : options.origin) {
56
+ this.origin = options.origin;
57
+ }
58
+ if (typeof document !== "undefined") {
59
+ this.ready_ = new Promise(function (resolve, reject) {
60
+ window.addEventListener("message", function (e) { return __awaiter(_this, void 0, void 0, function () {
61
+ return __generator(this, function (_a) {
62
+ switch (_a.label) {
63
+ case 0:
64
+ if (!(e.data.target === "cartridge" &&
65
+ e.data.payload.method === "ready")) return [3 /*break*/, 2];
66
+ return [4 /*yield*/, this.probe()];
67
+ case 1:
68
+ _a.sent();
69
+ this.loading = false;
70
+ resolve(true);
71
+ _a.label = 2;
72
+ case 2: return [2 /*return*/];
73
+ }
74
+ });
75
+ }); });
76
+ });
77
+ }
78
+ if (typeof document !== "undefined" && !this.messenger) {
79
+ var iframe = document.getElementById(this.selector);
80
+ if (!!iframe) {
81
+ if (!this.messenger) {
82
+ this.messenger = new Messenger(iframe.contentWindow, this.origin);
83
+ }
84
+ }
85
+ else {
86
+ iframe = document.createElement("iframe");
87
+ iframe.id = this.selector;
88
+ iframe.src = "".concat(this.url, "/iframe");
89
+ iframe.style.opacity = "0";
90
+ iframe.style.height = "0";
91
+ iframe.style.width = "0";
92
+ iframe.sandbox.add("allow-scripts");
93
+ iframe.sandbox.add("allow-same-origin");
94
+ if (!!document.hasStorageAccess) {
95
+ iframe.sandbox.add("allow-storage-access-by-user-activation");
96
+ }
97
+ document.body.appendChild(iframe);
98
+ this.messenger = new Messenger(iframe.contentWindow, this.origin);
99
+ }
100
+ }
101
+ }
102
+ Cartridge.prototype.ready = function () {
103
+ return __awaiter(this, void 0, void 0, function () {
104
+ return __generator(this, function (_a) {
105
+ if (!this.loading)
106
+ return [2 /*return*/, Promise.resolve(true)];
107
+ return [2 /*return*/, this.ready_];
108
+ });
109
+ });
110
+ };
111
+ Cartridge.prototype.probe = function () {
112
+ var _a, _b;
113
+ return __awaiter(this, void 0, void 0, function () {
114
+ var prob;
115
+ return __generator(this, function (_c) {
116
+ switch (_c.label) {
117
+ case 0: return [4 /*yield*/, ((_a = this.messenger) === null || _a === void 0 ? void 0 : _a.send({
118
+ method: "probe",
119
+ }))];
120
+ case 1:
121
+ prob = _c.sent();
122
+ if (this.messenger && ((_b = prob === null || prob === void 0 ? void 0 : prob.result) === null || _b === void 0 ? void 0 : _b.address)) {
123
+ this.account = new Account(prob.result.address, prob.result.scopes, this.messenger, {
124
+ url: this.url,
125
+ });
126
+ return [2 /*return*/, this.account];
127
+ }
128
+ return [2 /*return*/];
129
+ }
130
+ });
131
+ });
132
+ };
133
+ Cartridge.prototype.connect = function () {
134
+ var _a;
135
+ return __awaiter(this, void 0, void 0, function () {
136
+ var id, ok, response;
137
+ return __generator(this, function (_b) {
138
+ switch (_b.label) {
139
+ case 0:
140
+ id = cuid();
141
+ if (this.account) {
142
+ return [2 /*return*/, this.account];
143
+ }
144
+ if (!!!document.hasStorageAccess) return [3 /*break*/, 3];
145
+ return [4 /*yield*/, document.hasStorageAccess()];
146
+ case 1:
147
+ ok = _b.sent();
148
+ if (!!ok) return [3 /*break*/, 3];
149
+ return [4 /*yield*/, document.requestStorageAccess()];
150
+ case 2:
151
+ _b.sent();
152
+ _b.label = 3;
153
+ case 3:
154
+ window.open("".concat(this.url, "/connect?").concat(qs.stringify({
155
+ id: id,
156
+ origin: window.origin,
157
+ scopes: JSON.stringify(this.scopes),
158
+ })), "_blank", "height=650,width=400");
159
+ return [4 /*yield*/, ((_a = this.messenger) === null || _a === void 0 ? void 0 : _a.send({
160
+ method: "connect",
161
+ params: {
162
+ id: id,
163
+ scopes: this.scopes,
164
+ },
165
+ }))];
166
+ case 4:
167
+ response = _b.sent();
168
+ if (!this.messenger || !response || response.error || !response.result) {
169
+ console.error("not ready for connect");
170
+ return [2 /*return*/];
171
+ }
172
+ this.account = new Account(response.result.address, response.result.scopes, this.messenger, {
173
+ url: this.url,
174
+ });
175
+ return [2 /*return*/, this.account];
176
+ }
177
+ });
178
+ });
179
+ };
180
+ return Cartridge;
181
+ }());
182
+ export { Cartridge };
183
+ export { Messenger };
184
+ export * from "./types";
185
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAW,SAAS,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,MAAM,cAAc,CAAC;AAG9B;IAUE,mBACE,MAAgB,EAChB,OAGC;QALH,iBA0DC;QAnEO,aAAQ,GAAG,qBAAqB,CAAC;QAEjC,WAAM,GAAY,EAAE,CAAC;QACrB,QAAG,GAAW,sBAAsB,CAAC;QACrC,WAAM,GAAW,sBAAsB,CAAC;QACxC,YAAO,GAAG,IAAI,CAAC;QAWrB,IAAI,MAAM,EAAE;YACV,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;SACt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AAO,GAAG,IAAI,OAAO,CACxB,QAAQ,CAAC,MAAM,CAAC,OAAQ,EACxB,QAAQ,CAAC,MAAM,CAAC,MAAM,EACtB,IAAI,CAAC,SAAS,EACd;4BACE,GAAG,EAAE,IAAI,CAAC,GAAG;yBACd,CACF,CAAC;wBAEF,sBAAO,IAAI,CAAC,OAAO,EAAC;;;;KACrB;IACH,gBAAC;AAAD,CAAC,AA9ID,IA8IC;;AAED,OAAO,EAAW,SAAS,EAAE,CAAC;AAC9B,cAAc,SAAS,CAAC"}
@@ -1,17 +1,16 @@
1
- import { Request, Response } from "./types";
2
- export declare type Message<T = Request | Response> = {
3
- id?: string;
4
- type: "request" | "response";
5
- target: string;
6
- payload: T;
7
- };
8
- export declare class Messenger {
9
- private target;
10
- private origin;
11
- private pending;
12
- private defaultHandler;
13
- constructor(target?: Window, origin?: string);
14
- onRequest(cb: (request: Request, reply: <T = Response>(response: T) => void) => void): void;
15
- send<T = Response>(request: Request): Promise<T>;
16
- }
17
- //# sourceMappingURL=messenger.d.ts.map
1
+ import { Request, Response } from "./types";
2
+ export declare type Message<T = Request | Response> = {
3
+ id?: string;
4
+ type: "request" | "response";
5
+ target: string;
6
+ payload: T;
7
+ };
8
+ export declare class Messenger {
9
+ private target;
10
+ private origin;
11
+ private pending;
12
+ private defaultHandler;
13
+ constructor(target: Window | null, origin?: string);
14
+ onRequest(cb: (request: Request, reply: (response: Response) => void) => void): void;
15
+ send<T = Response>(request: Request): Promise<T>;
16
+ }
@@ -0,0 +1,87 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import cuid from "cuid";
13
+ var Messenger = /** @class */ (function () {
14
+ function Messenger(target, origin) {
15
+ if (origin === void 0) { origin = "https://cartridge.gg"; }
16
+ var _this = this;
17
+ this.pending = [];
18
+ this.defaultHandler = function (e) {
19
+ if (e.data.target === "cartridge" &&
20
+ e.data.type === "request") {
21
+ _this.pending.push(e);
22
+ }
23
+ };
24
+ this.target = target;
25
+ this.origin = origin;
26
+ if (typeof document !== "undefined" &&
27
+ document.body.getAttribute("cartridge") !== "true") {
28
+ document.body.setAttribute("cartridge", "true");
29
+ }
30
+ window.addEventListener("message", this.defaultHandler);
31
+ }
32
+ Messenger.prototype.onRequest = function (cb) {
33
+ window.removeEventListener("message", this.defaultHandler);
34
+ var onResponse = function (_a) {
35
+ var origin = _a.origin, source = _a.source, id = _a.data.id;
36
+ return function (response) {
37
+ if (!source) {
38
+ return;
39
+ }
40
+ source.postMessage({
41
+ id: id,
42
+ target: "cartridge",
43
+ type: "response",
44
+ payload: __assign({ origin: window.origin }, response),
45
+ }, { targetOrigin: origin });
46
+ };
47
+ };
48
+ for (var i = 0; i < this.pending.length; i++) {
49
+ var payload = this.pending[i].data.payload;
50
+ cb(payload, onResponse(this.pending[i]));
51
+ }
52
+ window.addEventListener("message", function (e) {
53
+ var _a = e.data, type = _a.type, target = _a.target, payload = _a.payload;
54
+ if (target === "cartridge" &&
55
+ type === "request") {
56
+ cb(payload, onResponse(e));
57
+ }
58
+ });
59
+ };
60
+ Messenger.prototype.send = function (request) {
61
+ var id = cuid();
62
+ if (!this.target) {
63
+ throw new Error("read only");
64
+ }
65
+ this.target.postMessage({
66
+ id: id,
67
+ target: "cartridge",
68
+ type: "request",
69
+ payload: __assign({ origin: window.origin }, request),
70
+ }, this.origin);
71
+ return new Promise(function (resolve, reject) {
72
+ var handler = function (_a) {
73
+ var data = _a.data;
74
+ if (data.target === "cartridge" &&
75
+ data.type === "response" &&
76
+ id === data.id) {
77
+ resolve(data.payload);
78
+ window.removeEventListener("message", handler);
79
+ }
80
+ };
81
+ window.addEventListener("message", handler);
82
+ });
83
+ };
84
+ return Messenger;
85
+ }());
86
+ export { Messenger };
87
+ //# sourceMappingURL=messenger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messenger.js","sourceRoot":"","sources":["../../src/messenger.ts"],"names":[],"mappings":";;;;;;;;;;;AACA,OAAO,IAAI,MAAM,MAAM,CAAC;AASxB;IAcE,mBAAY,MAAqB,EAAE,MAAuC;QAAvC,uBAAA,EAAA,+BAAuC;QAA1E,iBAYC;QAvBO,YAAO,GAAqC,EAAE,CAAA;QAE9C,mBAAc,GAAG,UAAC,CAAiC;YACzD,IACE,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,WAAW;gBAC7B,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,EACzB;gBACA,KAAI,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,6BAAS,GAAT,UACE,EAAmE;QAEnE,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,cAAc,CAAC,CAAA;QAE1D,IAAM,UAAU,GAAG,UAAC,EAIa;gBAH/B,MAAM,YAAA,EACN,MAAM,YAAA,EACE,EAAE,aAAA;YAC0B,OAAA,UAAC,QAAkB;gBACvD,IAAI,CAAC,MAAM,EAAE;oBACX,OAAM;iBACP;gBAED,MAAM,CAAC,WAAW,CAChB;oBACE,EAAE,IAAA;oBACF,MAAM,EAAE,WAAW;oBACnB,IAAI,EAA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AAhC,IAAI,UAAA;gBACrB,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;IACH,gBAAC;AAAD,CAAC,AAhHD,IAgHC"}
@@ -1,39 +1,38 @@
1
- import { Abi, Invocation, InvocationsSignerDetails, SignerInterface, Signature, typedData } from "starknet";
2
- import { Messenger } from "./messenger";
3
- export declare class Signer implements SignerInterface {
4
- private messenger;
5
- private url;
6
- constructor(messenger: Messenger, options?: {
7
- url?: string;
8
- });
9
- /**
10
- * Method to get the public key of the signer
11
- *
12
- * @returns public key of signer as hex string with 0x prefix
13
- */
14
- getPubKey(): Promise<string>;
15
- /**
16
- * Sign an JSON object for off-chain usage with the starknet private key and return the signature
17
- * This adds a message prefix so it cant be interchanged with transactions
18
- *
19
- * @param typedData - JSON object to be signed
20
- * @param accountAddress - account
21
- * @returns the signature of the JSON object
22
- * @throws {Error} if the JSON object is not a valid JSON
23
- */
24
- signMessage(typedData: typedData.TypedData, account: string): Promise<Signature>;
25
- /**
26
- * Signs a transaction with the starknet private key and returns the signature
27
- *
28
- * @param invocation the invocation object containing:
29
- * - contractAddress - the address of the contract
30
- * - entrypoint - the entrypoint of the contract
31
- * - calldata - (defaults to []) the calldata
32
- * - signature - (defaults to []) the signature
33
- * @param abi (optional) the abi of the contract for better displaying
34
- *
35
- * @returns signature
36
- */
37
- signTransaction(transactions: Invocation[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
38
- }
39
- //# sourceMappingURL=signer.d.ts.map
1
+ import { Abi, Invocation, InvocationsSignerDetails, SignerInterface, Signature, typedData } from "starknet";
2
+ import { Messenger } from "./messenger";
3
+ export declare class Signer implements SignerInterface {
4
+ private messenger;
5
+ private url;
6
+ constructor(messenger: Messenger, options?: {
7
+ url?: string;
8
+ });
9
+ /**
10
+ * Method to get the public key of the signer
11
+ *
12
+ * @returns public key of signer as hex string with 0x prefix
13
+ */
14
+ getPubKey(): Promise<string>;
15
+ /**
16
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
17
+ * This adds a message prefix so it cant be interchanged with transactions
18
+ *
19
+ * @param typedData - JSON object to be signed
20
+ * @param accountAddress - account
21
+ * @returns the signature of the JSON object
22
+ * @throws {Error} if the JSON object is not a valid JSON
23
+ */
24
+ signMessage(typedData: typedData.TypedData, account: string): Promise<Signature>;
25
+ /**
26
+ * Signs a transaction with the starknet private key and returns the signature
27
+ *
28
+ * @param invocation the invocation object containing:
29
+ * - contractAddress - the address of the contract
30
+ * - entrypoint - the entrypoint of the contract
31
+ * - calldata - (defaults to []) the calldata
32
+ * - signature - (defaults to []) the signature
33
+ * @param abi (optional) the abi of the contract for better displaying
34
+ *
35
+ * @returns signature
36
+ */
37
+ signTransaction(transactions: Invocation[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
38
+ }
@@ -0,0 +1,141 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import qs from 'query-string';
38
+ import cuid from "cuid";
39
+ var Signer = /** @class */ (function () {
40
+ function Signer(messenger, options) {
41
+ this.url = "https://cartridge.gg";
42
+ this.messenger = messenger;
43
+ if (options === null || options === void 0 ? void 0 : options.url) {
44
+ this.url = options.url;
45
+ }
46
+ }
47
+ /**
48
+ * Method to get the public key of the signer
49
+ *
50
+ * @returns public key of signer as hex string with 0x prefix
51
+ */
52
+ Signer.prototype.getPubKey = function () {
53
+ return Promise.resolve("");
54
+ };
55
+ /**
56
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
57
+ * This adds a message prefix so it cant be interchanged with transactions
58
+ *
59
+ * @param typedData - JSON object to be signed
60
+ * @param accountAddress - account
61
+ * @returns the signature of the JSON object
62
+ * @throws {Error} if the JSON object is not a valid JSON
63
+ */
64
+ Signer.prototype.signMessage = function (typedData, account) {
65
+ return __awaiter(this, void 0, void 0, function () {
66
+ var id, response;
67
+ return __generator(this, function (_a) {
68
+ switch (_a.label) {
69
+ case 0:
70
+ id = cuid();
71
+ window.open("".concat(this.url, "/sign?").concat(qs.stringify({
72
+ id: id,
73
+ origin: window.origin,
74
+ message: JSON.stringify(typedData.message),
75
+ })), "_blank", "height=650,width=400");
76
+ return [4 /*yield*/, this.messenger.send({
77
+ method: "sign-message",
78
+ params: {
79
+ id: id,
80
+ account: account,
81
+ typedData: typedData,
82
+ },
83
+ })];
84
+ case 1:
85
+ response = _a.sent();
86
+ if (response.error) {
87
+ throw new Error(response.error);
88
+ }
89
+ return [2 /*return*/, response.result];
90
+ }
91
+ });
92
+ });
93
+ };
94
+ /**
95
+ * Signs a transaction with the starknet private key and returns the signature
96
+ *
97
+ * @param invocation the invocation object containing:
98
+ * - contractAddress - the address of the contract
99
+ * - entrypoint - the entrypoint of the contract
100
+ * - calldata - (defaults to []) the calldata
101
+ * - signature - (defaults to []) the signature
102
+ * @param abi (optional) the abi of the contract for better displaying
103
+ *
104
+ * @returns signature
105
+ */
106
+ Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
107
+ return __awaiter(this, void 0, void 0, function () {
108
+ var id, calls, response;
109
+ return __generator(this, function (_a) {
110
+ switch (_a.label) {
111
+ case 0:
112
+ id = cuid();
113
+ calls = Array.isArray(transactions) ? transactions : [transactions];
114
+ window.open("".concat(this.url, "/sign?").concat(qs.stringify({
115
+ id: id,
116
+ origin: window.origin,
117
+ calls: JSON.stringify(calls),
118
+ })), "_blank", "height=650,width=400");
119
+ return [4 /*yield*/, this.messenger.send({
120
+ method: "sign-transaction",
121
+ params: {
122
+ id: id,
123
+ transactions: transactions,
124
+ abis: abis,
125
+ transactionsDetail: transactionsDetail,
126
+ },
127
+ })];
128
+ case 1:
129
+ response = _a.sent();
130
+ if (response.error) {
131
+ throw new Error(response.error);
132
+ }
133
+ return [2 /*return*/, response.result];
134
+ }
135
+ });
136
+ });
137
+ };
138
+ return Signer;
139
+ }());
140
+ export { Signer };
141
+ //# sourceMappingURL=signer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signer.js","sourceRoot":"","sources":["../../src/signer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,OAAO,EAAE,MAAM,cAAc,CAAC;AAC9B,OAAO,IAAI,MAAM,MAAM,CAAC;AAQxB;IAII,gBAAY,SAAoB,EAAE,OAEjC;QAJO,QAAG,GAAW,sBAAsB,CAAC;QAKzC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAE3B,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,EAAE;YACd,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;SAC1B;IACL,CAAC;IAED;;;;KAIC;IACM,0BAAS,GAAhB;QACI,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;IAC9B,CAAC;IAED;;;;;;;;OAQG;IACU,4BAAW,GAAxB,UAAyB,SAA8B,EAAE,OAAe;;;;;;wBAC9D,EAAE,GAAG,IAAI,EAAE,CAAA;wBAEjB,MAAM,CAAC,IAAI,CACP,UAAG,IAAI,CAAC,GAAG,mBAAS,EAAE,CAAC,SAAS,CAAC;4BAC7B,EAAE,IAAA;4BACF,MAAM,EAAE,MAAM,CAAC,MAAM;4BACrB,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC;yBAC7C,CAAC,CAAE,EACJ,QAAQ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