@cartridge/controller 0.1.51 → 0.1.53

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,57 @@
1
+ import { Account, DeployContractPayload, Abi, Call, EstimateFeeDetails, DeployContractResponse as StarknetDeployContractResponse, InvocationsDetails, Signature, typedData, InvokeFunctionResponse, EstimateFee } from "starknet";
2
+ import { Scope, Keychain } from "./types";
3
+ import { AsyncMethodReturns } from "@cartridge/penpal";
4
+ declare class CartridgeAccount extends Account {
5
+ address: string;
6
+ private keychain;
7
+ private url;
8
+ constructor(address: string, scopes: Scope[] | undefined, keychain: AsyncMethodReturns<Keychain>, options?: {
9
+ url?: string;
10
+ });
11
+ /**
12
+ * Deploys a given compiled contract (json) to starknet
13
+ *
14
+ * @param payload payload to be deployed containing:
15
+ * - compiled contract code
16
+ * - constructor calldata
17
+ * - address salt
18
+ * @param abi the abi of the contract
19
+ * @returns a confirmation of sending a transaction on the starknet contract
20
+ */
21
+ deployContract(payload: DeployContractPayload, abi?: Abi): Promise<StarknetDeployContractResponse>;
22
+ /**
23
+ * Estimate Fee for a method on starknet
24
+ *
25
+ * @param calls the invocation object containing:
26
+ * - contractAddress - the address of the contract
27
+ * - entrypoint - the entrypoint of the contract
28
+ * - calldata - (defaults to []) the calldata
29
+ * - signature - (defaults to []) the signature
30
+ *
31
+ * @returns response from addTransaction
32
+ */
33
+ estimateFee(calls: Call | Call[], details?: EstimateFeeDetails): Promise<EstimateFee>;
34
+ /**
35
+ * Invoke execute function in account contract
36
+ *
37
+ * @param calls the invocation object or an array of them, containing:
38
+ * - contractAddress - the address of the contract
39
+ * - entrypoint - the entrypoint of the contract
40
+ * - calldata - (defaults to []) the calldata
41
+ * - signature - (defaults to []) the signature
42
+ * @param abis (optional) the abi of the contract for better displaying
43
+ *
44
+ * @returns response from addTransaction
45
+ */
46
+ execute(calls: Call | Call[], abis?: Abi[], transactionsDetail?: InvocationsDetails): Promise<InvokeFunctionResponse>;
47
+ /**
48
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
49
+ * This adds a message prefix so it cant be interchanged with transactions
50
+ *
51
+ * @param json - JSON object to be signed
52
+ * @returns the signature of the JSON object
53
+ * @throws {Error} if the JSON object is not a valid JSON
54
+ */
55
+ signMessage(typedData: typedData.TypedData): Promise<Signature>;
56
+ }
57
+ export default CartridgeAccount;
package/lib/account.js CHANGED
@@ -14,17 +14,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const cuid_1 = __importDefault(require("cuid"));
16
16
  const starknet_1 = require("starknet");
17
- const number_1 = require("starknet/utils/number");
18
17
  const query_string_1 = __importDefault(require("query-string"));
19
18
  const signer_1 = require("./signer");
19
+ const constants_1 = require("starknet/constants");
20
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));
21
+ constructor(address, scopes = [], keychain, options) {
22
+ super(starknet_1.defaultProvider, address, new signer_1.Signer(keychain, options));
23
23
  this.url = "https://x.cartridge.gg";
24
- this._scopes = [];
25
24
  this.address = address;
26
- this.messenger = messenger;
27
- this._scopes = scopes;
25
+ this.keychain = keychain;
28
26
  if (options === null || options === void 0 ? void 0 : options.url) {
29
27
  this.url = options.url;
30
28
  }
@@ -43,18 +41,19 @@ class CartridgeAccount extends starknet_1.Account {
43
41
  return __awaiter(this, void 0, void 0, function* () {
44
42
  const id = (0, cuid_1.default)();
45
43
  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
- },
53
- });
54
- if (response.error) {
55
- throw new Error(response.error);
56
- }
57
- return response.result;
44
+ throw new Error("unimplemented");
45
+ // const response = await this.messenger.send<DeployContractResponse>({
46
+ // method: "deploy-contract",
47
+ // params: {
48
+ // id,
49
+ // payload,
50
+ // abi,
51
+ // },
52
+ // });
53
+ // if (response.error) {
54
+ // throw new Error(response.error as string);
55
+ // }
56
+ // return response.result!;
58
57
  });
