-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathoutsideExecution.ts
155 lines (145 loc) · 4.41 KB
/
outsideExecution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Call, CallData, hash, num, RawArgs, SignerInterface, typedData } from "starknet";
import { manager } from "./";
const typesRev0 = {
StarkNetDomain: [
{ name: "name", type: "felt" },
{ name: "version", type: "felt" },
{ name: "chainId", type: "felt" },
],
OutsideExecution: [
{ name: "caller", type: "felt" },
{ name: "nonce", type: "felt" },
{ name: "execute_after", type: "felt" },
{ name: "execute_before", type: "felt" },
{ name: "calls_len", type: "felt" },
{ name: "calls", type: "OutsideCall*" },
],
OutsideCall: [
{ name: "to", type: "felt" },
{ name: "selector", type: "felt" },
{ name: "calldata_len", type: "felt" },
{ name: "calldata", type: "felt*" },
],
};
const typesRev1 = {
StarknetDomain: [
{ name: "name", type: "shortstring" },
{ name: "version", type: "shortstring" },
{ name: "chainId", type: "shortstring" },
{ name: "revision", type: "shortstring" },
],
OutsideExecution: [
{ name: "Caller", type: "ContractAddress" },
{ name: "Nonce", type: "felt" },
{ name: "Execute After", type: "u128" },
{ name: "Execute Before", type: "u128" },
{ name: "Calls", type: "Call*" },
],
Call: [
{ name: "To", type: "ContractAddress" },
{ name: "Selector", type: "selector" },
{ name: "Calldata", type: "felt*" },
],
};
function getDomain(chainId: string, revision: typedData.TypedDataRevision) {
if (revision == typedData.TypedDataRevision.Active) {
// WARNING! Version and revision are encoded as numbers in the StarkNetDomain type and not as shortstring
// This is due to a bug in the Braavos implementation, and has been kept for compatibility
return {
name: "Account.execute_from_outside",
version: "2",
chainId: chainId,
revision: "1",
};
}
return {
name: "Account.execute_from_outside",
version: "1",
chainId: chainId,
};
}
export interface OutsideExecution {
caller: string;
nonce: num.BigNumberish;
execute_after: num.BigNumberish;
execute_before: num.BigNumberish;
calls: OutsideCall[];
}
export interface OutsideCall {
to: string;
selector: num.BigNumberish;
calldata: RawArgs;
}
export function getOutsideCall(call: Call): OutsideCall {
return {
to: call.contractAddress,
selector: hash.getSelectorFromName(call.entrypoint),
calldata: call.calldata ?? [],
};
}
export function getTypedDataHash(
outsideExecution: OutsideExecution,
accountAddress: num.BigNumberish,
chainId: string,
revision: typedData.TypedDataRevision,
): string {
return typedData.getMessageHash(getTypedData(outsideExecution, chainId, revision), accountAddress);
}
export function getTypedData(
outsideExecution: OutsideExecution,
chainId: string,
revision: typedData.TypedDataRevision,
) {
if (revision == typedData.TypedDataRevision.Active) {
return {
types: typesRev1,
primaryType: "OutsideExecution",
domain: getDomain(chainId, revision),
message: {
Caller: outsideExecution.caller,
Nonce: outsideExecution.nonce,
"Execute After": outsideExecution.execute_after,
"Execute Before": outsideExecution.execute_before,
Calls: outsideExecution.calls.map((call) => {
return {
To: call.to,
Selector: call.selector,
Calldata: call.calldata,
};
}),
},
};
}
return {
types: typesRev0,
primaryType: "OutsideExecution",
domain: getDomain(chainId, revision),
message: {
...outsideExecution,
calls_len: outsideExecution.calls.length,
calls: outsideExecution.calls.map((call) => {
return {
...call,
calldata_len: call.calldata.length,
calldata: call.calldata,
};
}),
},
};
}
export async function getOutsideExecutionCall(
outsideExecution: OutsideExecution,
accountAddress: string,
signer: SignerInterface,
revision: typedData.TypedDataRevision,
chainId?: string,
): Promise<Call> {
chainId = chainId ?? (await manager.getChainId());
const currentTypedData = getTypedData(outsideExecution, chainId, revision);
const signature = await signer.signMessage(currentTypedData, accountAddress);
return {
contractAddress: accountAddress,
entrypoint: revision == typedData.TypedDataRevision.Active ? "execute_from_outside_v2" : "execute_from_outside",
calldata: CallData.compile({ ...outsideExecution, signature }),
};
}