-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevgenesis_test.go
More file actions
154 lines (123 loc) · 4.23 KB
/
devgenesis_test.go
File metadata and controls
154 lines (123 loc) · 4.23 KB
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
// Copyright (C) 2026, Zoo Labs Foundation. All rights reserved.
// See the file LICENSE for licensing terms.
package main
import (
"encoding/hex"
"fmt"
"os"
"testing"
"github.com/luxfi/crypto/secp256k1"
)
func TestLoadDevKey_Mnemonic(t *testing.T) {
// Zoo test mnemonic (NOT production — unit tests only)
mnemonic := "light light light light light light light light light light light energy"
t.Setenv("MNEMONIC", mnemonic)
t.Setenv("PRIVATE_KEY", "") // ensure priority works
key, err := loadDevKey()
if err != nil {
t.Fatalf("loadDevKey failed: %v", err)
}
secpAddr := key.Address()
ethAddr := secp256k1.PubkeyToAddress(key.ToECDSA().PublicKey)
// The secp256k1 address and ETH address MUST differ for the same key.
// This is the core bug: if they're the same, the fix is wrong.
secpHex := hex.EncodeToString(secpAddr[:])
ethHex := hex.EncodeToString(ethAddr[:])
if secpHex == ethHex {
t.Fatalf("secp256k1 and ETH addresses must differ for the same key\n secp: %s\n eth: %s", secpHex, ethHex)
}
t.Logf("secp256k1 addr (P-Chain): %s", secpHex)
t.Logf("ETH addr (C-Chain): 0x%s", ethHex)
}
func TestLoadDevKey_PrivateKey(t *testing.T) {
// Use LIGHT_MNEMONIC for all tests
t.Setenv("LIGHT_MNEMONIC", "light light light light light light light light light light light energy")
t.Setenv("MNEMONIC", "")
t.Setenv("PRIVATE_KEY", "")
key, err := loadDevKey()
if err != nil {
t.Fatalf("loadDevKey failed: %v", err)
}
secpAddr := key.Address()
ethAddr := secp256k1.PubkeyToAddress(key.ToECDSA().PublicKey)
// Verify the ETH address is non-empty
ethHex := hex.EncodeToString(ethAddr[:])
if ethHex == "0000000000000000000000000000000000000000" {
t.Fatal("ETH address is zero")
}
t.Logf("ETH addr from LIGHT_MNEMONIC: 0x%s", ethHex)
// secp256k1 address must be different
secpHex := hex.EncodeToString(secpAddr[:])
if secpHex == ethHex {
t.Fatal("secp256k1 and ETH addresses must differ")
}
t.Logf("secp256k1 addr (P-Chain): %s", secpHex)
t.Logf("ETH addr (C-Chain): 0x%s", ethHex)
}
func TestLoadDevKey_NoEnv(t *testing.T) {
t.Setenv("MNEMONIC", "")
t.Setenv("PRIVATE_KEY", "")
t.Setenv("LIGHT_MNEMONIC", "")
_, err := loadDevKey()
if err == nil {
t.Fatal("expected error when no env vars set")
}
}
func TestLoadDevKey_Priority(t *testing.T) {
// When both are set, LUX_MNEMONIC should take priority
mnemonic := "light light light light light light light light light light light energy"
privHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
t.Setenv("MNEMONIC", mnemonic)
t.Setenv("PRIVATE_KEY", privHex)
key, err := loadDevKey()
if err != nil {
t.Fatalf("loadDevKey failed: %v", err)
}
// Verify it used the mnemonic, not the private key
ethAddr := secp256k1.PubkeyToAddress(key.ToECDSA().PublicKey)
ethHex := hex.EncodeToString(ethAddr[:])
// Anvil #0 address -- if we get this, mnemonic was NOT used
anvilAddr := "f39fd6e51aad88f6f4ce6ab8827279cfffb92266"
if ethHex == anvilAddr {
t.Fatal("LUX_MNEMONIC should take priority over LUX_PRIVATE_KEY")
}
t.Logf("mnemonic key ETH addr: 0x%s (not anvil #0: 0x%s)", ethHex, anvilAddr)
}
func TestDevCChainGenesis(t *testing.T) {
var addr [20]byte
copy(addr[:], []byte{0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB})
genesis := devCChainGenesis(addr)
// Verify the address appears in the genesis
addrHex := fmt.Sprintf("%x", addr)
if len(genesis) == 0 {
t.Fatal("empty genesis")
}
// The address should appear in the alloc section
if !contains(genesis, addrHex) {
t.Fatalf("genesis does not contain address %s", addrHex)
}
// Verify standard test accounts are also present
testAccounts := []string{
"f39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"70997970C51812dc3A010C7d01b50e0d17dc79C8",
}
for _, acct := range testAccounts {
if !contains(genesis, acct) {
t.Fatalf("genesis does not contain test account %s", acct)
}
}
}
func contains(s, substr string) bool {
return len(s) > 0 && len(substr) > 0 && stringContains(s, substr)
}
func stringContains(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
// Ensure test imports are used
var _ = os.Getenv