59
58
  }
60
59
  /**
@@ -68,21 +67,9 @@ class CartridgeAccount extends starknet_1.Account {
68
67
  *
69
68
  * @returns response from addTransaction
70
69
  */
71
- estimateFee(calls, { nonce: providedNonce, blockIdentifier } = {}) {
70
+ estimateFee(calls, details) {
72
71
  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
- },
81
- });
82
- if (response.error) {
83
- throw new Error(response.error);
84
- }
85
- return response.result;
72
+ return this.keychain.estimateFee(calls, details);
86
73
  });
87
74
  }
88
75
  /**
@@ -99,39 +86,42 @@ class CartridgeAccount extends starknet_1.Account {
99
86
  */
100
87
  execute(calls, abis, transactionsDetail) {
101
88
  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
- },
109
- });
110
- if (response.result) {
111
- return response.result;
89
+ if (!transactionsDetail) {
90
+ transactionsDetail = {};
112
91
  }
113
- if (response.error && response.error !== "missing scopes") {
114
- throw new Error(response.error);
92
+ if (!transactionsDetail.nonce) {
93
+ transactionsDetail.nonce = 0; //await this.getNonce();
94
+ }
95
+ if (!transactionsDetail.version) {
96
+ transactionsDetail.version = 1;
97
+ }
98
+ if (!transactionsDetail.maxFee) {
99
+ try {
100
+ transactionsDetail.maxFee = "100"; // (await this.estimateFee(calls, { nonce: transactionsDetail.nonce })).suggestedMaxFee
101
+ }
102
+ catch (e) {
103
+ console.error(e);
104
+ throw e;
105
+ }
106
+ }
107
+ try {
108
+ return yield this.keychain.execute(calls, abis, transactionsDetail);
109
+ }
110
+ catch (e) {
111
+ if (e.message !== "missing scopes") {
112
+ console.error(e);
113
+ throw e;
114
+ }
115
115
  }
116
- const id = (0, cuid_1.default)();
117
116
  window.open(`${this.url}/execute?${query_string_1.default.stringify({
118
- id,
119
117
  origin: window.origin,
120
118
  calls: JSON.stringify(calls),
119
+ nonce: transactionsDetail.nonce,
120
+ version: transactionsDetail.version,
121
+ maxFee: transactionsDetail.maxFee,
122
+ chainId: constants_1.StarknetChainId.TESTNET,
121
123
  })}`, "_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;
124
+ return this.keychain.execute(calls, abis, transactionsDetail, true);
135
125
  });
136
126
  }
137
127
  /**
@@ -144,23 +134,10 @@ class CartridgeAccount extends starknet_1.Account {
144
134
  */
145
135
  signMessage(typedData) {
146
136
  return __awaiter(this, void 0, void 0, function* () {
147
- const id = (0, cuid_1.default)();
148
137
  window.open(`${this.url}/sign?${query_string_1.default.stringify({
149
- id,
150
- origin: window.origin,
151
138
  typedData: JSON.stringify(typedData),
152
139
  })}`, "_blank", "height=650,width=400");
153
- const response = yield this.messenger.send({
154
- method: "sign-message",
155
- params: {
156
- id,
157
- typedData,
158
- },
159
- });
160
- if (response.error) {
161
- throw new Error(response.error);
162
- }
163
- return response.result;
140
+ return this.keychain.signMessage(typedData, this.address);
164
141
  });
165
142
  }
166
143
  }
