Esempio n. 1
0
// generateAddr computes the associated bitcon address from the provided
// public key. We compute ripemd160(sha256(b)) of the pubkey and then
// shimmy the hashed bytes into btcsuite's AddressPubKeyHash type
func generateAddr(pub *btcec.PublicKey) *btcutil.AddressPubKeyHash {

	net := &btcnet.MainNetParams

	// Serialize the public key into bytes and then run ripemd160(sha256(b)) on it
	b := btcutil.Hash160(pub.SerializeCompressed())

	// Convert the hashed public key into the btcsuite type so that the library
	// will handle the base58 encoding when we call addr.String()
	addr, err := btcutil.NewAddressPubKeyHash(b, net)
	if err != nil {
		log.Fatal(err)
	}

	return addr
}
Esempio n. 2
0
// generateKeyPair generates and stores an ECDSA keypair to a file.
func generateKeyPair(filename string) error {
	// Generate keypairs.
	aKeypair, err := ecdsa.GenerateKey(btcec.S256(), crand.Reader)
	if err != nil {
		return err
	}
	pubkeyBtcec := btcec.PublicKey{aKeypair.PublicKey.Curve,
		aKeypair.PublicKey.X,
		aKeypair.PublicKey.Y}
	keypairBtcec := btcec.PrivateKey{aKeypair.PublicKey, aKeypair.D}

	// Create a map to json marshal
	keypairMap := make(map[string]string)
	keypairMap["pubkey"] = hex.EncodeToString(pubkeyBtcec.SerializeCompressed())
	keypairMap["privkey"] = hex.EncodeToString(keypairBtcec.Serialize())

	// Store the address in case anyone wants to use it for BTC
	pkh, err := btcutil.NewAddressPubKey(pubkeyBtcec.SerializeCompressed(),
		&btcnet.MainNetParams)
	if err != nil {
		return err
	}
	keypairMap["address"] = pkh.EncodeAddress()

	b, err := json.Marshal(keypairMap)
	if err != nil {
		return err
	}

	err = ioutil.WriteFile(filename, b, 0644)
	if err != nil {
		return err
	}

	return nil
}