/**
* @Author zxl
* @Date 2022/6/28 17:19
* @Desc
**/
package test
import (
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enode"
"testing"
)
func TestGenerateKey(t *testing.T) {
key, err := crypto.GenerateKey()
if err != nil {
t.Fatalf("failed GenerateKey with %s.", err)
}
fmt.Println("private key have 0x \n", hexutil.Encode(crypto.FromECDSA(key)))
fmt.Println("private key no 0x \n", hex.EncodeToString(crypto.FromECDSA(key)))
if err := crypto.SaveECDSA("privatekey", key); err != nil {
log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
}
fmt.Println("public key have 0x \n", hexutil.Encode(crypto.FromECDSAPub(&key.PublicKey)))
fmt.Println("public key no 0x \n", hex.EncodeToString(crypto.FromECDSAPub(&key.PublicKey)))
//由私钥字符串转换私钥
acc1Key, _ := crypto.HexToECDSA("1d1f9bf41b660a3f38e478f1f24935692c502b0b09d7040a6a056d89545f194b")
//puk:="041a3bf7bd5767ba6914a7c0f1db503c897d97166e17a5b16c7ceb3e0cfba4765e02b46d72dc76d58da9738f0a73ff5aa37c627fbfd5ea496016a95218f23d7f9a"
address1 := crypto.PubkeyToAddress(acc1Key.PublicKey)
fmt.Println("address ", address1.String())
cusAddr := common.HexToAddress("0xd17F7A5Ae36EfaF77D2192569346539Ba2498c98")
fmt.Println("cusAddr", cusAddr.String())
//字节转地址
addr3 := common.BytesToAddress([]byte("ethereum"))
fmt.Println("address ", addr3.String())
//字节转hash
hash1 := common.BytesToHash([]byte("topic1"))
fmt.Println("hash ", hash1.String())
var testAddrHex = "0xd17F7A5Ae36EfaF77D2192569346539Ba2498c98"
var testPrivHex = "1d1f9bf41b660a3f38e478f1f24935692c502b0b09d7040a6a056d89545f194b"
key1, _ := crypto.HexToECDSA(testPrivHex)
addrtest := common.HexToAddress(testAddrHex)
msg := crypto.Keccak256([]byte("foo"))
sig, err := crypto.Sign(msg, key1)
recoveredPub, err := crypto.Ecrecover(msg, sig)
pubKey, _ := crypto.UnmarshalPubkey(recoveredPub)
fmt.Println("public key ", hex.EncodeToString(crypto.FromECDSAPub(pubKey)))
recoveredAddr := crypto.PubkeyToAddress(*pubKey)
// should be equal to SigToPub
recoveredPub2, _ := crypto.SigToPub(msg, sig)
recoveredAddr2 := crypto.PubkeyToAddress(*recoveredPub2)
fmt.Println("addrtest ", addrtest.String())
fmt.Println("recoveredAddr ", recoveredAddr.String())
fmt.Println("recoveredAddr2 ", recoveredAddr2.String())
}
func TestNodeKey(t *testing.T) {
var testPrivHex = "a6bb92963e145d495e7f5924186017d3f0a45ecd320bd1c5164fbf8a74ddfe8f"
key1, _ := crypto.HexToECDSA(testPrivHex)
keyAddr := crypto.PubkeyToAddress(key1.PublicKey)
fmt.Println("address:", keyAddr)
pubBytes := crypto.FromECDSAPub(&key1.PublicKey)
fmt.Println("public key 65:", hex.EncodeToString(pubBytes[:]))
fmt.Println("public key 64:", hex.EncodeToString(pubBytes[1:]))
node := enode.NewV4(&key1.PublicKey, nil, 0, 0)
fmt.Println("enode url:", node.URLv4())
}
// 2022-06-28
func TestKeyEncryptDecrypt_zxl(t *testing.T) {
// 测试环境node3的key
keyjson := []byte("{\"address\":\"987b3ab29466a8a1080263fa4eae59fbc72c5cb2\",\"crypto\":{\"cipher\":\"aes-128-ctr\",\"ciphertext\":\"0e4f38916ad836b3cccf746fa45f04cc9a9669e876bb750e7da6ae7c4fbf163a\",\"cipherparams\":{\"iv\":\"1838ec9815d4445270eccd9588ed7871\"},\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"n\":262144,\"p\":1,\"r\":8,\"salt\":\"7d01d455be63ae2e0299ebe0b4c9949e9608a1e3b2df8562e0a9a9ecb8a2fee4\"},\"mac\":\"e06a89fd1009a126ccc7e4b566c2359e37b623497a2e714fe1e811c023a24cf9\"},\"id\":\"adba1b1a-464f-4b6c-b5d5-47c5f9fc8c78\",\"version\":3}")
password := "123456"
//address := common.HexToAddress("45dea0fb0bba44f4fcf290bba71fd57d7117cbb8")
// Decrypt with the correct password
key, err := keystore.DecryptKey(keyjson, password)
if err != nil {
t.Fatalf("test %d: json key failed to decrypt: %v", 0, err)
}
t.Log(key.Address.Hex())
ecdsakey := crypto.FromECDSA(key.PrivateKey)
t.Log("pk:", hex.EncodeToString(ecdsakey))
keyAddr := crypto.PubkeyToAddress(key.PrivateKey.PublicKey)
t.Log("address:", keyAddr)
pubBytes := crypto.FromECDSAPub(&key.PrivateKey.PublicKey)
pubByte1 := pubBytes[1:]
//
//
hashk := crypto.Keccak256(pubByte1)
pubByte12 := hashk[12:]
//
t.Log("addr:", common.BytesToAddress(pubByte12))
//t.Log("addr", common.BytesToAddress(crypto.Keccak256(pubBytes[1:])[12:]))
hexPk := hex.EncodeToString(pubBytes[1:])
t.Log("public key:", hexPk)
//if key.Address != address {
// t.Errorf("test %d: key address mismatch: have %x, want %x", 0, key.Address, address)
//}
}
func TestPublicKeyToAddress(t *testing.T) {
publicKey, err := hex.DecodeString("cb44308ef0fa056244f1ce2562f8ba58efc4530f5cc9e570c4c55f79d07f3db85ecb0d1b69fcd5c6f649113f28fd0a597e036132f4781daa45cbd7eb388213dd00")
if err != nil {
t.Fatal(err)
}
pubkey, err := crypto.UnmarshalPubkey(publicKey)
if err != nil {
t.Fatal(err)
}
keyAddr := crypto.PubkeyToAddress(*pubkey)
t.Log("address:", keyAddr)
}
网友评论