go port
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package tron
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestSignatureMatchesPython locks the hand-rolled RFC6979/low-s recoverable
|
||||
// signing to values produced by coincurve/libsecp256k1 via tronpy's
|
||||
// PrivateKey.sign_msg_hash. The hashes are the txids computed elsewhere in the
|
||||
// suite.
|
||||
func TestSignatureMatchesPython(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
priv string
|
||||
hash string
|
||||
want string
|
||||
addr string
|
||||
}{
|
||||
{
|
||||
"t3_transfer_txid",
|
||||
"a96135266ba58bb5b3a450b8fe7a9d59ea3e6cb0cdb2300e8138d2d2d48e61a8",
|
||||
"e93945efde601eed484553338e58218f4e81c51261dfda72db2d874f75378fe8",
|
||||
"a48ffafc8bdb57bdb64216b0facdff77cd2bb7090e1e1b7dcd6e829823c7cd207422b7bd910a063b96f562e32dc566b3b6a1c935d7c63c01642dda14b0b4966d00",
|
||||
"TG9vH8c2jBWq2wSnksrq5mVyq2wjhG4A1n",
|
||||
},
|
||||
{
|
||||
"t3_sparse_hash",
|
||||
"a96135266ba58bb5b3a450b8fe7a9d59ea3e6cb0cdb2300e8138d2d2d48e61a8",
|
||||
"0000000000000000000000000000000000000000000000000000000000000001",
|
||||
"2df95cd58fc299c155dd51e13799c42c00786b1d18a1423f9b098f8354f4785e670d6a75c0fea96162a990689a9ee0089e08bd9e663dfe27104902a0bdf6a04f01",
|
||||
"TG9vH8c2jBWq2wSnksrq5mVyq2wjhG4A1n",
|
||||
},
|
||||
{
|
||||
"t2_trigger_txid",
|
||||
"3252a4cb9b3865c94dbb7d6f50107ca704c1c2b9e3d1eb81753e221c7db8fa25",
|
||||
"0c1dec1fa73590ddf4a3975b78791e72d18998ae42751af199384587c2ef12d0",
|
||||
"3136e1e010697c23c3aa7ba78b3eee06a3fbb75d32e37c0730f2b2a733ed5da6191f93d706b0c6c18d480d0f27cfd208bf64ce157ee83f6ff6082fdd230d0cab01",
|
||||
"TUSQ1u3HKUsTpfsi5uRXScUzES9RN1w8kk",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
key, err := PrivateKeyFromHex(tc.priv)
|
||||
if err != nil {
|
||||
t.Fatalf("PrivateKeyFromHex: %v", err)
|
||||
}
|
||||
h, err := hex.DecodeString(tc.hash)
|
||||
if err != nil {
|
||||
t.Fatalf("decode hash: %v", err)
|
||||
}
|
||||
sig, err := key.Sign(h)
|
||||
if err != nil {
|
||||
t.Fatalf("Sign: %v", err)
|
||||
}
|
||||
if got := hex.EncodeToString(sig); got != tc.want {
|
||||
t.Fatalf("signature = %s, want %s", got, tc.want)
|
||||
}
|
||||
rec, err := RecoverAddress(h, sig)
|
||||
if err != nil {
|
||||
t.Fatalf("RecoverAddress: %v", err)
|
||||
}
|
||||
if rec != tc.addr {
|
||||
t.Fatalf("recovered = %s, want %s", rec, tc.addr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user