func TestBinaryDecode(t *testing.T) { privAccount := GenPrivAccount() pubKey := privAccount.PubKey privKey := privAccount.PrivKey msg := CRandBytes(128) sig := privKey.Sign(msg) t.Logf("msg: %X, sig: %X", msg, sig) buf, n, err := new(bytes.Buffer), new(int64), new(error) wire.WriteBinary(sig, buf, n, err) if *err != nil { t.Fatalf("Failed to write Signature: %v", err) } if len(buf.Bytes()) != ed25519.SignatureSize+1 { // 1 byte TypeByte, 64 bytes signature bytes t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes())) } if buf.Bytes()[0] != SignatureTypeEd25519 { t.Fatalf("Unexpected signature type byte") } sig2, ok := wire.ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519) if !ok || *err != nil { t.Fatalf("Failed to read Signature: %v", err) } // Test the signature if !pubKey.VerifyBytes(msg, sig2) { t.Errorf("Account message signature verification failed") } }
// General Convenience func SimpleHashFromBinary(item interface{}) []byte { hasher, n, err := ripemd160.New(), new(int64), new(error) wire.WriteBinary(item, hasher, n, err) if *err != nil { PanicCrisis(err) } return hasher.Sum(nil) }
// TODO: Slicing the array gives us length prefixing but loses the type byte. // Revisit if we add more pubkey types. // For now, we artificially append the type byte in front to give us backwards // compatibility for when the pubkey wasn't fixed length array func (pubKey PubKeyEd25519) Address() []byte { w, n, err := new(bytes.Buffer), new(int64), new(error) wire.WriteBinary(pubKey[:], w, n, err) if *err != nil { PanicCrisis(*err) } // append type byte encodedPubkey := append([]byte{1}, w.Bytes()...) hasher := ripemd160.New() hasher.Write(encodedPubkey) // does not error return hasher.Sum(nil) }
func (kv KVPair) Hash() []byte { hasher, n, err := ripemd160.New(), new(int64), new(error) wire.WriteString(kv.Key, hasher, n, err) if kvH, ok := kv.Value.(Hashable); ok { wire.WriteByteSlice(kvH.Hash(), hasher, n, err) } else { wire.WriteBinary(kv.Value, hasher, n, err) } if *err != nil { PanicSanity(*err) } return hasher.Sum(nil) }
func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) { wire.WriteBinary(o.(*ValidatorInfo), w, n, err) }
func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) { wire.WriteBinary(o.(*Validator), w, n, err) }
func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) { wire.WriteBinary(o.(*Account), w, n, err) }