@cartridge/controller 0.4.0 → 0.5.0-alpha.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.
- package/.turbo/turbo-build$colon$deps.log +5 -4
- package/.turbo/turbo-format.log +25 -0
- package/dist/account.d.ts +35 -0
- package/dist/account.js +76 -0
- package/dist/account.js.map +1 -0
- package/dist/constants.d.ts +0 -2
- package/dist/constants.js +0 -2
- package/dist/constants.js.map +1 -1
- package/dist/controller.d.ts +7 -14
- package/dist/controller.js +41 -53
- package/dist/controller.js.map +1 -1
- package/dist/errors.d.ts +0 -3
- package/dist/errors.js +0 -6
- package/dist/errors.js.map +1 -1
- package/dist/icon.d.ts +1 -0
- package/dist/icon.js +2 -0
- package/dist/icon.js.map +1 -0
- package/dist/iframe/base.d.ts +0 -1
- package/dist/iframe/base.js +21 -11
- package/dist/iframe/base.js.map +1 -1
- package/dist/iframe/keychain.d.ts +1 -1
- package/dist/iframe/keychain.js +1 -4
- package/dist/iframe/keychain.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +18 -0
- package/dist/provider.js +128 -0
- package/dist/provider.js.map +1 -0
- package/dist/session/account.d.ts +32 -0
- package/dist/session/account.js +31 -0
- package/dist/session/account.js.map +1 -0
- package/dist/session/backend.d.ts +58 -0
- package/dist/session/backend.js +38 -0
- package/dist/session/backend.js.map +1 -0
- package/dist/session/index.d.ts +5 -0
- package/dist/session/index.js +6 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/provider.d.ts +25 -0
- package/dist/session/provider.js +89 -0
- package/dist/session/provider.js.map +1 -0
- package/dist/telegram/backend.d.ts +30 -0
- package/dist/telegram/backend.js +39 -0
- package/dist/telegram/backend.js.map +1 -0
- package/dist/telegram/provider.d.ts +19 -0
- package/dist/telegram/provider.js +78 -0
- package/dist/telegram/provider.js.map +1 -0
- package/dist/types.d.ts +11 -29
- package/dist/types.js.map +1 -1
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -4
- package/dist/utils.js.map +1 -1
- package/package.json +13 -10
- package/src/{device.ts → account.ts} +26 -62
- package/src/constants.ts +0 -2
- package/src/controller.ts +50 -76
- package/src/errors.ts +0 -8
- package/src/icon.ts +2 -0
- package/src/iframe/base.ts +27 -12
- package/src/iframe/keychain.ts +2 -12
- package/src/index.ts +5 -0
- package/src/provider.ts +181 -0
- package/src/session/account.ts +65 -0
- package/src/session/backend.ts +73 -0
- package/src/session/index.ts +6 -0
- package/src/session/provider.ts +141 -0
- package/src/telegram/backend.ts +43 -0
- package/src/telegram/provider.ts +131 -0
- package/src/types.ts +22 -56
- package/src/utils.ts +0 -10
- package/tsconfig.tsbuildinfo +1 -1
- package/src/session.ts +0 -92
- package/src/signer.ts +0 -144
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cloudStorage,
|
|
3
|
+
miniApp,
|
|
4
|
+
openLink,
|
|
5
|
+
retrieveLaunchParams,
|
|
6
|
+
} from "@telegram-apps/sdk";
|
|
7
|
+
import { ec, stark, WalletAccount } from "starknet";
|
|
8
|
+
|
|
9
|
+
import { KEYCHAIN_URL } from "src/constants";
|
|
10
|
+
import { Policy } from "src/types";
|
|
11
|
+
import SessionAccount from "src/session/account";
|
|
12
|
+
import BaseProvider from "src/provider";
|
|
13
|
+
|
|
14
|
+
interface SessionRegistration {
|
|
15
|
+
username: string;
|
|
16
|
+
address: string;
|
|
17
|
+
ownerGuid: string;
|
|
18
|
+
transactionHash?: string;
|
|
19
|
+
expiresAt: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default class TelegramProvider extends BaseProvider {
|
|
23
|
+
private _tmaUrl: string;
|
|
24
|
+
protected _chainId: string;
|
|
25
|
+
protected _username?: string;
|
|
26
|
+
protected _policies: Policy[];
|
|
27
|
+
|
|
28
|
+
constructor({
|
|
29
|
+
rpc,
|
|
30
|
+
chainId,
|
|
31
|
+
policies,
|
|
32
|
+
tmaUrl,
|
|
33
|
+
}: {
|
|
34
|
+
rpc: string;
|
|
35
|
+
chainId: string;
|
|
36
|
+
policies: Policy[];
|
|
37
|
+
tmaUrl: string;
|
|
38
|
+
}) {
|
|
39
|
+
super({
|
|
40
|
+
rpc,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
this._tmaUrl = tmaUrl;
|
|
44
|
+
this._chainId = chainId;
|
|
45
|
+
this._policies = policies;
|
|
46
|
+
|
|
47
|
+
if (typeof window !== "undefined") {
|
|
48
|
+
(window as any).starknet_controller = this;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async probe(): Promise<WalletAccount | undefined> {
|
|
53
|
+
await this.tryRetrieveFromQueryOrStorage();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async connect(): Promise<WalletAccount | undefined> {
|
|
58
|
+
await this.tryRetrieveFromQueryOrStorage();
|
|
59
|
+
if (this.account) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Generate a random local key pair
|
|
64
|
+
const pk = stark.randomAddress();
|
|
65
|
+
const publicKey = ec.starkCurve.getStarkKey(pk);
|
|
66
|
+
|
|
67
|
+
cloudStorage.setItem(
|
|
68
|
+
"sessionSigner",
|
|
69
|
+
JSON.stringify({
|
|
70
|
+
privKey: pk,
|
|
71
|
+
pubKey: publicKey,
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const url = `${KEYCHAIN_URL}/session?public_key=${publicKey}&redirect_uri=${
|
|
76
|
+
this._tmaUrl
|
|
77
|
+
}&redirect_query_name=startapp&policies=${JSON.stringify(
|
|
78
|
+
this._policies,
|
|
79
|
+
)}&rpc_url=${this.rpc}`;
|
|
80
|
+
|
|
81
|
+
localStorage.setItem("lastUsedConnector", this.id);
|
|
82
|
+
openLink(url);
|
|
83
|
+
miniApp.close();
|
|
84
|
+
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
disconnect(): Promise<void> {
|
|
89
|
+
cloudStorage.deleteItem("sessionSigner");
|
|
90
|
+
cloudStorage.deleteItem("session");
|
|
91
|
+
this.account = undefined;
|
|
92
|
+
this._username = undefined;
|
|
93
|
+
return Promise.resolve();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async tryRetrieveFromQueryOrStorage() {
|
|
97
|
+
const signer = JSON.parse((await cloudStorage.getItem("sessionSigner"))!);
|
|
98
|
+
let sessionRegistration: SessionRegistration | null = null;
|
|
99
|
+
|
|
100
|
+
const launchParams = retrieveLaunchParams();
|
|
101
|
+
const session = launchParams.startParam;
|
|
102
|
+
if (session) {
|
|
103
|
+
sessionRegistration = JSON.parse(atob(session));
|
|
104
|
+
cloudStorage.setItem("session", JSON.stringify(sessionRegistration));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!sessionRegistration) {
|
|
108
|
+
const session = await cloudStorage.getItem("session");
|
|
109
|
+
if (session) {
|
|
110
|
+
sessionRegistration = JSON.parse(session);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!sessionRegistration) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this._username = sessionRegistration.username;
|
|
119
|
+
this.account = new SessionAccount(this, {
|
|
120
|
+
rpcUrl: this.rpc.toString(),
|
|
121
|
+
privateKey: signer.privKey,
|
|
122
|
+
address: sessionRegistration.address,
|
|
123
|
+
ownerGuid: sessionRegistration.ownerGuid,
|
|
124
|
+
chainId: this._chainId,
|
|
125
|
+
expiresAt: parseInt(sessionRegistration.expiresAt),
|
|
126
|
+
policies: this._policies,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
return this.account;
|
|
130
|
+
}
|
|
131
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
constants,
|
|
3
|
-
|
|
3
|
+
BigNumberish,
|
|
4
4
|
Call,
|
|
5
|
+
Abi,
|
|
5
6
|
InvocationsDetails,
|
|
6
|
-
TypedData,
|
|
7
|
-
InvokeFunctionResponse,
|
|
8
|
-
Signature,
|
|
9
|
-
EstimateFeeDetails,
|
|
10
|
-
EstimateFee,
|
|
11
|
-
DeclareContractPayload,
|
|
12
|
-
BigNumberish,
|
|
13
|
-
InvocationsSignerDetails,
|
|
14
|
-
DeployAccountSignerDetails,
|
|
15
|
-
DeclareSignerDetails,
|
|
16
7
|
} from "starknet";
|
|
8
|
+
import {
|
|
9
|
+
AddInvokeTransactionResult,
|
|
10
|
+
Signature,
|
|
11
|
+
TypedData,
|
|
12
|
+
} from "@starknet-io/types-js";
|
|
17
13
|
import { KeychainIFrame, ProfileIFrame } from "./iframe";
|
|
18
14
|
import wasm from "@cartridge/account-wasm/controller";
|
|
19
15
|
|
|
@@ -59,7 +55,7 @@ export type ConnectReply = {
|
|
|
59
55
|
};
|
|
60
56
|
|
|
61
57
|
export type ExecuteReply =
|
|
62
|
-
| (
|
|
58
|
+
| (AddInvokeTransactionResult & {
|
|
63
59
|
code: ResponseCodes.SUCCESS;
|
|
64
60
|
})
|
|
65
61
|
| {
|
|
@@ -97,46 +93,28 @@ export interface Keychain {
|
|
|
97
93
|
revoke(origin: string): void;
|
|
98
94
|
|
|
99
95
|
deploy(): Promise<DeployReply | ConnectError>;
|
|
100
|
-
estimateDeclareFee(
|
|
101
|
-
payload: DeclareContractPayload,
|
|
102
|
-
details?: EstimateFeeDetails,
|
|
103
|
-
): Promise<EstimateFee>;
|
|
104
|
-
estimateInvokeFee(
|
|
105
|
-
calls: Call | Call[],
|
|
106
|
-
estimateFeeDetails?: EstimateFeeDetails,
|
|
107
|
-
): Promise<EstimateFee>;
|
|
108
96
|
execute(
|
|
109
97
|
calls: Call | Call[],
|
|
110
98
|
abis?: Abi[],
|
|
111
99
|
transactionsDetail?: InvocationsDetails,
|
|
112
100
|
sync?: boolean,
|
|
113
|
-
paymaster?:
|
|
101
|
+
paymaster?: any,
|
|
114
102
|
error?: ControllerError,
|
|
115
103
|
): Promise<ExecuteReply | ConnectError>;
|
|
104
|
+
signMessage(
|
|
105
|
+
typedData: TypedData,
|
|
106
|
+
account: string,
|
|
107
|
+
): Promise<Signature | ConnectError>;
|
|
116
108
|
logout(): Promise<void>;
|
|
117
109
|
openSettings(): Promise<void | ConnectError>;
|
|
118
110
|
session(): Promise<Session>;
|
|
119
111
|
sessions(): Promise<{
|
|
120
112
|
[key: string]: Session;
|
|
121
113
|
}>;
|
|
122
|
-
signMessage(
|
|
123
|
-
typedData: TypedData,
|
|
124
|
-
account: string,
|
|
125
|
-
): Promise<Signature | ConnectError>;
|
|
126
|
-
signTransaction(
|
|
127
|
-
transactions: Call[],
|
|
128
|
-
transactionsDetail: InvocationsSignerDetails,
|
|
129
|
-
abis?: Abi[],
|
|
130
|
-
): Promise<Signature>;
|
|
131
|
-
signDeployAccountTransaction(
|
|
132
|
-
transaction: DeployAccountSignerDetails,
|
|
133
|
-
): Promise<Signature>;
|
|
134
|
-
signDeclareTransaction(transaction: DeclareSignerDetails): Promise<Signature>;
|
|
135
114
|
delegateAccount(): string;
|
|
136
115
|
username(): string;
|
|
137
116
|
fetchControllers(contractAddresses: string[]): Promise<ControllerAccounts>;
|
|
138
117
|
}
|
|
139
|
-
|
|
140
118
|
export interface Profile {
|
|
141
119
|
navigate(tab: ProfileContextTypeVariant): void;
|
|
142
120
|
}
|
|
@@ -149,7 +127,9 @@ export interface Modal {
|
|
|
149
127
|
/**
|
|
150
128
|
* Options for configuring the controller
|
|
151
129
|
*/
|
|
152
|
-
export type ControllerOptions =
|
|
130
|
+
export type ControllerOptions = ProviderOptions &
|
|
131
|
+
KeychainOptions &
|
|
132
|
+
ProfileOptions;
|
|
153
133
|
|
|
154
134
|
export type TokenOptions = {
|
|
155
135
|
tokens: Tokens;
|
|
@@ -169,16 +149,17 @@ export type IFrameOptions = {
|
|
|
169
149
|
};
|
|
170
150
|
};
|
|
171
151
|
|
|
152
|
+
export type ProviderOptions = {
|
|
153
|
+
/** The URL of the RPC */
|
|
154
|
+
rpc: string;
|
|
155
|
+
};
|
|
156
|
+
|
|
172
157
|
export type KeychainOptions = IFrameOptions & {
|
|
173
158
|
policies?: Policy[];
|
|
174
159
|
/** The URL of keychain */
|
|
175
160
|
url?: string;
|
|
176
|
-
/** The URL of the RPC */
|
|
177
|
-
rpc?: string;
|
|
178
161
|
/** The origin of keychain */
|
|
179
162
|
origin?: string;
|
|
180
|
-
/** Paymaster options for transaction fee management */
|
|
181
|
-
paymaster?: PaymasterOptions;
|
|
182
163
|
/** Propagate transaction errors back to caller instead of showing modal */
|
|
183
164
|
propagateSessionErrors?: boolean;
|
|
184
165
|
};
|
|
@@ -192,22 +173,7 @@ export type ProfileOptions = IFrameOptions & {
|
|
|
192
173
|
tokens?: Tokens;
|
|
193
174
|
};
|
|
194
175
|
|
|
195
|
-
export type ProfileContextTypeVariant = "
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Options for configuring a paymaster
|
|
199
|
-
*/
|
|
200
|
-
export type PaymasterOptions = {
|
|
201
|
-
/**
|
|
202
|
-
* The address of the account paying for the transaction.
|
|
203
|
-
* This should be a valid Starknet address or "ANY_CALLER" short string.
|
|
204
|
-
*/
|
|
205
|
-
caller: string;
|
|
206
|
-
/**
|
|
207
|
-
* The URL of the paymaster. Currently not used.
|
|
208
|
-
*/
|
|
209
|
-
url?: string;
|
|
210
|
-
};
|
|
176
|
+
export type ProfileContextTypeVariant = "trophies" | "inventory" | "activity";
|
|
211
177
|
|
|
212
178
|
export type ColorMode = "light" | "dark";
|
|
213
179
|
|
package/src/utils.ts
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
import equal from "fast-deep-equal";
|
|
2
|
-
import { Policy } from "./types";
|
|
3
1
|
import { addAddressPadding, Call, CallData } from "starknet";
|
|
4
2
|
|
|
5
|
-
export function diff(a: Policy[], b: Policy[]): Policy[] {
|
|
6
|
-
return a.reduce(
|
|
7
|
-
(prev, policyA) =>
|
|
8
|
-
b.some((policyB) => equal(policyB, policyA)) ? prev : [...prev, policyA],
|
|
9
|
-
[] as Policy[],
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
3
|
export function normalizeCalls(calls: Call | Call[]) {
|
|
14
4
|
return (Array.isArray(calls) ? calls : [calls]).map((call) => {
|
|
15
5
|
return {
|