@@ -1 +1 @@
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,C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SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC;aACrC,CAAC,EAAE,EACJ,QAAQ,EACR,sBAAsB,CACvB,CAAC;YAEF,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC5D,CAAC;KAAA;CACF;AAED,kBAAe,gBAAgB,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { Scope } from "./types";
2
+ export declare class MissingScopes extends Error {
3
+ missing: Scope[];
4
+ constructor(missing: Scope[]);
5
+ }
package/lib/errors.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MissingScopes = void 0;
4
+ class MissingScopes extends Error {
5
+ constructor(missing) {
6
+ super("missing scopes");
7
+ this.missing = missing;
8
+ // because we are extending a built-in class
9
+ Object.setPrototypeOf(this, MissingScopes.prototype);
10
+ }
11
+ }
12
+ exports.MissingScopes = MissingScopes;
13
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAEA,MAAa,aAAc,SAAQ,KAAK;IAGpC,YAAY,OAAgB;QACxB,KAAK,CAAC,gBAAgB,CAAC,CAAC;QAExB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,4CAA4C;QAC5C,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,aAAa,CAAC,SAAS,CAAC,CAAC;IACzD,CAAC;CACJ;AAXD,sCAWC"}
package/lib/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { AccountInterface } from "starknet";
2
- import Messenger, { Message } from "./messenger";
3
2
  import { Scope } from "./types";
4
3
  declare class Controller {
5
4
  private selector;
6
- private messenger?;
5
+ private connection?;
6
+ private keychain?;
7
7
  private scopes;
8
8
  private url;
9
9
  private loading;
@@ -14,16 +14,17 @@ declare class Controller {
14
14
  origin?: string;
15
15
  });
16
16
  ready(): Promise<boolean | undefined>;
17
- probe(): Promise<AccountInterface | undefined>;
17
+ probe(): Promise<boolean | null>;
18
18
  register(username: string, credential: {
19
19
  x: string;
20
20
  y: string;
21
21
  }): Promise<{
22
22
  address: string;
23
- }>;
23
+ deviceKey: string;
24
+ } | null | undefined>;
24
25
  connect(): Promise<AccountInterface | null>;
26
+ disconnect(): Promise<void | null>;
25
27
  }
26
28
  export default Controller;
27
- export type { Message };
28
- export { Messenger };
29
29
  export * from "./types";
30
+ export * from "./errors";
package/lib/index.js CHANGED
@@ -26,12 +26,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.Messenger = void 0;
30
- const cuid_1 = __importDefault(require("cuid"));
31
29
  const query_string_1 = __importDefault(require("query-string"));
30
+ const penpal_1 = require("@cartridge/penpal");
32
31
  const account_1 = __importDefault(require("./account"));
33
- const messenger_1 = __importDefault(require("./messenger"));
34
- exports.Messenger = messenger_1.default;
35
32
  class Controller {
36
33
  constructor(scopes, options) {
37
34
  this.selector = "cartridge-messenger";
@@ -44,87 +41,85 @@ class Controller {
44
41
  if (options === null || options === void 0 ? void 0 : options.url) {
45
42
  this.url = options.url;
46
43
  }
47
- if (typeof document !== "undefined") {
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
- }));
57
- });
44
+ if (typeof document === "undefined") {
45
+ console.log("no doc");
46
+ return;
58
47
  }
59
- if (typeof document !== "undefined" && !this.messenger) {
60
- let iframe = document.getElementById(this.selector);
61
- if (!!iframe) {
62
- if (!this.messenger) {
63
- this.messenger = new messenger_1.default(iframe.contentWindow, this.url);
64
- }
65
- }
66
- else {
67
- iframe = document.createElement("iframe");
68
- iframe.id = this.selector;
69
- iframe.src = `${this.url}`;
70
- iframe.style.opacity = "0";
71
- iframe.style.height = "0";
72
- iframe.style.width = "0";
73
- iframe.sandbox.add("allow-scripts");
74
- iframe.sandbox.add("allow-same-origin");
75
- if (!!document.hasStorageAccess) {
76
- iframe.sandbox.add("allow-storage-access-by-user-activation");
77
- }
48
+ const iframe = document.createElement("iframe");
49
+ iframe.id = this.selector;
50
+ iframe.src = this.url;
51
+ iframe.style.opacity = "0";
52
+ iframe.style.height = "0";
53
+ iframe.style.width = "0";
54
+ iframe.sandbox.add("allow-scripts");
55
+ iframe.sandbox.add("allow-same-origin");
56
+ if (!!document.hasStorageAccess) {
57
+ iframe.sandbox.add("allow-storage-access-by-user-activation");
58
+ }
59
+ if (document.readyState === 'complete' ||
60
+ document.readyState === 'interactive') {
61
+ document.body.appendChild(iframe);
62
+ }
63
+ else {
64
+ document.addEventListener('DOMContentLoaded', () => {
78
65
  document.body.appendChild(iframe);
79
- this.messenger = new messenger_1.default(iframe.contentWindow, this.url);
80
- }
66
+ });
81
67
  }
68
+ this.connection = (0, penpal_1.connectToChild)({
69
+ iframe,
70
+ debug: true,
71
+ });
72
+ this.connection.promise.then((keychain) => this.keychain = keychain).then(() => this.probe());
82
73
  }
