118 lines
3.9 KiB
Go
118 lines
3.9 KiB
Go
package tron
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"testing"
|
|
)
|
|
|
|
// Golden vectors copied verbatim from the repository fixtures (t2, t3).
|
|
func TestMnemonicDerivationGoldenVectors(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
mnemonic string
|
|
priv string
|
|
pub string
|
|
addr string
|
|
hexAddr string
|
|
}{
|
|
{
|
|
name: "t3",
|
|
mnemonic: "cloth print knife public box polar impose kingdom phone favorite rose lounge",
|
|
priv: "a96135266ba58bb5b3a450b8fe7a9d59ea3e6cb0cdb2300e8138d2d2d48e61a8",
|
|
pub: "076e3db946fcb53d575c4e742a9f8ff8c4b57ded53776b2a80d13022e1647adaa1641f3e2210f5f6a3bdd73d073ac029ba79d576093d8a92dd148e448a8e63cb",
|
|
addr: "TG9vH8c2jBWq2wSnksrq5mVyq2wjhG4A1n",
|
|
hexAddr: "4143d75ee7cdcd7f2fcd4fc3b4eb574b6bdc2a3fcb",
|
|
},
|
|
{
|
|
name: "t2",
|
|
mnemonic: "bullet tissue frozen garment immune valley anxiety nurse vicious caution divert latin",
|
|
priv: "3252a4cb9b3865c94dbb7d6f50107ca704c1c2b9e3d1eb81753e221c7db8fa25",
|
|
pub: "ede91cec53b797aa5951b707703a2b20150b97f642abc214289c935ee97ce9959d52acc45ccc5b7907f9a9bdb7f2bc2071789c91ea477e4a2485c92c8e18fb17",
|
|
addr: "TUSQ1u3HKUsTpfsi5uRXScUzES9RN1w8kk",
|
|
hexAddr: "41ca96b08266d7b36857d8cf2712d449ea07bf0275",
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
key, err := DeriveFromMnemonic(tc.mnemonic, "", "")
|
|
if err != nil {
|
|
t.Fatalf("DeriveFromMnemonic: %v", err)
|
|
}
|
|
if key.Hex() != tc.priv {
|
|
t.Fatalf("private key = %s, want %s", key.Hex(), tc.priv)
|
|
}
|
|
if key.Pub.Hex() != tc.pub {
|
|
t.Fatalf("public key = %s, want %s", key.Pub.Hex(), tc.pub)
|
|
}
|
|
if got := key.Pub.ToBase58CheckAddress(); got != tc.addr {
|
|
t.Fatalf("address = %s, want %s", got, tc.addr)
|
|
}
|
|
if got := key.Pub.ToHexAddress(); got != tc.hexAddr {
|
|
t.Fatalf("hex address = %s, want %s", got, tc.hexAddr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAddressFromPrivateKey checks the pk3 fixture (t3 private key).
|
|
func TestAddressFromPrivateKey(t *testing.T) {
|
|
pk := "a96135266ba58bb5b3a450b8fe7a9d59ea3e6cb0cdb2300e8138d2d2d48e61a8"
|
|
key, err := PrivateKeyFromHex(pk)
|
|
if err != nil {
|
|
t.Fatalf("PrivateKeyFromHex: %v", err)
|
|
}
|
|
if got := key.Pub.ToBase58CheckAddress(); got != "TG9vH8c2jBWq2wSnksrq5mVyq2wjhG4A1n" {
|
|
t.Fatalf("address = %s", got)
|
|
}
|
|
}
|
|
|
|
func TestAddressConversions(t *testing.T) {
|
|
b58 := "TG9vH8c2jBWq2wSnksrq5mVyq2wjhG4A1n"
|
|
hexAddr := "4143d75ee7cdcd7f2fcd4fc3b4eb574b6bdc2a3fcb"
|
|
|
|
got, err := ToBase58CheckAddress(hexAddr)
|
|
if err != nil || got != b58 {
|
|
t.Fatalf("ToBase58CheckAddress(%s) = %s, %v", hexAddr, got, err)
|
|
}
|
|
gotHex, err := ToHexAddress(b58)
|
|
if err != nil || gotHex != hexAddr {
|
|
t.Fatalf("ToHexAddress(%s) = %s, %v", b58, gotHex, err)
|
|
}
|
|
raw, err := ToRawAddress(b58)
|
|
if err != nil || len(raw) != 21 || raw[0] != 0x41 {
|
|
t.Fatalf("ToRawAddress(%s) = %x, %v", b58, raw, err)
|
|
}
|
|
if !IsAddress(b58) || !IsAddress(hexAddr) {
|
|
t.Fatalf("IsAddress should accept both formats")
|
|
}
|
|
if IsAddress("TNotARealAddress000000000000000000") {
|
|
t.Fatalf("IsAddress accepted an invalid address")
|
|
}
|
|
}
|
|
|
|
func TestSignRecoverRoundTrip(t *testing.T) {
|
|
pk, _ := hex.DecodeString("a96135266ba58bb5b3a450b8fe7a9d59ea3e6cb0cdb2300e8138d2d2d48e61a8")
|
|
key, err := NewPrivateKey(pk)
|
|
if err != nil {
|
|
t.Fatalf("NewPrivateKey: %v", err)
|
|
}
|
|
hash := make([]byte, 32)
|
|
for i := range hash {
|
|
hash[i] = byte(i)
|
|
}
|
|
sigBytes, err := key.Sign(hash)
|
|
if err != nil {
|
|
t.Fatalf("Sign: %v", err)
|
|
}
|
|
if len(sigBytes) != 65 {
|
|
t.Fatalf("signature length = %d, want 65", len(sigBytes))
|
|
}
|
|
rec, err := RecoverAddress(hash, sigBytes)
|
|
if err != nil {
|
|
t.Fatalf("RecoverAddress: %v", err)
|
|
}
|
|
if rec != key.Pub.ToBase58CheckAddress() {
|
|
t.Fatalf("recovered %s, want %s", rec, key.Pub.ToBase58CheckAddress())
|
|
}
|
|
}
|