Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
206 commits
Select commit Hold shift + click to select a range
bcf55b7
Merge pull request #2 from tonkeeper/fix/update-wallet_id
siandreev Sep 19, 2023
42a41ca
Fix link to source code
oleganza Sep 26, 2023
ccba7a4
With libs wallet v5 is 93% smaller than v4
oleganza Sep 26, 2023
1748d80
Actual code size reduction
oleganza Sep 26, 2023
13d1e72
feat: lib deployment scripts added
siandreev Sep 27, 2023
7356c23
fix: contract gas optimisation
siandreev Sep 27, 2023
550894f
fix: library deployer improvement
siandreev Sep 27, 2023
b1a9af8
fix: deployment scripts fixes
siandreev Sep 27, 2023
924e3c6
fix: specification updated
siandreev Sep 27, 2023
cd4f684
fix: lib code added to the tests
siandreev Sep 28, 2023
b882bf7
fix: added another single transfer test
siandreev Sep 28, 2023
d399976
Optimized unneccessary cell loads and operations
Skydev0h Oct 2, 2023
c35b90b
Recorded failed attempt at defragmenting storage
Skydev0h Oct 2, 2023
fcc45d6
Removed unneccessary always true check
Skydev0h Oct 2, 2023
7ff7d07
Unrolled the common internal handler code
Skydev0h Oct 2, 2023
01cf03d
Implicitly return from the external handler
Skydev0h Oct 2, 2023
557c0d7
Reaped benefits of separated internal loaders
Skydev0h Oct 2, 2023
a8ee4c0
Discarded unneccessary slice remains in dispatcher
Skydev0h Oct 2, 2023
5e65d1c
Loaded auth_kind optionally using LDUQ instruction
Skydev0h Oct 3, 2023
44345e4
Is ifnot a joke for you? (emits less instructions)
Skydev0h Oct 3, 2023
bacb468
Localize extensions in loop and short-circ simple
Skydev0h Oct 3, 2023
46033e9
Reordering int msg handlers somehow saves 10 gas
Skydev0h Oct 3, 2023
57d1a9f
Moving signature check higher saves some gas
Skydev0h Oct 3, 2023
ff9a1b2
Reordering checks somehow sames some more gas
Skydev0h Oct 3, 2023
3601ebe
Removing end_parse is -gas and +reliability
Skydev0h Oct 3, 2023
eaf48f7
Keep your functions close and vars even closer
Skydev0h Oct 3, 2023
15f3dc6
OK, can remove end_parse, will write rationale later
Skydev0h Oct 3, 2023
ea38004
Add global gas counters to keep an eye on non-contest paths
Skydev0h Oct 9, 2023
f245407
Recalculated GGC for every commit to find a tradeoff
Skydev0h Oct 9, 2023
99cd428
Refactored internal message flows, good GGC value
Skydev0h Oct 9, 2023
48f8868
Github-friendly RST improvements page
Skydev0h Oct 9, 2023
9ecfb79
Reorganized inlining point for extension message flow
Skydev0h Oct 9, 2023
fe7f68e
Fix improvements table width for commit name
Skydev0h Oct 9, 2023
50ccdc0
[Internal] Fix ext-storer bug (negligible GGC gas impact)
Skydev0h Oct 9, 2023
a4890b2
Do not carry around params not needed (ext opt)
Skydev0h Oct 9, 2023
75c5bac
Add failed rewrite attempt (signed processing)
Skydev0h Oct 9, 2023
c45523e
Optimize argument order to match stack
Skydev0h Oct 9, 2023
fc3bc96
More reliable tail processing at cost of very small GGC increase
Skydev0h Oct 9, 2023
79ab86d
Swapping extn and sign order back saves some net gas
Skydev0h Oct 9, 2023
4f0cea7
Substantial code rewrite to make it more readable
Skydev0h Oct 10, 2023
4c6ee62
Record failed attempt to cache one ext w/o dict (increased gas)
Skydev0h Oct 10, 2023
1a08eae
Short-circuit optimization of LDUQ with IFNOTRET
Skydev0h Oct 10, 2023
3c26248
Short-circuited some returns with asm
Skydev0h Oct 10, 2023
7adaf5f
ASM-optimized simple action cases
Skydev0h Oct 10, 2023
bd454a7
Created scalpel tool for TVM delidding and analyzing under microscope
Skydev0h Oct 10, 2023
d8cd02d
Optimized out more unneeded instructions if may RET
Skydev0h Oct 10, 2023
204dddd
Removed another unneccessary DROP with preload uint
Skydev0h Oct 10, 2023
d19f689
Reordered argument order to optimize stack operations
Skydev0h Oct 10, 2023
ba29b84
Rewritten RETALT to IFNOTJMP - less gas, more reliable
Skydev0h Oct 10, 2023
8e5696b
Another argument stack optimization (psr -> dr call)
Skydev0h Oct 10, 2023
f77dded
Black magic route optimization (drop some result later)
Skydev0h Oct 10, 2023
4192634
Supercharged scalpel
Skydev0h Oct 10, 2023
542b241
This quite did not worked out. git good.
Skydev0h Oct 11, 2023
f461946
Another black magic optimization (drop auth_kind later)
Skydev0h Oct 11, 2023
e62691e
WIP: Documentation on optimizations
Skydev0h Oct 11, 2023
272892e
Update README.md
Skydev0h Oct 11, 2023
be920b8
Center contest logo
Skydev0h Oct 11, 2023
870e9ff
Fix centering
Skydev0h Oct 11, 2023
2ddb845
Update README
Skydev0h Oct 11, 2023
18a3e62
I have not noticed this achievement earlier!
Skydev0h Oct 11, 2023
15b4d6c
Revised code, started researching gas under microscope
Skydev0h Oct 12, 2023
653fa9d
Add tools to inspect generated code cell tree
Skydev0h Oct 12, 2023
8bdcf47
fift stuff
Skydev0h Oct 12, 2023
5e9bfa0
Prepare ground for radical optimizations
Skydev0h Oct 12, 2023
c11545f
Allow to turn on and off detailed gas tracing of main cases in tests
Skydev0h Oct 12, 2023
0794b7d
More preparations for radical optimization
Skydev0h Oct 12, 2023
8041686
Backport some tooling from entrypoint branch
Skydev0h Oct 13, 2023
886b9fe
Backported FunC optimizations from entrypoint branch
Skydev0h Oct 13, 2023
fcdb880
Use SDBEGINS to enforce "sign" prefix in external message
Skydev0h Oct 13, 2023
07b73f2
Use SDBEGINSQ to check internal message prefixes
Skydev0h Oct 13, 2023
4f2cf58
Backported some opts, organized code and docs
Skydev0h Oct 13, 2023
22a2f68
Finally written documentation about improvements
Skydev0h Oct 15, 2023
e104fda
Optimized instructions order for extension and fix args
Skydev0h Oct 15, 2023
17337eb
Clean up failed optimization attempts
Skydev0h Nov 2, 2023
33c4e87
Cleaned up unused code
Skydev0h Nov 2, 2023
5709f2a
Fixed calculations in table (type in origin amount)
Skydev0h Nov 2, 2023
b475fda
Fixed more calculations in improvements file
Skydev0h Nov 2, 2023
af93f0a
Update README.md
siandreev Nov 11, 2023
b1878cc
Update Specification.md
siandreev Nov 11, 2023
ea879ad
Merge pull request #3 from Skydev0h/main
siandreev Nov 20, 2023
88ba4f6
chore: build script
siandreev Nov 20, 2023
dbb7292
Merge remote-tracking branch 'origin/main'
siandreev Nov 20, 2023
0b95549
chore: ci updated
siandreev Nov 21, 2023
6145f6b
Merge branch 'main' into feature/optimisation
Skydev0h Jan 23, 2024
6a245a2
Added root cell repacking algorithm with enforced Asm version
Skydev0h Jan 24, 2024
e70ff08
Added size constants and changed stored seqno type from ui32 to i33
Skydev0h Jan 24, 2024
194f803
Changed sign op for internal messages to sint to avoid replay attacks
Skydev0h Jan 24, 2024
04a363d
Removed set_data and temporarily disabled the relevant tests
Skydev0h Jan 24, 2024
3222372
Added actions checking (send_msg w/ mode 2 enforced) and set pk enabled
Skydev0h Jan 24, 2024
54a28d3
Add guidelines and code to be able to make unsafe version
Skydev0h Jan 24, 2024
7abb84d
Optimized verify_actions by rearranging loop and conditions
Skydev0h Jan 24, 2024
12b6e05
Added more info about sequence and fixed some cases in getters
Skydev0h Jan 24, 2024
c5c65d1
Adjusted comments to be more correct and precise
Skydev0h Jan 24, 2024
0c9fa87
Removed unneccessary noop middleware and renamed method to classic name
Skydev0h Jan 24, 2024
74cc7f6
Removed throwif from the comment about seqno
Skydev0h Jan 24, 2024
4217b8a
Made internal messages bounce-safe (replaced throws with soft checks)
Skydev0h Jan 24, 2024
dc6b4e9
Added quick return for empty internal messages
Skydev0h Jan 24, 2024
3ad3a7a
[Docs] Moved contest note to separate file and tidied up README a bit
Skydev0h Jan 24, 2024
0d9ffef
[Docs] Slight readme tidy-up
Skydev0h Jan 24, 2024
507b821
[Docs] Updated specification in accordance with new contract features
Skydev0h Jan 24, 2024
b5bdf24
[Docs] Update types.tlb
Skydev0h Jan 24, 2024
f2601ae
[Docs] Add use cases for disabling public key
Skydev0h Jan 27, 2024
9545120
Updated actions, allow naming, empty ref check, docs, actions wrapper
Skydev0h Jan 27, 2024
c7bd87a
Added gas tests for incoming messages
Skydev0h Jan 28, 2024
916ed76
Removed fift optimizations that might create problems for tooling
Skydev0h Jan 28, 2024
43eba0d
Flattened and split some asm functions for understanding and toolings
Skydev0h Jan 28, 2024
167347a
Add external message bit to signed message part
Skydev0h Jan 30, 2024
e580224
Changed signed message prefix from 1 bit to 32-bit message tag
Skydev0h Feb 3, 2024
41f3063
Moved signature to the end of the message (includes opcode)
Skydev0h Feb 9, 2024
033b63e
Adjusted TLB definitions to match signature at the end
Skydev0h Feb 9, 2024
34a8556
Adjusted all tests to match new signature place (in end)
Skydev0h Feb 9, 2024
c724855
Fixed accounting for seqno twice on signature disabling
Skydev0h Feb 12, 2024
4bf0feb
Added throw for unknown ext-act, setcode/data neg tests, test-only acts
Skydev0h Feb 15, 2024
3f76dbb
Added tests for signature disallow cases, incorrect allow act now throws
Skydev0h Feb 15, 2024
7f786ea
Added a test for separate auth disable by extension
Skydev0h Feb 15, 2024
a840198
Merge pull request #13 from Skydev0h/feature/optimisation
oleganza Feb 19, 2024
7739099
improved overview wording
oleganza Feb 20, 2024
c2734f0
Bounce internal signed messages to protect relayer against abuse
oleganza Feb 20, 2024
b233fb7
fix throw argument
oleganza Feb 20, 2024
55a383d
Merge pull request #14 from tonkeeper/oleganza/sint-bounce
oleganza Feb 20, 2024
4cd9181
Fixed failing tests after adding back throw (bounce) for sint after sig
Skydev0h Feb 20, 2024
53cf00c
Merge pull request #15 from Skydev0h/skydev/fix-bouncing-tests
oleganza Feb 20, 2024
8661ce2
Added safeguard against deleting last ext with disabled key (need tests)
Skydev0h Feb 21, 2024
4458be0
Fix prefix clashing bug and adjust code style (reported by @behrang)
Skydev0h Feb 23, 2024
51358ad
Fixed tests and added cases for deleting last ext with disallowed pubkey
Skydev0h Feb 23, 2024
fa1b372
Merge pull request #16 from Skydev0h/skydev/del-ext-safeguard
oleganza Feb 23, 2024
d373fb0
Introduction improvements
oleganza Jun 13, 2024
92471dd
Some improvements and changes to the contract
Skydev0h Jun 14, 2024
7d1d144
Plugins now bound to same workchain as the wallet, no packing (6)
Skydev0h Jun 14, 2024
1f9b0a3
Moved signature_auth_disable to separate variable from seq_no sign (3)
Skydev0h Jun 14, 2024
a210caa
Fix `getExtensionsArray()` method when address hash starts with `00`
Skydev0h Jun 14, 2024
ba7e312
add builds to git
rise1507 Jun 21, 2024
1d44704
cosmetic: rename constants (no changes in compiled code)
rise1507 Jun 21, 2024
99493bc
cosmetic: types (no changes in compiled code)
rise1507 Jun 21, 2024
0d5cc02
cosmetic: rename vars, avoid abbreviations (no changes in compiled code)
rise1507 Jun 21, 2024
ce56813
cosmetic: remove comments about optimizations (no changes in compiled…
rise1507 Jun 21, 2024
b58bbb9
cosmetic: add additional consts for sizes (no changes in compiled code)
rise1507 Jun 21, 2024
f1f4cb4
cosmetic: add error const (no changes in compiled code)
rise1507 Jun 21, 2024
0537f52
cosmetic: subwallet_id->wallet_id in comments (no changes in compiled…
rise1507 Jun 21, 2024
0574f37
don't store workchain in extensions dict values
rise1507 Jun 21, 2024
2f20a49
remove unnecessary micro-optimiazations
rise1507 Jun 21, 2024
edef797
simplify
rise1507 Jun 21, 2024
4f77395
refactor signature_allowed
rise1507 Jun 21, 2024
9869fef
preload_dict
rise1507 Jun 21, 2024
5a21842
get-methods cosmetic
rise1507 Jun 21, 2024
ad1d90b
simplify
rise1507 Jun 21, 2024
2e31a01
simplify
rise1507 Jun 21, 2024
5424092
c5 comments
rise1507 Jun 21, 2024
b3d712d
fix size check
rise1507 Jun 21, 2024
520dec7
fix error naming
rise1507 Jun 21, 2024
243d7d2
simplify
rise1507 Jun 21, 2024
a39b7b0
simplify
rise1507 Jun 21, 2024
fe128dd
cosmetic-optimization
rise1507 Jun 21, 2024
ab0233d
explicit () in binary operations
rise1507 Jun 22, 2024
477bf6d
cosmetic
rise1507 Jun 22, 2024
029e60a
fix return
rise1507 Jun 22, 2024
5910896
simplify
rise1507 Jun 22, 2024
2ba7fda
commit only for external
rise1507 Jun 22, 2024
c10105a
xctos comment
rise1507 Jun 22, 2024
1f3c177
update stdlib from stablecoin-contract
rise1507 Jun 22, 2024
464e68d
rename get_is_signature_auth_allowed -> is_signature_allowed
rise1507 Jun 22, 2024
d5ea381
comments
rise1507 Jun 24, 2024
816e446
add query_id to extension request so that the extension can recognise…
rise1507 Jun 24, 2024
43fabdf
action prefix 8bit
rise1507 Jun 25, 2024
68a0866
wallet_id 32bit
rise1507 Jun 25, 2024
21f229a
change refs in message (tests not synced yet)
rise1507 Jun 25, 2024
0a40931
only extension can change signature mode (tests not synced yet)
rise1507 Jun 25, 2024
3d822d9
allow work with (~ is_signature_allowed) and empty extensions
rise1507 Jun 25, 2024
c5dc057
refactor errors - only only_extension_can_change_signature_mode numbe…
rise1507 Jun 26, 2024
c678610
refactor errors naming
rise1507 Jun 26, 2024
aff9c49
refactor errors
rise1507 Jun 26, 2024
e4b8c5b
cosmetic (bytecode not changed)
rise1507 Jun 26, 2024
dd9ff28
cosmetic comment (bytecode not changed)
rise1507 Jun 26, 2024
b7b8fd8
simplify
rise1507 Jun 26, 2024
3dc030e
fix mistake
rise1507 Jun 26, 2024
e6ef9e4
receive tons
rise1507 Jun 26, 2024
39d0feb
get_wallet_id -> get_subwallet_id, same with v4
rise1507 Jun 26, 2024
444ddde
return_unless not work
rise1507 Jun 26, 2024
3bd769e
update wrappers and tests
rise1507 Jun 26, 2024
38f7e3a
fix typo
rise1507 Jun 27, 2024
64cff84
change error codes to avoid collision with tvm-exit-codes
rise1507 Jun 28, 2024
6e81ad3
remove return_if, return_unless because its dangerous
rise1507 Jun 28, 2024
facbfcf
additional checks for c5
rise1507 Jul 1, 2024
84eaf15
fix numbers
rise1507 Jul 1, 2024
ee52524
Upgrade packages
Jul 2, 2024
047852a
Migrate to @ton packages
Jul 2, 2024
982f304
Minor fix
Jul 2, 2024
ff8cd74
Extra moar utils for the utils god
Jul 2, 2024
d349886
Test wrapper and constants
Jul 2, 2024
57e37aa
W5 wallet tests
Jul 2, 2024
e805cde
Merge pull request #1 from Trinketer22/w5_tests
tolya-yanot Jul 3, 2024
6220bd6
rebuild
rise1507 Jul 3, 2024
2f962c3
optimize bounced check
rise1507 Jul 3, 2024
b66b103
more accurate check
rise1507 Jul 3, 2024
62bfd99
delete unused code
rise1507 Jul 3, 2024
0bfb383
update tlb
rise1507 Jul 3, 2024
2c1b964
update tlb
rise1507 Jul 3, 2024
e02e43c
delete v4
rise1507 Jul 3, 2024
26ab229
update Specification.md
rise1507 Jul 3, 2024
230e48b
simplify
rise1507 Jul 3, 2024
5e711b2
strict func version
rise1507 Jul 3, 2024
5962103
update tlb
rise1507 Jul 3, 2024
c5465be
Merge pull request #28 from tolya-yanot/main
KuznetsovNikita Jul 3, 2024
9f51f4a
fix w5 tkb
Kaladin13 Nov 11, 2025
69785b9
Merge pull request #245 from Kaladin13/fix-tlb-v5
Self-Perfection Nov 11, 2025
f019dcd
Add LICENSE.exe file
AnonymousScript007 Jan 2, 2026
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
WALLET_MNEMONIC=

# v4
WALLET_VERSION=
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Run tests

on:
push:
branches: [ "main" ]
branches: [ "main", "feature/optimisation" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "feature/optimisation" ]

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
temp
build
.idea
.env
26 changes: 26 additions & 0 deletions Contest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Contest note!

<div align="center">
<img alt="Contest logo" src="contest.png" height="280" width="280">
</div>

**Because of the extreme amount of optimizations, developer's discretion is advised!** *Evil laugh*

The build system is the same as in the original Wallet V5, **no security features have been sacrificed**
for performance improvements, that is - there are **practically no tradeoffs or compromises**.

Message and storage layouts were **not changed**, although some rearragement might squeeze a little more gas,
but that may break existing optimizations due to stack reordering.

Also, **tests were improved** - a **Global Gas Counter** mechanism was added that accounts for gas in all transactions
of all test suites and cases (except for negative and getter ones). This allows to keep an eye on other non-contest
cases to track how bad is tradeoff when performing optimizations here and there.

Another utility that was developed for contest is ***scalpel script***, that allows for a detailed, *really* detailed optimizations
of the code by comparing lines of code function by function, printing out diffs, and providing detailed TVM files with
stack comments and rewrites. This utility allowed to make some latter optimizations, since with each optimization
next one becomes exponentionally harder to make. While result is not entirely precise and is needed to be verified
by tests, this allows to instantly estimate whether there is some progress or not, since scalpel is executed immediately,
while tests take approximately 10 seconds to execute.

### Details of optimizations, their rationale and explanations, comparison of consumed gas both in test cases and not in test cases (global gas counter) are provided on a dedicated page: [Gas improvements](Improvements.rst).
447 changes: 447 additions & 0 deletions Improvements.rst

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions LICENSE → LICENSE.exe
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
MIT License
MIT License
Copy link

@spinnercasino12345-ux spinnercasino12345-ux Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Copyright (c) 2023 Tonkeeper
Copyright (c) 2026 Tonkeeper Web

© 2026 Yeifry Borrero Salcedo
This documentation is provided for educational purposes only.
Commercial use requires explicit authorization.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
# Wallet V5
# 🔥W5: wallet v5 standard

This is an extensible wallet specification aimed at replacing V4 and allowing arbitrary extensions.

Wallet V5 is 34% smaller in bytecode, can delegate payments for gas to third parties and supports flexible extension mechanism.
W5 has **25% lower fees**, supports **gasless transactions** (via third party relayers) and implements a **flexible extension mechanism**.

## Project structure

- [Specification](Specification.md)
- `contracts` - source code of all the smart contracts of the project and their dependencies.
- `wrappers` - wrapper classes (implementing `Contract` from ton-core) for the contracts, including any [de]serialization primitives and compilation functions.
- `tests` - tests for the contracts.
- `scripts` - scripts used by the project, mainly the deployment scripts.
- `scripts` - scripts used by the project, mainly the deployment scripts, additionally contains utilities for gas optimisation.
- `fift` - contains standard Fift v0.4.4 library including the assembler and disassembler for gas optimisation utilities.

### Additional documentation

- [Gas improvements](Improvements.rst) - a log of improvements, detailed by primary code paths, global gas counters per commit.
- [Contest](Contest.md) - a note showing some information about interesting improvements during the optimisation contest.

## How to use

### Build

`npx blueprint build` or `yarn blueprint build`
`npm run build:v5`

### Test

`npx blueprint test` or `yarn blueprint test`

### Deploy or run another script
`npm run test`

`npx blueprint run` or `yarn blueprint run`
### Deployment
1. Deploy library: `npm run deploy-library`
2. Deploy wallet: `npm run deploy-wallet`

### Add a new contract
### Get wallet compiled code

`npx blueprint create ContractName` or `yarn blueprint create ContractName`
`npm run print-wallet-code`
127 changes: 21 additions & 106 deletions Specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,35 @@ Thanks to [Andrew Gutarev](https://github.com/pyAndr3w) for the idea to set c5 r

Thanks to [@subden](https://t.me/subden), [@botpult](https://t.me/botpult) and [@tvorogme](https://t.me/tvorogme) for ideas and discussion.

Thanks to [Skydev](https://github.com/Skydev0h) for optimization and preparing the second revision of the contract.


## Features

* 34% smaller compiled code than in v4R2 (481 vs 734 bytes).
* 25% smaller computation fees.
* Arbitrary amount of outgoing messages is supported via action list.
* Wallet code can be upgraded transparently without breaking user's address in the future.
* Wallet code can be extended by anyone in a decentralized and conflict-free way: multiple feature extensions can co-exist.
* Extensions can perform the same operations as the signer: emit arbitrary messages on behalf of the owner, add and remove extensions.
* Signed requests can be delivered via internal message to allow 3rd party pay for gas.
* For consistency and ease of indexing, external messages also receive a 32-bit opcode.
* To lay foundation for support of scenarios like 2FA or access recovery it is possible to disable signature authentication.

## Overview

Wallet V5 supports **2 authentication modes**, all standard output actions (send message, set library, code replacement) plus additional **3 operation types**.
Wallet V5 supports **2 authentication modes** and **3 operations types**.

Authentication:
* by signature
* by extension

Operation types:
* standard output actions
* “set data” operation
* install extension
* remove extension
Operations:
* standard "send message" action (up to 255 messages at once),
* enable/disable signature authentication (can be invoked only by extension),
* install/remove extension.

Signed messages can be delivered both by external and internal messages.

All operation types are available to all authentication modes.
All operations are available to all authentication modes.

## Discussion

Expand All @@ -63,40 +64,12 @@ User may delegate this job to other apps via extensions.

### Extending the wallet

**A. Use extensions**

The best way to extend functionality of the wallet is to use the extensions mechanism that permit delegating access to the wallet to other contracts.

From the perspective of the wallet, every extension can perform the same actions as the owner of a private key. Therefore limits and capabilities can be embedded in such an extension with a custom storage scheme.

Extensions can co-exist simultaneously, so experimental capabilities can be deployed and tested independently from each other.

**B. Code optimization**

Backwards compatible code optimization **can be performed** with a single `set_code` action (`action_set_code#ad4de08e`) signed by the user. That is, hypothetical upgrade from `v5R1` to `v5R2` can be done in-place without forcing users to change their wallet address.

If the optimized code requires changes to the data layout (e.g. reordering fields) the user can sign a request with two actions: `set_code` (in the standard action) and `set_data` (an extended action per this specification). Note that `set_data` action must make sure `seqno` is properly incremented after the upgrade as to prevent replays. Also, `set_data` must be performed right before the standard actions to not get overwritten by extension actions. The updated wallet **must** have the new subwallet ID to prevent accidental repeated migrations.

User agents **should not** make `set_code` and `set_data` actions available via general-purpose API to prevent misuse and mistakes. Instead, they should be used as a part of migration logic for a specific wallet code.

To restore the wallet by a seed phrase, user agent should use the original code and should expect the upgraded code to work in exactly the same way as previously.

**C. Emergency upgrades**

This is a variant of (B), so the same consideration apply. The difference is that functionality may be modified as to prevent user from suffering loss of funds. E.g. some previously possible actions or signed messages would lead to a failure.

Just like with (B), user agents **should not** make `set_code` and `set_data` actions available via general-purpose API to prevent misuse and mistakes. Instead, they should be used as a part of migration logic for a specific wallet code.

New users’ wallets **should not** be deployed with upgraded code. Instead, the improved wallet code should also be released as a new wallet version (e.g. v6, with a separate subwallet ID) and new wallets should be deployed with that code. This way `set_code` would be used as an emergency patch for existing wallets, while new wallets would be deployed directly with the major next version.


**D. Substantial upgrades**

We **do not recommend** performing substantial wallet upgrades in-place using `set_code`/`set_data` actions. Instead, user agents should have support for multiple accounts and easy switching between them.

In-place migration requires maintaining backwards compatibility for all wallet features, which in turn could lead to increase in code size and higher gas and rent costs.


### Can the wallet outsource payment for gas fees?

Yes! You can deliver signed messages via an internal message from a 3rd party wallet. Also, the message is handled exactly like an external one: after the basic checks the wallet takes care of the fees itself, so that 3rd party does not need to overpay for users who actually do have TONs.
Expand All @@ -123,83 +96,25 @@ You need to put two requests in your message body:

Yes. We have considered constant-size schemes where the wallet only stores trusted extension code. However, extension authentication becomes combursome and expensive: plugin needs to transmit additional data and each request needs to recompute plugin’s address. We estimate that for the reasonably sized wallets (less than 100 plugins) authentication via the dictionary lookup would not exceed costs of indirect address authentication.

### Why it can be useful to disallow authentication with signature?

## Wallet ID

Wallet ID disambiguates requests signed with the same public key to different wallet versions (V3/V4/V5) or wallets deployed on different chains.

For Wallet V5 we suggest using the following wallet ID:

```
mainnet: 20230823 + workchain
testnet: 30230823 + workchain
```

## Packed address

To make authorize extensions efficiently we compress 260-bit address (workchain + sha256 of stateinit) into a 256-bit integer:

```
int addr = addr_hash ^ (wc + 1)
```
Ability to disallow authentication with signature enables two related use-cases:

Previously deployed wallet v4 was packing the address into a cell which costs ≈500 gas, while access to dictionary costs approximately `120*lg2(N)` in gas, that is serialization occupies more than half of the access cost for wallets with up to 16 extensions. This design makes packing cost around 50 gas and allows cutting the authentication cost 2-3x for reasonably sized wallets.
1. Two-factor authentication schemes: where control over wallet is fully delegated to an extension that checks two signatures: the user’s one and the signature from the auth service. Naturally, if the signature authentication in the wallet remains allowed, the second factor check is bypassed.

As of 2023 TON network consists of two workchains: -1 (master) and 0 (base). This means that the proposed address packing reduces second-preimage resistance of sha256 by 1 bit which we consider negligible. Even if the network is expanded with 254 more workchains in a distant future, our scheme would reduce security of extension authentication by only 8 bits down to 248 bits. Note that birthday attack is irrelevant in our setting as the user agent is not installing random extensions, although the security margin is plenty anyway (124 bits).
2. Account recovery: delegating full control to another wallet in case of key compromise or loss. Wallet may contain larger amount of assets and its address could be tied to long-term contracts, therefore delegation to another controlling account is preferred to simply transferring the assets.

### What is library on masterchain?

Library is a special code storage mechanism that allows to reduce storage cost for a new Wallet V5 contract instance. Wallet V5 contract code is stored into a masterchain library.
When wallet contract is being deployed, original code hash is being used as the contract code.
Library contract itself data and code are empty cells. That leads to the inability to change the library code, delete the contract, or withdraw funds from it.
Therefore, any Wallet V5 user can top up the library contract balance if they are afraid that the library code of their wallet will be frozen.

## TL-B definitions

Action types:

```tl-b
// Standard actions from block.tlb:
out_list_empty$_ = OutList 0;
out_list$_ {n:#} prev:^(OutList n) action:OutAction
= OutList (n + 1);
action_send_msg#0ec3c86d mode:(## 8)
out_msg:^(MessageRelaxed Any) = OutAction;
action_set_code#ad4de08e new_code:^Cell = OutAction;
action_reserve_currency#36e6b809 mode:(## 8)
currency:CurrencyCollection = OutAction;
libref_hash$0 lib_hash:bits256 = LibRef;
libref_ref$1 library:^Cell = LibRef;
action_change_library#26fa1dd4 mode:(## 7) { mode <= 2 }
libref:LibRef = OutAction;

// Extended actions in W5:
action_list_basic$0 {n:#} actions:^(OutList n) = ActionList n 0;
action_list_extended$1 {m:#} {n:#} action:ExtendedAction prev:^(ActionList n m) = ActionList n (m+1);

action_set_data#1ff8ea0b data:^Cell = ExtendedAction;
action_add_ext#1c40db9f addr:MsgAddressInt = ExtendedAction;
action_delete_ext#5eaef4a4 addr:MsgAddressInt = ExtendedAction;
```

Authentication modes:

```tl-b
signed_request$_
signature: bits512 // 512
subwallet_id: uint32 // 512+32
valid_until: uint32 // 512+32+32
msg_seqno: uint32 // 512+32+32+32 = 608
inner: InnerRequest = SignedRequest;

internal_signed#7369676E signed:SignedRequest = InternalMsgBody;
internal_extension#6578746E inner:InnerRequest = InternalMsgBody;
external_signed#7369676E signed:SignedRequest = ExternalMsgBody;

actions$_ {m:#} {n:#} actions:(ActionList n m) = InnerRequest;
```

Contract state:
```tl-b
wallet_id$_ global_id:int32 wc:int8 version:(## 8) subwallet_number:(## 32) = WalletID;
contract_state$_ seqno:# wallet_id:WalletID public_key:(## 256) extensions_dict:(HashmapE 256 int8) = ContractState;
```
See `types.tlb`.

## Source code

See [contracts/wallet_v5_3.fc](contracts/wallet_v5_3.fc).
See [contracts/wallet_v5.fc](contracts/wallet_v5.fc).
1 change: 1 addition & 0 deletions build/library-deployer.compiled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"hash":"7b886902938fda7f8ee72fe31ee250744bf82489579c0b8e502105a49cb72e2a","hashBase64":"e4hpApOP2n+O5y/jHuJQdEv4JIlXnAuOUCEFpJy3Lio=","hex":"b5ee9c72410106010030000114ff00f4a413f4bcf2c80b0102012002050202d1030400053c006000193b511cbec1b232483ec13b55200006f2f0014136d496"}
1 change: 1 addition & 0 deletions build/wallet_v5.compiled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"hash":"20834b7b72b112147e1b2fb457b84e74d1a30f04f737d4f62a668e9552d2b72f","hashBase64":"IINLe3KxEhR+Gy+0V7hOdNGjDwT3N9T2KmaOlVLSty8=","hex":"b5ee9c7241021401000281000114ff00f4a413f4bcf2c80b01020120020d020148030402dcd020d749c120915b8f6320d70b1f2082106578746ebd21821073696e74bdb0925f03e082106578746eba8eb48020d72101d074d721fa4030fa44f828fa443058bd915be0ed44d0810141d721f4058307f40e6fa1319130e18040d721707fdb3ce03120d749810280b99130e070e2100f020120050c020120060902016e07080019adce76a2684020eb90eb85ffc00019af1df6a2684010eb90eb858fc00201480a0b0017b325fb51341c75c875c2c7e00011b262fb513435c280200019be5f0f6a2684080a0eb90fa02c0102f20e011e20d70b1f82107369676ebaf2e08a7f0f01e68ef0eda2edfb218308d722028308d723208020d721d31fd31fd31fed44d0d200d31f20d31fd3ffd70a000af90140ccf9109a28945f0adb31e1f2c087df02b35007b0f2d0845125baf2e0855036baf2e086f823bbf2d0882292f800de01a47fc8ca00cb1f01cf16c9ed542092f80fde70db3cd81003f6eda2edfb02f404216e926c218e4c0221d73930709421c700b38e2d01d72820761e436c20d749c008f2e09320d74ac002f2e09320d71d06c712c2005230b0f2d089d74cd7393001a4e86c128407bbf2e093d74ac000f2e093ed55e2d20001c000915be0ebd72c08142091709601d72c081c12e25210b1e30f20d74a111213009601fa4001fa44f828fa443058baf2e091ed44d0810141d718f405049d7fc8ca0040048307f453f2e08b8e14038307f45bf2e08c22d70a00216e01b3b0f2d090e2c85003cf1612f400c9ed54007230d72c08248e2d21f2e092d200ed44d0d2005113baf2d08f54503091319c01810140d721d70a00f2e08ee2c8ca0058cf16c9ed5493f2c08de20010935bdb31e1d74cd0b4d6c35e"}
Binary file added contest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading