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 }
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) } }
func makeWIF() { privKey := "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D" wif := bitcoin.ComputeWIF("BTC", privKey, true) Info("WIF: %v", wif) addr := bitcoin.ComputeAddressForPrivKey("BTC", privKey) Info("addr: %v", addr) pubKey := bitcoin.PubKeyBytesFromPrivKeyBytes(hexDecode(privKey), false) Info("pubKey: %v", hexEncode(pubKey)) addr = bitcoin.AddrFromPubKeyBytes("BTC", pubKey) Info("addr2: %v", addr) message := "this is a message" signature := bitcoin.SignMessage(privKey, message, true) Info("signature: %v", signature) }