83
74
  ready() {
75
+ var _a;
84
76
  return __awaiter(this, void 0, void 0, function* () {
85
- if (!this.loading)
86
- return Promise.resolve(true);
87
- return this.ready_;
77
+ return (_a = this.connection) === null || _a === void 0 ? void 0 : _a.promise.then(() => this.probe()).then((res) => !!res, () => false);
88
78
  });
89
79
  }
90
80
  probe() {
91
- var _a, _b;
92
81
  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, {
82
+ if (!this.keychain) {
83
+ console.error("not ready for connect");
84
+ return null;
85
+ }
86
+ try {
87
+ const { address, scopes } = yield this.keychain.probe();
88
+ this.account = new account_1.default(address, scopes, this.keychain, {
98
89
  url: this.url,
99
90
  });
100
- return this.account;
101
91
  }
92
+ catch (e) {
93
+ console.error(e);
94
+ }
95
+ return !!this.account;
102
96
  });
103
97
  }
104
98
  // Register a new device key.
105
99
  register(username, credential) {
106
- var _a;
107
100
  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
- username,
112
- credential
113
- }
114
- }));
115
- if (!register || register.error) {
116
- throw new Error("registration error");
101
+ if (!this.keychain) {
102
+ console.error("not ready for connect");
103
+ return null;
104
+ }
105
+ try {
106
+ return yield this.keychain.register(username, credential);
107
+ }
108
+ catch (e) {
109
+ console.error(e);
117
110
  }
118
- return register.result;
119
111
  });
120
112
  }
121
113
  connect() {
122
- var _a;
123
114
  return __awaiter(this, void 0, void 0, function* () {
124
- const id = (0, cuid_1.default)();
125
115
  if (this.account) {
126
116
  return this.account;
127
117
  }
118
+ if (!this.keychain) {
119
+ console.error("not ready for connect");
120
+ return null;
121
+ }
122
+ console.log("connect");
128
123
  if (!!document.hasStorageAccess) {
129
124
  const ok = yield document.hasStorageAccess();
130
125
  if (!ok) {
@@ -132,28 +127,33 @@ class Controller {
132
127
  }
133
128
  }
134
129
  window.open(`${this.url}/connect?${query_string_1.default.stringify({
135
- id,
136
130
  origin: window.origin,
137
131
  scopes: JSON.stringify(this.scopes),
138
132
  })}`, "_blank", "height=650,width=400");
139
- const response = yield ((_a = this.messenger) === null || _a === void 0 ? void 0 : _a.send({
140
- method: "connect",
141
- params: {
142
- id,
143
- scopes: this.scopes,
144
- },
145
- }));
146
- if (!this.messenger || !response || response.error || !response.result) {
147
- console.error("not ready for connect");
148
- return null;
149
- }
150
- this.account = new account_1.default(response.result.address, response.result.scopes, this.messenger, {
133
+ const response = yield this.keychain.connect(this.scopes);
134
+ this.account = new account_1.default(response.address, response.scopes, this.keychain, {
151
135
  url: this.url,
152
136
  });
153
137
  return this.account;
154
138
  });
155
139
  }
140
+ disconnect() {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ if (!this.keychain) {
143
+ console.error("not ready for connect");
144
+ return null;
145
+ }
146
+ if (!!document.hasStorageAccess) {
147
+ const ok = yield document.hasStorageAccess();
148
+ if (!ok) {
149
+ yield document.requestStorageAccess();
150
+ }
151
+ }
152
+ return yield this.keychain.disconnect();
153
+ });
154
+ }
156
155
  }
