forked from ton-blockchain/wallet-contract
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwallet-v5.ts
199 lines (178 loc) · 8.94 KB
/
wallet-v5.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { SandboxContract } from "@ton-community/sandbox";
import { Address, beginCell, Cell, Contract, contractAddress, ContractProvider, internal, MessageRelaxed, Sender, SenderArguments, SendMode, storeMessageRelaxed, toNano } from "ton-core";
import { sign } from "ton-crypto";
export type Transfer = {action: 'transfer', message: MessageRelaxed, sendMode: SendMode};
export type UninstallPlugin = {action: 'uninstall', hash: Buffer};
export type InstallPlugin = {action: 'install', code: Cell};
export type InvokeCode = {action: 'invoke', code: Cell};
export type Action = InstallPlugin | InvokeCode | Transfer | UninstallPlugin;
export class WalletContractV5R2 implements Contract {
readonly workchain: number;
readonly publicKey: Buffer;
readonly address: Address;
readonly walletId: number;
readonly init: { data: Cell, code: Cell };
static create(args: {workchain: number, publicKey: Buffer, walletId?: number}) {
let {workchain, publicKey, walletId} = args;
return new WalletContractV5R2(workchain, publicKey, walletId);
}
private constructor(workchain: number, publicKey: Buffer, walletId?: number) {
this.workchain = workchain;
this.publicKey = publicKey;
if (walletId !== undefined) {
this.walletId = walletId;
} else {
this.walletId = 698983191 + workchain;
}
let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APQYQAPLtRNDTP/QE9AQB+GHTHwTT/1MTgwf0D/LhLCL4QYMH9A8wAdDtHlRDA28DggFdVm+MaKWl+H6BAP9vhCCkYQF/2zho+F6hb4BvECBumjD4QYMH9Fsw+GGZ+EESgwf0F/hh4lUC+EEEyMs/E/QAE/QAEssfAc8Wye1UAv4x7UTQ0z/0BPQEAfhh0x9UNBQC1CH5ACOCCIDeT7qbMwPXC/8T+RDy4TaOGAOCEGTd6+u6l1AD+RTy4TaWE18D8sE34uLQ0z9RFLry4TjTHwH4I77y4TkjpFRzJfhBBMjLPxP0ABP0ABLLHwHPFsntVPgP+AD0BZMgbrOK6DACHh8BCFjbPFkgADCkUCP4QQTIyz8T9AAT9AASyx8BzxbJ7VQC9gHQ0x8hghA+6UPxupkx1CDXCwcS+wCO4iGCCfq/9rqOFzEg1wv/ElICgwf0WzD4QRKDB/RbMPhhjr4hghB5ri2fuo4yAYIQaQjJlLqOItQB0O0ebwBwb4xopaX4foEA/2+EIKRhAX/bOGj4XqFvgDCT8sEv4gHjDeIB4iEiAPwx1CH5AFRCFIMH9BeCEEP/1E+AGMjLBY0IZ/8olf9ITD54aEoNbXf085JIgV+/WI1hdRU7aZ/xCT+UxM8WghAJiWgAJIIJ4TOAAYIBhqD5QTBYcIAS+DOAIPQMb6EwgQCo1yHTPwOoAtM/MFADqKABqKsPoPoCy4oSzMly+wAABvQFAQ==');
let data = beginCell()
.storeUint(this.walletId, 32) // subwallet_id -\ uid
.storeUint(0, 32) // seqno _/
.storeUint(0, 2) // plugins code and data
.storeUint(0x0080de4f, 32) // key::public::ed25519
.storeBuffer(this.publicKey)
.endCell();
this.init = { code, data };
this.address = contractAddress(workchain, { code, data });
}
async getBalance(provider: ContractProvider) {
let state = await provider.getState();
return state.balance;
}
async _nowrapGetSeqno(provider: ContractProvider): Promise<bigint> {
let state = await provider.getState();
if (state.state.type === 'active') {
let res = await provider.get('seqno', []);
return res.stack.readBigNumber();
} else {
return BigInt(this.walletId) * 4294967296n + 0n;
}
}
async getSeqno(provider: ContractProvider): Promise<bigint> {
return this._nowrapGetSeqno(provider);
}
async sendExternal(provider: ContractProvider, message: Cell) {
await provider.external(message);
}
async sendTransfer(provider: ContractProvider, args: {
seqno: bigint,
secretKey: Buffer,
messages: MessageRelaxed[]
sendMode?: SendMode,
timeout?: number,
}) {
let transfer = this.createTransfer(args);
await this.sendExternal(provider, transfer);
}
_storeAction(nextAction: Cell | null, add: Action): Cell | null {
if (add.action == 'install') {
return beginCell()
.storeUint(0x79ae2d9f, 32)
.storeRef(add.code)
.storeMaybeRef(nextAction)
.endCell();
} else if (add.action == 'transfer') {
return beginCell()
.storeUint(0x3ee943f1, 32)
.storeUint(add.sendMode | SendMode.IGNORE_ERRORS, 8)
.storeRef(beginCell().store(storeMessageRelaxed(add.message)))
.storeMaybeRef(nextAction)
.endCell();
} else if (add.action == 'invoke') {
return beginCell()
.storeUint(0x6908c994, 32)
.storeRef(add.code)
.storeMaybeRef(nextAction)
.endCell();
} else if (add.action == 'uninstall') {
return beginCell()
.storeUint(0x01fabff6, 32)
.storeBuffer(add.hash)
.storeMaybeRef(nextAction)
.endCell();
} else {
throw new Error("trying to store unsupported action");
}
}
signMultiAction(args: {
seqno: bigint,
secretKey: Buffer,
actions: Action[],
}): Cell {
let {seqno, secretKey, actions} = args;
if (actions.length > 255) {
throw Error("Maximum number of messages in a single transfer is 255");
}
actions.reverse();
let lastActionRepr: Cell | null = null;
for (let a of actions) {
lastActionRepr = this._storeAction(lastActionRepr, a);
}
const deadline = Math.floor(Date.now() / 1000) + 60;
const request = beginCell().storeUint(seqno, 64).storeUint(deadline, 32).storeMaybeRef(lastActionRepr).endCell();
const signature: Buffer = sign(request.hash(), secretKey);
const body = beginCell().storeUint(0x142c7d04, 32).storeBuffer(signature).storeRef(request).endCell();
return body;
}
createTransfer(args: {
seqno: bigint,
secretKey: Buffer,
messages: MessageRelaxed[],
sendMode?: SendMode | null,
}): Cell {
let {seqno, secretKey, messages, sendMode} = args;
let fixedSendMode : SendMode = (sendMode ?? SendMode.PAY_GAS_SEPARATELY) | SendMode.IGNORE_ERRORS;
const actions: Action[] = messages.map(
m => {return {action: 'transfer', message: m, sendMode: fixedSendMode};}
);
return this.signMultiAction({seqno, actions, secretKey});
}
async sendInstallPlugin(provider: ContractProvider, args: {
seqno?: bigint,
secretKey: Buffer,
code: Cell
}): Promise<Buffer> {
let transfer = this.signMultiAction({
seqno: args.seqno ?? await this._nowrapGetSeqno(provider),
secretKey: args.secretKey,
actions: [{action: 'install', code: args.code}]
})
await this.sendExternal(provider, transfer);
return args.code.hash();
}
async sendUninstallPlugin(provider: ContractProvider, args: {
seqno?: bigint,
secretKey: Buffer,
hash?: Buffer,
code?: Cell
}) {
let hash: Buffer = args.hash ?? args.code!!.hash();
let transfer = this.signMultiAction({
seqno: args.seqno ?? await this._nowrapGetSeqno(provider),
secretKey: args.secretKey,
actions: [{action: 'uninstall', hash}]
})
await this.sendExternal(provider, transfer);
}
async sendDeploy(provider: ContractProvider, via: Sender, value?: string) {
await provider.internal(via, {
value: toNano(value ?? '0.01'),
bounce: false,
sendMode: SendMode.PAY_GAS_SEPARATELY
});
}
}
export function makeSender(contract: SandboxContract<WalletContractV5R2>, secretKey: Buffer) : Sender {
return {
send: async (args: SenderArguments) => {
let seqno = await contract.getSeqno();
let transfer = contract.createTransfer({
seqno,
secretKey,
sendMode: args.sendMode,
messages: [internal(args)]
});
await contract.sendExternal(transfer);
}
};
}