π CURRENT WALLET SYSTEM - COMPLETE ANALYSIS
Date: October 6, 2025
Purpose: Document what’s already implemented before starting Task 0.1
π EXECUTIVE SUMMARY
β GOOD NEWS: Much More Implemented Than Expected!
The wallet system has THREE parallel implementations:
- β TON Wallet Generator v4R2 - FULLY WORKING (using tonutils-go)
- β οΈ Placeholder Signers - Returns “not implemented” (in ton_tron_placeholders.go)
- π§ Trust Wallet Core Integration - Framework ready (via build tags)
Key Finding: We DON’T need to build TON from scratch! We need to: - Integrate existing TON generator into the Signer interface - Add TRON signer implementation - Optionally enable Trust Wallet Core for multi-chain support
ποΈ SYSTEM ARCHITECTURE (Current State)
Wallet Service Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββ
β Wallet Service (gRPC/HTTP) β
β Port: 9092 β
ββββββββββββ¬ββββββββββββββββββββββββββββ
β
ββββββββββββββββΌβββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββ ββββββββββββββ ββββββββββββββββ
β Wallet β β Wallet Rootβ β Signer β
βRepository β β Repository β β Factory β
βββββββββββββ ββββββββββββββ ββββββββ¬ββββββββ
β
βββββββββββββββββΌβββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββββ ββββββββββββββ ββββββββββββββββ
β TON Wallet β βPlaceholder β β Trust Wallet β
β Gen v4R2 β β Signers β β Core (TWC) β
β β β β β β
β β
WORKING β β β οΈ BROKEN β β π§ OPTIONAL β
ββββββββββββββββ ββββββββββββββ ββββββββββββββββ
β WHAT’S ALREADY IMPLEMENTED
1. TON Wallet Generator v4R2 (COMPLETE & WORKING)
File: services/wallet/internal/wallet/domain/service/ton_wallet_generator_v4r2.go
Status: β FULLY FUNCTIONAL
Features:
β
BIP39 24-word mnemonic generation (via tonutils-go)
β
ed25519 keypair generation
β
v4R2 wallet contract address generation
β
Bounceable/non-bounceable address forms
β
Address validation
β
Workchain 0 (mainnet) support
β
Mnemonic encryption (AES-256-GCM + PBKDF2)
β
Address derivation from existing mnemonic
β
Integration with wallet service
Key Methods:
- GenerateWallet(telegramID, label, isDefault) β Creates wallet + root
- DeriveAddressFromMnemonic(mnemonic) β Derives address
- ValidateTONAddress(address) β Validates TON address
- GetAddressForms(address) β Returns bounceable/non-bounceable
- EncryptData(data) / DecryptData(encrypted) β Secure storage
Current Usage:
// In wallet.service.go line 827+
func (w walletService) deriveAddress(ctx context.Context, telegramId int64,
network string, derivationPath string) {
if network == "TON" {
// β
Uses TON Wallet Generator v4R2
mnemonicStr, _ := w.TonWalletGen.DecryptData(encryptedMnemonic)
address, _ := w.TonWalletGen.DeriveAddressFromMnemonic(mnemonicStr)
// ... generates proper TON addresses
}
}
Dependencies:
github.com/xssnick/tonutils-go v1.14.1 β
Already in go.mod
golang.org/x/crypto v0.39.0 β
Already in go.mod
Test Status:
- β
Unit tests exist (ton_wallet_generator_test.go)
- β
Generates valid EQ…/UQ… addresses
- β
Encryption/decryption working
2. Signer Interface & Factory (ARCHITECTURE READY)
Files:
- services/wallet/internal/wallet/service/signer/interfaces.go
- services/wallet/internal/wallet/service/signer/factory.go
Status: β WELL-DESIGNED ARCHITECTURE
Interface Definition:
type Signer interface {
Address(pub PubKey, chain Chain) (string, error)
SignTx(chain Chain, priv PrivKey, payload []byte) ([]byte, error)
}
type Deriver interface {
FromMnemonic(m string, path string, curve Curve) (PrivKey, PubKey, error)
}
Factory Pattern:
func NewSigner(chain Chain) Signer {
enableTWC := os.Getenv("ENABLE_TWC_PLUGIN") == "true"
if enableTWC {
return newTWCSafe() // Trust Wallet Core (optional)
}
switch chain {
case Chain("TON"):
return &tonSigner{} // β οΈ Currently returns "not implemented"
case Chain("TRON"):
return &tronSigner{} // β οΈ Currently returns "not implemented"
}
}
Design Principles: - β Clean interface segregation - β Factory pattern for chain selection - β Build tag support for optional TWC - β Environment variable toggle
3. Trust Wallet Core Integration (FRAMEWORK READY)
Files:
- services/wallet/internal/wallet/service/signer/twc/twc_signer_enabled.go
- services/wallet/internal/wallet/service/signer/twc/twc_signer_disabled.go
- services/wallet/internal/wallet/service/signer/twc_enabled_factory.go
- services/wallet/internal/wallet/service/signer/twc_disabled_factory.go
Status: π§ BUILD TAG INFRASTRUCTURE COMPLETE
Build Tags System:
// twc_enabled_factory.go
//go:build twc
func twcFactory() Signer { return twcimpl.Enabled{} }
// twc_disabled_factory.go
//go:build !twc
func twcFactory() Signer { return nil }
CGO Setup (in twc_signer_enabled.go):
/*
#cgo CFLAGS: -I${SRCDIR}/wallet-core/include
#cgo LDFLAGS: -L${SRCDIR}/wallet-core/build -lTrustWalletCore ...
#include <TrustWalletCore/TWHDWallet.h>
*/
import "C"
Status: - β Build tag infrastructure complete - β CGO configuration ready - β οΈ Implementation stub (returns “not implemented”) - β οΈ Trust Wallet Core library not built yet
Usage:
# Build without TWC (default)
go build ./cmd/server
# Build with TWC (optional)
go build -tags twc ./cmd/server
# Enable at runtime
export ENABLE_TWC_PLUGIN=true
4. Additional Signer Implementation (PARTIAL)
File: services/wallet/internal/wallet/service/signer/signer.go
Status: β οΈ PARTIAL IMPLEMENTATION
What Exists:
β
TONSigner struct with ed25519 support
β
NewTONSigner(privateKeyBytes) β Creates signer from key
β
NewTONSignerFromSeed(seed) β Creates signer from seed
β
GenerateKeyPair() β Generates new keypair
β
Sign(message) β Signs with ed25519
β
Verify(message, signature) β Verifies signature
β
SignTransaction(tx) β Signs TON transaction
β οΈ GenerateTONAddress() β SIMPLIFIED (not proper v4R2)
Issue with signer.go:
// Line 108 - SIMPLIFIED ADDRESS GENERATION
func GenerateTONAddress(publicKey []byte, bounceable bool) (string, error) {
hash := sha256.Sum256(publicKey) // β οΈ TOO SIMPLE
if bounceable {
hash[0] |= 0x40
}
encoded := base64.URLEncoding.EncodeToString(hash[:])
return "EQ" + encoded[:len(encoded)-2], nil
}
Problem: This does NOT generate proper v4R2 wallet addresses!
Solution: Use the TON Wallet Generator v4R2 instead (already working).
5. HD Wallet Support (BASIC)
File: packages/crypto/crypto.go
Status: β οΈ PLACEHOLDER IMPLEMENTATION
What Exists:
β οΈ HDWallet struct
β οΈ NewHDWallet(mnemonic, passphrase)
β οΈ DerivePath(path) β BIP44 derivation
β οΈ BIP44 coin types defined (TON: 396, TRON: 195, ETH: 60)
β οΈ GenerateDerivationPath(network, account, change, index)
Issues:
// Line 292 - PLACEHOLDER HMAC
func hmacSHA512(key, data []byte) []byte {
// Placeholder: β οΈ WRONG
hash := sha512.Sum512(append(key, data...))
return hash[:]
}
// Line 306 - PLACEHOLDER KEY DERIVATION
func privateToPublic(privateKey []byte) ([]byte, error) {
// Placeholder: β οΈ WRONG
hash := sha256.Sum256(privateKey)
return hash[:], nil
}
Recommendation: Don’t use packages/crypto/ HD wallet. Use proper libraries:
- TON: tonutils-go (already working in TON Wallet Gen v4R2)
- TRON: go-ethereum for secp256k1 + gotron-sdk
6. Placeholder Signers (THE PROBLEM)
File: services/wallet/internal/wallet/service/signer/ton_tron_placeholders.go
Status: β BROKEN - RETURNS “NOT IMPLEMENTED”
type tonSigner struct{}
func (t *tonSigner) Address(pub PubKey, chain Chain) (string, error) {
return "", errors.New("ton signer not implemented") // β
}
func (t *tonSigner) SignTx(chain Chain, priv PrivKey, payload []byte) ([]byte, error) {
return nil, errors.New("ton signer not implemented") // β
}
type tronSigner struct{}
func (t *tronSigner) Address(pub PubKey, chain Chain) (string, error) {
return "", errors.New("tron signer not implemented") // β
}
func (t *tronSigner) SignTx(chain Chain, priv PrivKey, payload []byte) ([]byte, error) {
return nil, errors.New("tron signer not implemented") // β
}
This is Task 0.1 and 0.2!
π― CURRENT WALLET FLOW (Working)
Deposit Address Generation (TON) β
User requests TON wallet
β
WalletService.EnsureDefaultWallet()
β
TONWalletGeneratorV4R2.GenerateWallet()
β
1. Generate 24-word mnemonic (tonutils-go)
2. Derive ed25519 keypair
3. Generate v4R2 address (proper TON address)
4. Encrypt mnemonic (AES-256-GCM)
5. Store in database:
- WalletRoot (encrypted mnemonic)
- Wallet (address, public key, network)
β
Return deposit address (EQ... format) β
Withdrawal Flow β οΈ INCOMPLETE
User requests withdrawal
β
Need to sign transaction
β
factory.NewSigner("TON")
β
Returns &tonSigner{} from placeholders
β
Calls SignTx()
β
ERROR: "ton signer not implemented" β
Problem: The Signer interface is used for withdrawals, but it returns errors!
π₯ THE DISCONNECT
Why Two Implementations?
TON Wallet Generator v4R2 (working):
- Purpose: Deposit wallet generation
- Used by:
WalletService.EnsureDefaultWallet() - Status: β Fully functional
- Creates: Proper TON v4R2 addresses
Signer Interface (broken placeholders):
- Purpose: Transaction signing (withdrawals)
- Used by: Withdrawal flows, transaction processing
- Status: β Returns “not implemented”
- Should: Sign transactions with private keys
The Gap:
βββββββββββββββββββββββββββββββββββββββββββ
β DEPOSITS: β
Working β
β Uses: TON Wallet Gen v4R2 β
β Generates proper addresses β
βββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββ
β WITHDRAWALS: β Broken β
β Uses: Signer interface β
β Returns: "not implemented" β
βββββββββββββββββββββββββββββββββββββββββββ
π TASK 0.1 - WHAT WE ACTUALLY NEED TO DO
DON’T:
- β Reimplement TON wallet generation (already works!)
- β Rewrite mnemonic generation (tonutils-go works!)
- β Rewrite address derivation (v4R2 generator works!)
DO:
- β Bridge the gap between TON Wallet Gen and Signer interface
- β
Implement
tonSigner.Address()using TON Wallet Gen v4R2 logic - β
Implement
tonSigner.SignTx()using tonutils-go signing - β Make Signer interface use the same libraries as TON Wallet Gen
Revised Implementation Plan:
// ton_tron_placeholders.go (replace)
import (
"crypto/ed25519"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/ton/wallet"
)
type tonSigner struct{}
func (t *tonSigner) Address(pub PubKey, chain Chain) (string, error) {
// β
Use same logic as TON Wallet Gen v4R2
publicKey := ed25519.PublicKey(pub)
rawAddr := address.NewAddress(0, 0, publicKey)
// Return bounceable form (user-facing)
return rawAddr.Bounce(true).String(), nil
}
func (t *tonSigner) SignTx(chain Chain, priv PrivKey, payload []byte) ([]byte, error) {
// β
Use tonutils-go for proper TON transaction signing
privateKey := ed25519.PrivateKey(priv)
// Sign using ed25519
signature := ed25519.Sign(privateKey, payload)
return signature, nil
}
Key Insight: We’re not starting from scratch! We’re integrating existing working code.
π RECOMMENDED APPROACH FOR TASK 0.1
Step 1: Understand What’s Working
- β
Read
ton_wallet_generator_v4r2.go(lines 33-104) - β See how tonutils-go is used
- β Understand address generation logic
Step 2: Copy the Working Logic
- β Take address generation from TON Wallet Gen v4R2
- β Use same library calls
- β
Put into
tonSigner.Address()
Step 3: Add Transaction Signing
- β
Use
ed25519.Sign()(standard library) - β Or use tonutils-go transaction building
- β
Put into
tonSigner.SignTx()
Step 4: Test Integration
- β Generate address with TON Wallet Gen v4R2
- β Sign transaction with tonSigner
- β Verify both use same keypair format
π DEPENDENCIES STATUS
β
github.com/xssnick/tonutils-go v1.14.1 Already in go.mod
β
golang.org/x/crypto v0.39.0 Already in go.mod
β github.com/fbsobreira/gotron-sdk Need to add (Task 0.2)
β github.com/ethereum/go-ethereum Need to add (Task 0.2)
π― SUCCESS CRITERIA (Revised)
Task 0.1 Complete When:
- β
tonSigner.Address()generates same addresses as TON Wallet Gen v4R2 - β
tonSigner.SignTx()signs transactions properly - β Integration tests pass
- β Can withdraw TON (end-to-end)
Don’t Reinvent:
- β Don’t rewrite mnemonic generation
- β Don’t rewrite v4R2 address logic
- β Don’t create new TON libraries
Do Integrate:
- β Use existing tonutils-go functions
- β Match TON Wallet Gen v4R2 behavior
- β Keep same address format
π NOTES FOR IMPLEMENTATION
Key Files to Reference:
ton_wallet_generator_v4r2.go(lines 33-104) - Working address generationwallet.service.go(lines 827-873) - How it’s currently usedinterfaces.go- Signer interface definition
Libraries Already Available:
github.com/xssnick/tonutils-go/addressβgithub.com/xssnick/tonutils-go/ton/walletβcrypto/ed25519β
Don’t Touch (Working):
- TON Wallet Generator v4R2 implementation
- Mnemonic encryption/decryption
- Database repositories
Do Modify:
ton_tron_placeholders.go- Replace placeholders- Add tests for signing
- Integrate with withdrawal flow
π LESSON LEARNED
Before starting ANY task: 1. β Explore what already exists 2. β Find working implementations 3. β Reuse and integrate, don’t rewrite 4. β Understand the disconnect
In this case: - We have working TON wallet generation - We have broken transaction signing - We just need to bridge them!
Next Step: Start Task 0.1 with this understanding.
Time Estimate: 2-3 hours (down from 4-6) because we’re integrating, not building from scratch.