157
156
  exports.default = Controller;
158
157
  __exportStar(require("./types"), exports);
158
+ __exportStar(require("./errors"), exports);
159
159
  //# 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAwB;AACxB,gEAA8B;AAI9B,wDAAgC;AAChC,4DAAiD;AAiKxC,oBAjKF,mBAAS,CAiKE;AA9JlB,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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,SAAS,CAAC;gBAClC,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC;aACpC,CAAC,EAAE,EACJ,QAAQ,EACR,sBAAsB,CACvB,CAAC;YAEF,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAE1D,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAO,CACxB,QAAQ,CAAC,OAAO,EAChB,QAAQ,CAAC,MAAM,EACf,IAAI,CAAC,QAAQ,EACb;gBACE,GAAG,EAAE,IAAI,CAAC,GAAG;aACd,CACF,CAAC;YAEF,OAAO,IAAI,CAAC,OAAO,CAAC;QACtB,CAAC;KAAA;IAEK,UAAU;;YACd,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;gBAClB,OAAO,CAAC,KAAK,CAAC,uBAAuB,CAAC,CAAA;gBACtC,OAAO,IAAI,CAAC;aACb;YAED,IAAI,CAAC,CAAC,QAAQ,CAAC,gBAAgB,EAAE;gBAC/B,MAAM,EAAE,GAAG,MAAM,QAAQ,CAAC,gBAAgB,EAAE,CAAA;gBAC5C,IAAI,CAAC,EAAE,EAAE;oBACP,MAAM,QAAQ,CAAC,oBAAoB,EAAE,CAAA;iBACtC;aACF;YAED,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC;QAC1C,CAAC;KAAA;CACF;AAED,kBAAe,UAAU,CAAC;AAC1B,0CAAwB;AACxB,2CAAyB"}
@@ -0,0 +1,39 @@
1
+ import { Abi, InvocationsSignerDetails, SignerInterface, Signature, typedData, Call } from "starknet";
2
+ import { Keychain } from "./types";
3
+ import { AsyncMethodReturns } from "@cartridge/penpal";
4
+ export declare class Signer implements SignerInterface {
5
+ private keychain;
6
+ private url;
7
+ constructor(keychain: AsyncMethodReturns<Keychain>, options?: {
8
+ url?: string;
9
+ });
10
+ /**
11
+ * Method to get the public key of the signer
12
+ *
13
+ * @returns public key of signer as hex string with 0x prefix
14
+ */
15
+ getPubKey(): Promise<string>;
16
+ /**
17
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
18
+ * This adds a message prefix so it cant be interchanged with transactions
19
+ *
20
+ * @param typedData - JSON object to be signed
21
+ * @param accountAddress - account
22
+ * @returns the signature of the JSON object
23
+ * @throws {Error} if the JSON object is not a valid JSON
24
+ */
25
+ signMessage(typedData: typedData.TypedData, account: string): Promise<Signature>;
26
+ /**
27
+ * Signs a transaction with the starknet private key and returns the signature
28
+ *
29
+ * @param invocation the invocation object containing:
30
+ * - contractAddress - the address of the contract
31
+ * - entrypoint - the entrypoint of the contract
32
+ * - calldata - (defaults to []) the calldata
33
+ * - signature - (defaults to []) the signature
34
+ * @param abi (optional) the abi of the contract for better displaying
35
+ *
36
+ * @returns signature
37
+ */
38
+ signTransaction(calls: Call[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
39
+ }
package/lib/signer.js CHANGED
@@ -16,9 +16,9 @@ exports.Signer = void 0;
16
16
  const query_string_1 = __importDefault(require("query-string"));
17
17
  const cuid_1 = __importDefault(require("cuid"));
18
18
  class Signer {
19
- constructor(messenger, options) {
19
+ constructor(keychain, options) {
20
20
  this.url = "https://cartridge.gg";
21
- this.messenger = messenger;
21
+ this.keychain = keychain;
22
22
  if (options === null || options === void 0 ? void 0 : options.url) {
23
23
  this.url = options.url;
24
24
  }
@@ -48,18 +48,7 @@ class Signer {
48
48
  origin: window.origin,
49
49
  message: JSON.stringify(typedData.message),
50
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
- },
58
- });
59
- if (response.error) {
60
- throw new Error(response.error);
61
- }
62
- return response.result;
51
+ return this.keychain.signMessage(typedData, account);
63
52
  });
64
53
  }
