Example #1
0
func ComputePrivateKeyForAddress(addr *bitcoin.Address) string {
	mpk := bitcoin.LoadMPK(addr.MPKId)
	if mpk == nil {
		panic(NewError("Failed to find mpk %v", addr.MPKId))
	}
	masterPrivKey, _ := masterPrivKeys.Get(mpk.PubKey).(string)
	if masterPrivKey == "" {
		panic(NewError("Failed to find privkey for mpk %v", mpk.PubKey))
	}
	privKey := bitcoin.ComputePrivateKey(masterPrivKey, mpk.Chain, addr.ChainPath, addr.ChainIdx)
	derivedAddr := bitcoin.ComputeAddressForPrivKey(addr.Coin, privKey)
	if addr.Address != derivedAddr {
		panic(NewError("Computed invalid private key for address. addr: %v, derivedAddr: %v", addr.Address, derivedAddr))
	}
	return privKey
}
Example #2
0
func makeMasters() {
	pubKey, chain, mPrivKey := bitcoin.ComputeMastersFromSeed("this is a test")
	Info(pubKey, ", ", chain, ", ", mPrivKey)

	// Now create an address for path "0/1/1"
	privKey := bitcoin.ComputePrivateKey(mPrivKey, chain, "0/1", 1)
	addr := bitcoin.ComputeAddressForPrivKey("BTC", privKey)

	// See that the addr matches the other derivation
	addr2 := bitcoin.ComputeAddress("BTC", pubKey, chain, "0/1", 1)
	if addr == addr2 {
		Info("Address matches: %v", addr)
	} else {
		Warn("Address does not match: %v vs %v", addr, addr2)
	}
}