-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_test.go
More file actions
76 lines (65 loc) · 1.84 KB
/
Copy pathmodule_test.go
File metadata and controls
76 lines (65 loc) · 1.84 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
package ethgo
import (
"bytes"
"encoding/hex"
"testing"
"github.com/umbracle/ethgo/wallet"
)
func TestHexToAddress(t *testing.T) {
m := &Module{}
input := "0x5FbDB2315678afecb367f032d93F642f64180aa3"
expected := "0x5fbdb2315678afecb367f032d93f642f64180aa3"
got := m.HexToAddress(input)
if got != expected {
t.Errorf("HexToAddress(%s) = %s; want %s", input, got, expected)
}
}
func TestPrivateKeyToAddress(t *testing.T) {
// Generate a deterministic 32-byte private key (all bytes = 0x01)
pkBytes := bytes.Repeat([]byte{1}, 32)
pkHex := hex.EncodeToString(pkBytes)
m := &Module{}
got, err := m.PrivateKeyToAddress(pkHex)
if err != nil {
t.Fatalf("PrivateKeyToAddress error: %v", err)
}
// Derive expected address via direct wallet call
w, err := wallet.NewWalletFromPrivKey(pkBytes)
if err != nil {
t.Fatalf("wallet.NewWalletFromPrivKey error: %v", err)
}
expected := w.Address().String()
if got != expected {
t.Errorf("PrivateKeyToAddress returned %s; want %s", got, expected)
}
}
func TestSignLegacyTx(t *testing.T) {
// Sample transaction parameters
tx := map[string]interface{}{
"to": "0x5fbdb2315678afecb367f032d93f642f64180aa3",
"value": 1000000000,
"chainId": 50312,
"gas": 6 * 1e9,
}
// Same deterministic private key as above
pkBytes := bytes.Repeat([]byte{1}, 32)
pkHex := hex.EncodeToString(pkBytes)
m := &Module{}
rawHex, err := m.SignLegacyTx(tx, pkHex)
if err != nil {
t.Fatalf("SignLegacyTx error: %v", err)
}
// Should start with 0x
if len(rawHex) < 3 || rawHex[:2] != "0x" {
t.Errorf("signed tx hex missing prefix '0x': %s", rawHex)
}
// Hex decode should succeed
rawBytes, err := hex.DecodeString(rawHex[2:])
if err != nil {
t.Errorf("failed to decode signed tx hex: %v", err)
}
if len(rawBytes) == 0 {
t.Error("decoded tx bytes is empty")
}
t.Logf("raw bytes: %s", rawHex)
}