65
54
  /**
@@ -74,28 +63,13 @@ class Signer {
74
63
  *
75
64
  * @returns signature
76
65
  */
77
- signTransaction(transactions, transactionsDetail, abis) {
66
+ signTransaction(calls, transactionsDetail, abis) {
78
67
  return __awaiter(this, void 0, void 0, function* () {
79
- const id = (0, cuid_1.default)();
80
- const calls = Array.isArray(transactions) ? transactions : [transactions];
81
68
  window.open(`${this.url}/sign?${query_string_1.default.stringify({
82
- id,
83
69
  origin: window.origin,
84
70
  calls: JSON.stringify(calls),
85
71
  })}`, "_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
- },
94
- });
95
- if (response.error) {
96
- throw new Error(response.error);
97
- }
98
- return response.result;
72
+ return this.keychain.signTransaction(calls, transactionsDetail, abis);
99
73
  });
100
74
  }
101
75
  }
package/lib/signer.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"signer.js","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAQA,gEAA8B;AAC9B,gDAAwB;AAQxB,MAAa,MAAM;IAIf,YAAY,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,SAAS;QACZ,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;IAC9B,CAAC;IAED;;;;;;;;OAQG;IACU,WAAW,CAAC,SAA8B,EAAE,OAAe;;YACpE,MAAM,EAAE,GAAG,IAAA,cAAI,GAAE,CAAA;YAEjB,MAAM,CAAC,IAAI,CACP,GAAG,IAAI,CAAC,GAAG,SAAS,sBAAE,CAAC,SAAS,CAAC;gBAC7B,EAAE;gBACF,MAAM,EAAE,MAAM,C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AAE,MAAM,CAAC,MAAM;gBACrB,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;aAC/B,CAAC,EAAE,EACJ,QAAQ,EACR,sBAAsB,CACzB,CAAC;YAEF,OAAO,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,KAAK,EAAE,kBAAkB,EAAE,IAAI,CAAC,CAAC;QAC1E,CAAC;KAAA;CACJ;AA5ED,wBA4EC"}
package/lib/types.d.ts CHANGED
@@ -1,129 +1,32 @@
1
- import { DeployContractPayload, DeployContractResponse as StarknetDeployContractResponse, Abi, Call, InvocationsDetails, typedData, InvokeFunctionResponse, Signature } from "starknet";
1
+ import { Abi, Call, InvocationsDetails, typedData, InvokeFunctionResponse, Signature, InvocationsSignerDetails, EstimateFeeDetails, EstimateFee } from "starknet";
2
2
  import { BigNumberish } from "starknet/dist/utils/number";
3
- import { BlockIdentifier } from "starknet/provider/utils";
4
- import { EstimateFee } from "starknet/types/account";
5
3
  export declare type Approvals = {
6
- [origin: string]: {
7
- scopes: Scope[];
8
- maxFee: BigNumberish;
9
- };
4
+ scopes: Scope[];
5
+ maxFee: BigNumberish;
10
6
  };
11
7
  export declare type Scope = {
12
8
  target: string;
13
9
  method?: string;
14
10
  };
