Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polishing TF-improvements to the w5 contract #24

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ actions$_ {m:#} {n:#} actions:(ActionList n m) = InnerRequest;
Contract state:
```tl-b
wallet_id$_ global_id:# wc:int8 version:(## 8) subwallet_number:# = WalletID;
contract_state$_ seqno:int33 wallet_id:WalletID public_key:(## 256) extensions_dict:(HashmapE 256 int8) = ContractState;
contract_state$_ signature_auth_disabled:(## 1) seqno:# wallet_id:WalletID public_key:(## 256) extensions_dict:(HashmapE 256 int8) = ContractState;
```

## Source code
Expand Down
199 changes: 76 additions & 123 deletions contracts/wallet_v5.fc

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions scripts/deployWalletV5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function run(provider: NetworkProvider) {
const walletV5 = provider.open(
WalletV5.createFromConfig(
{
signature_auth_disabled: false,
seqno: 0,
walletId: new WalletId({ networkGlobalId: -3 }).serialized, // testnet
publicKey: keypair.publicKey,
Expand Down
3 changes: 1 addition & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ export function bufferToBigInt(buffer: Buffer): bigint {
}

export function packAddress(address: Address) {
const wcPlus = address.workChain + 1;
return bufferToBigInt(address.hash) ^ BigInt(wcPlus);
return bufferToBigInt(address.hash);
}

export function validUntil(ttlMs = 1000 * 60 * 3) {
Expand Down
11 changes: 3 additions & 8 deletions tests/wallet-v5-extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('Wallet V5 extensions auth', () => {
walletV5 = blockchain.openContract(
WalletV5.createFromConfig(
{
signature_auth_disabled: false,
seqno: 0,
walletId: WALLET_ID.serialized,
publicKey: keypair.publicKey,
Expand Down Expand Up @@ -391,10 +392,7 @@ describe('Wallet V5 extensions auth', () => {
expect(isSignatureAuthAllowed1).toEqual(-1);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 2);

// Allowing or disallowing signature auth increments seqno, need to re-read
seqno = contract_seqno;
expect(contract_seqno).toEqual(seqno);

const testReceiver = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
const forwardValue = toNano(0.001);
Expand Down Expand Up @@ -480,10 +478,7 @@ describe('Wallet V5 extensions auth', () => {
expect(isSignatureAuthAllowed1).toEqual(-1);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 2);

// Allowing or disallowing signature auth increments seqno, need to re-read
seqno = contract_seqno;
expect(contract_seqno).toEqual(seqno);

const testReceiver = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
const forwardValue = toNano(0.001);
Expand Down
15 changes: 7 additions & 8 deletions tests/wallet-v5-external.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('Wallet V5 sign auth external', () => {
const _walletV5 = blockchain.openContract(
WalletV5.createFromConfig(
{
signature_auth_disabled: params?.signature_auth_disabled ?? false,
seqno: params?.seqno ?? 0,
walletId: params?.walletId ?? WALLET_ID.serialized,
publicKey: params?.publicKey ?? _keypair.publicKey,
Expand Down Expand Up @@ -100,6 +101,7 @@ describe('Wallet V5 sign auth external', () => {
walletV5 = blockchain.openContract(
WalletV5.createFromConfig(
{
signature_auth_disabled: false,
seqno: 0,
walletId: WALLET_ID.serialized,
publicKey: keypair.publicKey,
Expand Down Expand Up @@ -293,7 +295,7 @@ describe('Wallet V5 sign auth external', () => {
});

it('Add two extensions and do a transfer', async () => {
const testExtension1 = Address.parse('Ef82pT4d8T7TyRsjW2BpGpGYga-lMA4JjQb4D2tc1PXMX28X');
const testExtension1 = Address.parse('EQA2pT4d8T7TyRsjW2BpGpGYga-lMA4JjQb4D2tc1PXMX5Bf');
const testExtension2 = Address.parse('EQCgYDKqfTh7zVj9BQwOIPs4SuOhM7wnIjb6bdtM2AJf_Z9G');

const testReceiver = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
Expand Down Expand Up @@ -746,7 +748,7 @@ describe('Wallet V5 sign auth external', () => {
expect(isSignatureAuthAllowed).toEqual(0);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 1);
expect(contract_seqno).toEqual(seqno);
});

it('Should add ext and disallow signature auth in separate txs', async () => {
Expand Down Expand Up @@ -797,7 +799,7 @@ describe('Wallet V5 sign auth external', () => {
expect(isSignatureAuthAllowed2).toEqual(0);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 1);
expect(contract_seqno).toEqual(seqno);
});

it('Should add ext, disallow sign, allow sign, remove ext in one tx; send in other', async () => {
Expand All @@ -823,10 +825,7 @@ describe('Wallet V5 sign auth external', () => {
expect(isSignatureAuthAllowed).toEqual(-1);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 2);

// Allowing or disallowing signature auth increments seqno, need to re-read
seqno = contract_seqno;
expect(contract_seqno).toEqual(seqno);

const testReceiver = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
const forwardValue = toNano(0.001);
Expand Down Expand Up @@ -932,7 +931,7 @@ describe('Wallet V5 sign auth external', () => {
expect(isSignatureAuthAllowed).toEqual(0);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 1);
expect(contract_seqno).toEqual(seqno);

await disableConsoleError(() =>
expect(walletV5.sendExternalSignedMessage(createBody(packActionsList([])))).rejects.toThrow()
Expand Down
14 changes: 10 additions & 4 deletions tests/wallet-v5-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('Wallet V5 get methods', () => {
walletV5 = blockchain.openContract(
WalletV5.createFromConfig(
{
signature_auth_disabled: params?.signature_auth_disabled ?? false,
seqno: params?.seqno ?? 0,
walletId: params?.walletId ?? WALLET_ID.serialized,
publicKey: params?.publicKey ?? keypair.publicKey,
Expand Down Expand Up @@ -107,7 +108,7 @@ describe('Wallet V5 get methods', () => {

it('Get extensions dict', async () => {
const plugin1 = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
const plugin2 = Address.parse('Ef82pT4d8T7TyRsjW2BpGpGYga-lMA4JjQb4D2tc1PXMX28X');
const plugin2 = Address.parse('EQA2pT4d8T7TyRsjW2BpGpGYga-lMA4JjQb4D2tc1PXMX5Bf');

const extensions: Dictionary<bigint, bigint> = Dictionary.empty(
Dictionary.Keys.BigUint(256),
Expand All @@ -126,22 +127,27 @@ describe('Wallet V5 get methods', () => {
});

it('Get extensions array', async () => {
const plugin1 = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
const plugin2 = Address.parse('Ef82pT4d8T7TyRsjW2BpGpGYga-lMA4JjQb4D2tc1PXMX28X');
const plugin1 = Address.parse(
'0:0000F5851B4A185F5F63C0D0CD0412F5ACA353F577DA18FF47C936F99DBD0000'
);
const plugin2 = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
const plugin3 = Address.parse('EQA2pT4d8T7TyRsjW2BpGpGYga-lMA4JjQb4D2tc1PXMX5Bf');

const extensions: Dictionary<bigint, bigint> = Dictionary.empty(
Dictionary.Keys.BigUint(256),
Dictionary.Values.BigInt(8)
);
extensions.set(packAddress(plugin1), BigInt(plugin1.workChain));
extensions.set(packAddress(plugin2), BigInt(plugin2.workChain));
extensions.set(packAddress(plugin3), BigInt(plugin3.workChain));

await deploy({ extensions });

const actual = await walletV5.getExtensionsArray();
expect(actual.length).toBe(2);
expect(actual.length).toBe(3);
expect(actual[0].equals(plugin1)).toBeTruthy();
expect(actual[1].equals(plugin2)).toBeTruthy();
expect(actual[2].equals(plugin3)).toBeTruthy();
});

it('Get empty extensions array', async () => {
Expand Down
17 changes: 8 additions & 9 deletions tests/wallet-v5-internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('Wallet V5 sign auth internal', () => {
const _walletV5 = blockchain.openContract(
WalletV5.createFromConfig(
{
signature_auth_disabled: params?.signature_auth_disabled ?? false,
seqno: params?.seqno ?? 0,
walletId: params?.walletId ?? WALLET_ID.serialized,
publicKey: params?.publicKey ?? _keypair.publicKey,
Expand Down Expand Up @@ -93,6 +94,7 @@ describe('Wallet V5 sign auth internal', () => {
walletV5 = blockchain.openContract(
WalletV5.createFromConfig(
{
signature_auth_disabled: false,
seqno: 0,
walletId: WALLET_ID.serialized,
publicKey: keypair.publicKey,
Expand Down Expand Up @@ -266,7 +268,7 @@ describe('Wallet V5 sign auth internal', () => {
});

it('Add two extensions and do a transfer', async () => {
const testExtension1 = Address.parse('Ef82pT4d8T7TyRsjW2BpGpGYga-lMA4JjQb4D2tc1PXMX28X');
const testExtension1 = Address.parse('EQA2pT4d8T7TyRsjW2BpGpGYga-lMA4JjQb4D2tc1PXMX5Bf');
const testExtension2 = Address.parse('EQCgYDKqfTh7zVj9BQwOIPs4SuOhM7wnIjb6bdtM2AJf_Z9G');

const testReceiver = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
Expand Down Expand Up @@ -941,7 +943,7 @@ describe('Wallet V5 sign auth internal', () => {
expect(isSignatureAuthAllowed).toEqual(0);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 1);
expect(contract_seqno).toEqual(seqno);
});

it('Should add ext and disallow signature auth in separate txs', async () => {
Expand Down Expand Up @@ -1008,7 +1010,7 @@ describe('Wallet V5 sign auth internal', () => {
expect(isSignatureAuthAllowed2).toEqual(0);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 1);
expect(contract_seqno).toEqual(seqno);
});

it('Should add ext, disallow sign, allow sign, remove ext in one tx; send in other', async () => {
Expand Down Expand Up @@ -1041,10 +1043,7 @@ describe('Wallet V5 sign auth internal', () => {
expect(isSignatureAuthAllowed).toEqual(-1);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 2);

// Allowing or disallowing signature auth increments seqno, need to re-read
seqno = contract_seqno;
expect(contract_seqno).toEqual(seqno);

const testReceiver = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
const forwardValue = toNano(0.001);
Expand Down Expand Up @@ -1175,7 +1174,7 @@ describe('Wallet V5 sign auth internal', () => {
expect(isSignatureAuthAllowed).toEqual(0);

const contract_seqno = await walletV5.getSeqno();
expect(contract_seqno).toEqual(seqno + 1);
expect(contract_seqno).toEqual(seqno);

const testReceiver = Address.parse('EQAvDfWFG0oYX19jwNDNBBL1rKNT9XfaGP9HyTb5nb2Eml6y');
const forwardValue = toNano(0.001);
Expand All @@ -1194,7 +1193,7 @@ describe('Wallet V5 sign auth internal', () => {
(receipt2.transactions[1].description as TransactionDescriptionGeneric)
.computePhase as TransactionComputeVm
).exitCode
).toEqual(33);
).toEqual(0);

expect(receipt2.transactions).not.toHaveTransaction({
from: walletV5.address,
Expand Down
2 changes: 1 addition & 1 deletion types.tlb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ actions$_ {m:#} {n:#} actions:(ActionList n m) = InnerRequest;

// Contract state
wallet_id$_ global_id:int32 wc:int8 version:(## 8) subwallet_number:(## 32) = WalletID;
contract_state$_ seqno:int33 wallet_id:WalletID public_key:(## 256) extensions_dict:(HashmapE 256 int8) = ContractState;
contract_state$_ signature_auth_disabled:(## 1) seqno:# wallet_id:WalletID public_key:(## 256) extensions_dict:(HashmapE 256 int8) = ContractState;
8 changes: 5 additions & 3 deletions wrappers/wallet-v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { bufferToBigInt } from '../tests/utils';

export type WalletV5Config = {
signature_auth_disabled: boolean;
seqno: number;
walletId: bigint;
publicKey: Buffer;
Expand All @@ -23,7 +24,8 @@ export type WalletV5Config = {

export function walletV5ConfigToCell(config: WalletV5Config): Cell {
return beginCell()
.storeInt(config.seqno, 33)
.storeBit(config.signature_auth_disabled)
.storeUint(config.seqno, 32)
.storeUint(config.walletId, 80)
.storeBuffer(config.publicKey, 32)
.storeDict(config.extensions, Dictionary.Keys.BigUint(256), Dictionary.Values.BigInt(8))
Expand Down Expand Up @@ -232,8 +234,8 @@ export class WalletV5 implements Contract {

return dict.keys().map(key => {
const wc = dict.get(key)!;
const addressHex = key ^ (wc + 1n);
return Address.parseRaw(`${wc}:${addressHex.toString(16)}`);
const addressHex = key;
return Address.parseRaw(`${wc}:${addressHex.toString(16).padStart(64, '0')}`);
});
}
}
Loading