Skip to content

Commit d349886

Browse files
author
Trinketer22
committed
Test wrapper and constants
1 parent ff8cd74 commit d349886

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

wrappers/Errors.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export abstract class ErrorsV5 {
2+
static readonly signature_disabled = 132;
3+
static readonly invalid_seqno = 133;
4+
static readonly invalid_wallet_id = 134;
5+
static readonly invalid_signature = 135;
6+
static readonly expired = 136;
7+
static readonly external_send_message_must_have_ignore_errors_send_mode = 137;
8+
static readonly invalid_message_operation = 138;
9+
static readonly add_extension = 139;
10+
static readonly remove_extension = 140;
11+
static readonly unsupported_action = 141;
12+
static readonly disable_signature_when_extensions_is_empty = 142;
13+
static readonly this_signature_mode_already_set = 143;
14+
static readonly remove_last_extension_when_signature_disabled = 144;
15+
static readonly extension_wrong_workchain = 145;
16+
static readonly only_extension_can_change_signature_mode = 146;
17+
static readonly invalid_c5 = 147;
18+
}

wrappers/wallet-v5-test.ts

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import { Cell, beginCell, Sender, ContractProvider, SendMode, MessageRelaxed, Address, toNano, contractAddress, OutAction, OutActionSendMsg, Builder, storeOutList } from '@ton/core';
2+
import { WalletV5, WalletV5Config, walletV5ConfigToCell, Opcodes } from './wallet-v5';
3+
import { sign } from '@ton/crypto';
4+
5+
export type WalletActions = {
6+
wallet?: OutAction[] | Cell,
7+
extended?: ExtendedAction[] | Cell
8+
}
9+
10+
export type ExtensionAdd = {
11+
type: 'add_extension',
12+
address: Address
13+
}
14+
export type ExtensionRemove = {
15+
type: 'remove_extension',
16+
address: Address
17+
}
18+
19+
export type SetSignatureAuth = {
20+
type: 'sig_auth',
21+
allowed: boolean
22+
}
23+
24+
export type ExtendedAction = ExtensionAdd | ExtensionRemove | SetSignatureAuth;
25+
26+
export type MessageOut = {
27+
message: MessageRelaxed,
28+
mode: SendMode
29+
};
30+
31+
function storeWalletActions(actions: WalletActions) {
32+
// store compatable
33+
return (builder: Builder) => {
34+
let hasExtendedActions = false;
35+
if(actions.wallet) {
36+
let actionCell: Cell | null = null;
37+
if(actions.wallet instanceof Cell) {
38+
actionCell = actions.wallet;
39+
}
40+
else if(actions.wallet.length > 0) {
41+
actionCell = beginCell().store(storeOutList(actions.wallet)).endCell();
42+
}
43+
builder.storeMaybeRef(actionCell);
44+
}
45+
else {
46+
builder.storeBit(false);
47+
}
48+
if(actions.extended) {
49+
if(actions.extended instanceof Cell) {
50+
builder.storeBit(true);
51+
builder.storeSlice(actions.extended.asSlice());
52+
}
53+
else if(actions.extended.length > 0) {
54+
builder.storeBit(true);
55+
builder.store(storeExtendedActions(actions.extended));
56+
}
57+
else {
58+
builder.storeBit(false);
59+
}
60+
}
61+
else {
62+
builder.storeBit(false);
63+
}
64+
}
65+
}
66+
67+
function storeExtensionAction(action: ExtendedAction) {
68+
return (builder: Builder) => {
69+
if(action.type == 'add_extension') {
70+
builder.storeUint(2, 8).storeAddress(action.address);
71+
}
72+
else if(action.type == 'remove_extension') {
73+
builder.storeUint(3, 8).storeAddress(action.address);
74+
}
75+
else {
76+
builder.storeUint(4, 8).storeBit(action.allowed);
77+
}
78+
}
79+
}
80+
81+
export function storeExtendedActions(actions: ExtendedAction[]) {
82+
const cell = actions.reverse().reduce((curCell, action) => {
83+
const ds = beginCell().store(storeExtensionAction(action));
84+
if(curCell.bits.length > 0) {
85+
ds.storeRef(curCell);
86+
}
87+
return ds.endCell();
88+
}, beginCell().endCell());
89+
90+
return (builder: Builder) => builder.storeSlice(cell.beginParse());
91+
}
92+
93+
export function message2action(msg: MessageOut) : OutActionSendMsg {
94+
return {
95+
type: 'sendMsg',
96+
mode: msg.mode,
97+
outMsg: msg.message
98+
}
99+
}
100+
101+
102+
export class WalletV5Test extends WalletV5 {
103+
constructor(readonly address: Address, readonly init?: { code: Cell; data: Cell }) {
104+
super(address, init);
105+
}
106+
static createFromAddress(address: Address) {
107+
return new WalletV5Test(address);
108+
}
109+
110+
static createFromConfig(config: WalletV5Config, code: Cell, workchain = 0) {
111+
const data = walletV5ConfigToCell(config);
112+
const init = { code, data };
113+
return new WalletV5Test(contractAddress(workchain, init), init);
114+
}
115+
static requestMessage(internal: boolean, wallet_id: bigint, valid_until: number, seqno: bigint | number, actions: WalletActions, key?: Buffer) {
116+
const op = internal ? Opcodes.auth_signed_internal : Opcodes.auth_signed;
117+
const msgBody = beginCell().storeUint(op, 32)
118+
.storeUint(wallet_id, 32)
119+
.storeUint(valid_until, 32)
120+
.storeUint(seqno, 32)
121+
.store(storeWalletActions(actions))
122+
.endCell();
123+
return key ? WalletV5Test.signRequestMessage(msgBody, key) : msgBody;
124+
}
125+
126+
static signRequestMessage(msg: Cell, key: Buffer) {
127+
const signature = sign(msg.hash(), key);
128+
129+
return beginCell().storeSlice(msg.asSlice()).storeBuffer(signature).endCell();
130+
}
131+
async sendMessagesExternal(provider: ContractProvider,
132+
wallet_id: bigint,
133+
valid_until: number,
134+
seqno: bigint | number,
135+
key: Buffer, messages: MessageOut[]) {
136+
const actions: OutActionSendMsg[] = messages.map(message2action);
137+
138+
await provider.external(
139+
WalletV5Test.requestMessage(false, wallet_id, valid_until, seqno, {wallet: actions}, key)
140+
);
141+
}
142+
143+
static extensionMessage(actions: WalletActions, query_id: bigint | number = 0) {
144+
return beginCell()
145+
.storeUint(Opcodes.auth_extension, 32)
146+
.storeUint(query_id, 64)
147+
.store(storeWalletActions(actions))
148+
.endCell();
149+
}
150+
async sendExtensionActions(provider: ContractProvider,
151+
via: Sender,
152+
actions: WalletActions,
153+
value: bigint = toNano('0.1'),
154+
query_id: bigint | number = 0) {
155+
156+
await provider.internal(via, {
157+
value,
158+
body: WalletV5Test.extensionMessage(actions, query_id),
159+
sendMode: SendMode.PAY_GAS_SEPARATELY
160+
});
161+
}
162+
163+
async sendMessagesInternal(provider: ContractProvider, via: Sender,
164+
wallet_id: bigint,
165+
valid_until: number,
166+
seqno: bigint | number,
167+
key: Buffer, messages: MessageOut[], value: bigint = toNano('0.05')) {
168+
169+
const actions: OutActionSendMsg[] = messages.map(message2action);
170+
171+
await provider.internal(via, {
172+
value,
173+
body: WalletV5Test.requestMessage(true, wallet_id, valid_until, seqno, {wallet: actions}, key),
174+
sendMode: SendMode.PAY_GAS_SEPARATELY
175+
});
176+
}
177+
178+
/*
179+
async sendAddExtensionViaExternal(provider: ContractProvider,
180+
wallet_id: bigint,
181+
valid_until: number,
182+
seqno: bigint | number,
183+
key: Buffer,
184+
extensions: Address[]) {
185+
const reqMsg = WalletV5Test.requestMessage(false, wallet_id, valid_until, seqno, {extension: beginCell().endCell()}, key);
186+
187+
await provider.external(reqMsg);
188+
}
189+
*/
190+
}

0 commit comments

Comments
 (0)