예제 #1
0
파일: keys.go 프로젝트: utamaro/gocoin
//GenerateKey generates random PublicKey and PrivateKey.
func GenerateKey(flagTestnet bool) (*Key, error) {
	seed := make([]byte, 32)
	_, err := rand.Read(seed)
	if err != nil {
		return nil, err
	}
	s256 := btcec.S256()

	private := PrivateKey{}
	private.isTestnet = flagTestnet
	public := PublicKey{}
	public.isTestnet = flagTestnet
	private.key, public.key = btcec.PrivKeyFromBytes(s256, seed)
	key := Key{
		Pub:  &public,
		Priv: &private,
	}

	//Print the keys
	logging.Println("Your private key in WIF is")
	logging.Println(private.GetWIFAddress())

	logging.Println("Your address is")
	logging.Println(public.GetAddress())

	return &key, nil
}
예제 #2
0
파일: keys.go 프로젝트: utamaro/gocoin
//GetKeyFromWIF gets PublicKey and PrivateKey from private key of WIF format.
func GetKeyFromWIF(wif string) (*Key, error) {
	secp256k1 := btcec.S256()
	privateKeyBytes, isCmpressed, err := base58check.Decode(wif)
	if err != nil {
		return nil, err
	}

	pub := PublicKey{}
	priv := PrivateKey{}
	key := Key{
		Pub:  &pub,
		Priv: &priv,
	}
	switch privateKeyBytes[0] {
	case 0xef:
		pub.isTestnet = true
		priv.isTestnet = true
	case 0x80:
		pub.isTestnet = false
		priv.isTestnet = false
	default:
		return nil, errors.New("cannot determin net param from private key")
	}
	pub.isCompressed = isCmpressed

	//Get the raw public
	priv.key, pub.key = btcec.PrivKeyFromBytes(secp256k1, privateKeyBytes[1:])

	return &key, nil

}