15
- export interface ProbeRequest extends RawRequest {
16
- method: "probe";
17
- }
18
- export interface ProbeResponse extends RawResponse {
19
- method: "probe";
20
- result?: {
21
- address?: string;
22
- scopes?: Scope[];
23
- };
24
- }
25
- export interface ConnectRequest extends RawRequest {
26
- method: "connect";
27
- params: {
28
- id: string;
11
+ export interface Keychain {
12
+ probe(): {
13
+ address: string;
29
14
  scopes: Scope[];
30
15
  };
31
- }
32
- export interface ConnectResponse extends RawResponse {
33
- method: "connect";
34
- result?: {
35
- success: boolean;
36
- address?: string;
16
+ connect(scopes: Scope[]): {
17
+ address: string;
37
18
  scopes: Scope[];
38
19
  };
39
- }
40
- export interface DeployContractRequest extends RawRequest {
41
- method: "deploy-contract";
42
- params: {
43
- id: string;
44
- payload: DeployContractPayload;
45
- abi?: Abi;
46
- };
47
- }
48
- export interface DeployContractResponse extends RawResponse {
49
- method: "deploy-contract";
50
- result?: StarknetDeployContractResponse;
51
- }
52
- export interface EstimateFeeRequest extends RawRequest {
53
- method: "estimate-fee";
54
- params: {
55
- calls: Call | Call[];
56
- nonce: BigNumberish;
57
- blockIdentifier?: BlockIdentifier;
58
- };
59
- }
60
- export interface EstimateFeeResponse extends RawResponse {
61
- method: "estimate-fee";
62
- result?: EstimateFee;
63
- }
64
- export interface ExecuteRequest extends RawRequest {
65
- method: "execute";
66
- params: {
67
- id?: string;
68
- calls: Call | Call[];
69
- abis?: Abi[];
70
- transactionsDetail?: InvocationsDetails;
71
- };
72
- }
73
- export interface ExecuteResponse extends RawResponse {
74
- method: "execute";
75
- result?: InvokeFunctionResponse;
76
- scopes?: Scope[];
77
- }
78
- export interface SignTransactionRequest extends RawRequest {
79
- method: "sign-transaction";
80
- params: {
81
- id: string;
82
- calls: Call | Call[];
83
- abis?: Abi[];
84
- transactionsDetail?: InvocationsDetails;
85
- };
86
- }
87
- export interface SignTransactionResponse extends RawResponse {
88
- method: "sign-transaction";
89
- result?: Signature;
90
- }
91
- export interface SignMessageRequest extends RawRequest {
92
- method: "sign-message";
93
- params: {
94
- id: string;
95
- account?: string;
96
- typedData: typedData.TypedData;
97
- };
98
- }
99
- export interface SignMessageResponse extends RawResponse {
100
- method: "sign-message";
101
- result?: Signature;
102
- }
103
- export interface RegisterRequest extends RawRequest {
104
- method: "register";
105
- params: {
106
- username: string;
107
- credential: {
108
- x: string;
109
- y: string;
110
- };
111
- };
112
- }
113
- export interface RegisterResponse extends RawResponse {
114
- method: "register";
115
- result: {
20
+ disconnect(): void;
21
+ estimateFee(calls: Call | Call[], estimateFeeDetails?: EstimateFeeDetails): Promise<EstimateFee>;
22
+ execute(calls: Call | Call[], abis?: Abi[], transactionsDetail?: InvocationsDetails, sync?: boolean): Promise<InvokeFunctionResponse>;
23
+ register(username: string, credential: {
24
+ x: string;
25
+ y: string;
26
+ }): Promise<{
116
27
  address: string;
117
- };
28
+ deviceKey: string;
29
+ }>;
30
+ signMessage(typedData: typedData.TypedData, account: string): Promise<Signature>;
31
+ signTransaction(transactions: Call[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
118
32
  }
119
- export declare type RawRequest = {
120
- origin?: string;
121
- method: string;
122
- params?: object | any[];
123
- };
124
- export declare type Request = RawRequest | ProbeRequest | ConnectRequest | DeployContractRequest | EstimateFeeRequest | ExecuteRequest | SignMessageRequest | RegisterRequest;
125
- export declare type RawResponse = {
126
- result?: any;
127
- error?: unknown;
128
- };
129
- export declare type Response = RawResponse | ProbeResponse | DeployContractResponse | EstimateFeeResponse | ConnectResponse | SignMessageResponse | RegisterResponse;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cartridge/controller",
3
- "version": "0.1.51",
3
+ "version": "0.1.53",
4
4
  "description": "Cartridge Controller",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -22,10 +22,11 @@
22
22
  "typescript": "4.8.2"
23
23
  },
24
24
  "dependencies": {
25
+ "@cartridge/penpal": "^6.2.3",
25
26
  "base64url": "^3.0.1",
26
27
  "cuid": "^2.1.8",
27
28
  "fast-deep-equal": "^3.1.3",
28
29
  "query-string": "^7.1.1",
29
- "starknet": "^4.3.0"
30
+ "starknet": "^4.7.0"
30
31
  }
31
32
  }