Exemple #1
0
//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

}
Exemple #2
0
//IsTestnet returns true if addr is for testnet.
func IsTestnet(addr string) (bool, error) {
	bytes, _, err := base58check.Decode(addr)
	if err != nil {
		return false, err
	}

	switch bytes[0] {
	case 0x6f:
		return true, nil
	case 0x00:
		return false, nil
	default:
		return false, errors.New("invalid address")
	}

}
Exemple #3
0
//CreateStandardScriptPubkey creates standard script pubkey .
func createP2PKHScriptPubkey(publicKeyBase58 string) ([]byte, error) {
	publicKeyBytes, _, err := base58check.Decode(publicKeyBase58)
	if err != nil {
		return nil, err
	}
	publicKeyBytes = publicKeyBytes[1:]

	var scriptPubKey bytes.Buffer
	scriptPubKey.WriteByte(opDUP)
	scriptPubKey.WriteByte(opHASH160)
	scriptPubKey.WriteByte(byte(len(publicKeyBytes))) //PUSH
	scriptPubKey.Write(publicKeyBytes)
	scriptPubKey.WriteByte(opEQUALVERIFY)
	scriptPubKey.WriteByte(opCHECKSIG)
	script := scriptPubKey.Bytes()
	return script, nil
}