func (pi *PInput) serialize(w io.Writer) error {

	if !pi.IsSane() {
		return ErrInvalidPsbtFormat
	}

	if pi.NonWitnessUtxo != nil {
		var buf bytes.Buffer
		err := pi.NonWitnessUtxo.Serialize(&buf)
		if err != nil {
			return err
		}

		err = serializeKVPairWithType(
			w, uint8(NonWitnessUtxoType), nil, buf.Bytes(),
		)
		if err != nil {
			return err
		}
	}
	if pi.WitnessUtxo != nil {
		var buf bytes.Buffer
		err := wire.WriteTxOut(&buf, 0, 0, pi.WitnessUtxo)
		if err != nil {
			return err
		}

		err = serializeKVPairWithType(
			w, uint8(WitnessUtxoType), nil, buf.Bytes(),
		)
		if err != nil {
			return err
		}
	}

	if pi.FinalScriptSig == nil && pi.FinalScriptWitness == nil {
		sort.Sort(PartialSigSorter(pi.PartialSigs))
		for _, ps := range pi.PartialSigs {
			err := serializeKVPairWithType(
				w, uint8(PartialSigType), ps.PubKey,
				ps.Signature,
			)
			if err != nil {
				return err
			}
		}

		if pi.SighashType != 0 {
			var shtBytes [4]byte
			binary.LittleEndian.PutUint32(
				shtBytes[:], uint32(pi.SighashType),
			)

			err := serializeKVPairWithType(
				w, uint8(SighashType), nil, shtBytes[:],
			)
			if err != nil {
				return err
			}
		}

		if pi.RedeemScript != nil {
			err := serializeKVPairWithType(
				w, uint8(RedeemScriptInputType), nil,
				pi.RedeemScript,
			)
			if err != nil {
				return err
			}
		}

		if pi.WitnessScript != nil {
			err := serializeKVPairWithType(
				w, uint8(WitnessScriptInputType), nil,
				pi.WitnessScript,
			)
			if err != nil {
				return err
			}
		}

		sort.Sort(Bip32Sorter(pi.Bip32Derivation))
		for _, kd := range pi.Bip32Derivation {
			err := serializeKVPairWithType(
				w,
				uint8(Bip32DerivationInputType), kd.PubKey,
				SerializeBIP32Derivation(
					kd.MasterKeyFingerprint, kd.Bip32Path,
				),
			)
			if err != nil {
				return err
			}
		}
	}

	if pi.FinalScriptSig != nil {
		err := serializeKVPairWithType(
			w, uint8(FinalScriptSigType), nil, pi.FinalScriptSig,
		)
		if err != nil {
			return err
		}
	}

	if pi.FinalScriptWitness != nil {
		err := serializeKVPairWithType(
			w, uint8(FinalScriptWitnessType), nil, pi.FinalScriptWitness,
		)
		if err != nil {
			return err
		}
	}

	// Unknown is a special case; we don't have a key type, only a key and
	// a value field
	for _, kv := range pi.Unknowns {
		err := serializeKVpair(w, kv.Key, kv.Value)
		if err != nil {
			return err
		}
	}

	return nil
}
// serialize attempts to serialize the target PInput into the passed io.Writer.
serialize attempts to serialize the target PInput into the passed io.Writer.
// serialize attempts to serialize the target PInput into the passed io . Writer .
func (pi *PInput) serialize(w io.Writer) error {
	if !pi.IsSane() {
		return ErrInvalidPsbtFormat
	}
	if pi.NonWitnessUtxo != nil {
		var buf bytes.Buffer
		err := pi.NonWitnessUtxo.Serialize(&buf)
		if err != nil {
			return err
		}
		err = serializeKVPairWithType(
			w, uint8(NonWitnessUtxoType), nil, buf.Bytes(),
		)
		if err != nil {
			return err
		}
	}
	if pi.WitnessUtxo != nil {
		var buf bytes.Buffer
		err := wire.WriteTxOut(&buf, 0, 0, pi.WitnessUtxo)
		if err != nil {
			return err
		}
		err = serializeKVPairWithType(
			w, uint8(WitnessUtxoType), nil, buf.Bytes(),
		)
		if err != nil {
			return err
		}
	}
	if pi.FinalScriptSig == nil && pi.FinalScriptWitness == nil {
		sort.Sort(PartialSigSorter(pi.PartialSigs))
		for _, ps := range pi.PartialSigs {
			err := serializeKVPairWithType(
				w, uint8(PartialSigType), ps.PubKey,
				ps.Signature,
			)
			if err != nil {
				return err
			}
		}
		if pi.SighashType != 0 {
			var shtBytes [4]byte
			binary.LittleEndian.PutUint32(
				shtBytes[:], uint32(pi.SighashType),
			)
			err := serializeKVPairWithType(
				w, uint8(SighashType), nil, shtBytes[:],
			)
			if err != nil {
				return err
			}
		}
		if pi.RedeemScript != nil {
			err := serializeKVPairWithType(
				w, uint8(RedeemScriptInputType), nil,
				pi.RedeemScript,
			)
			if err != nil {
				return err
			}
		}
		if pi.WitnessScript != nil {
			err := serializeKVPairWithType(
				w, uint8(WitnessScriptInputType), nil,
				pi.WitnessScript,
			)
			if err != nil {
				return err
			}
		}
		sort.Sort(Bip32Sorter(pi.Bip32Derivation))
		for _, kd := range pi.Bip32Derivation {
			err := serializeKVPairWithType(
				w,
				uint8(Bip32DerivationInputType), kd.PubKey,
				SerializeBIP32Derivation(
					kd.MasterKeyFingerprint, kd.Bip32Path,
				),
			)
			if err != nil {
				return err
			}
		}
	}
	if pi.FinalScriptSig != nil {
		err := serializeKVPairWithType(
			w, uint8(FinalScriptSigType), nil, pi.FinalScriptSig,
		)
		if err != nil {
			return err
		}
	}
	if pi.FinalScriptWitness != nil {
		err := serializeKVPairWithType(
			w, uint8(FinalScriptWitnessType), nil, pi.FinalScriptWitness,
		)
		if err != nil {
			return err
		}
	}
	for _, kv := range pi.Unknowns {
		err := serializeKVpair(w, kv.Key, kv.Value)
		if err != nil {
			return err
		}
	}
	return nil
}
func ( pi * PInput ) serialize ( w io . Writer ) error { if ! pi . IsSane ( ) { return ErrInvalidPsbtFormat } if pi . NonWitnessUtxo != nil { var buf bytes . Buffer err := pi . NonWitnessUtxo . Serialize ( & buf ) if err != nil { return err } err = serializeKVPairWithType ( w , uint8 ( NonWitnessUtxoType ) , nil , buf . Bytes ( ) , ) if err != nil { return err } } if pi . WitnessUtxo != nil { var buf bytes . Buffer err := wire . WriteTxOut ( & buf , 0 , 0 , pi . WitnessUtxo ) if err != nil { return err } err = serializeKVPairWithType ( w , uint8 ( WitnessUtxoType ) , nil , buf . Bytes ( ) , ) if err != nil { return err } } if pi . FinalScriptSig == nil && pi . FinalScriptWitness == nil { sort . Sort ( PartialSigSorter ( pi . PartialSigs ) ) for _ , ps := range pi . PartialSigs { err := serializeKVPairWithType ( w , uint8 ( PartialSigType ) , ps . PubKey , ps . Signature , ) if err != nil { return err } } if pi . SighashType != 0 { var shtBytes [ 4 ] byte binary . LittleEndian . PutUint32 ( shtBytes [ : ] , uint32 ( pi . SighashType ) , ) err := serializeKVPairWithType ( w , uint8 ( SighashType ) , nil , shtBytes [ : ] , ) if err != nil { return err } } if pi . RedeemScript != nil { err := serializeKVPairWithType ( w , uint8 ( RedeemScriptInputType ) , nil , pi . RedeemScript , ) if err != nil { return err } } if pi . WitnessScript != nil { err := serializeKVPairWithType ( w , uint8 ( WitnessScriptInputType ) , nil , pi . WitnessScript , ) if err != nil { return err } } sort . Sort ( Bip32Sorter ( pi . Bip32Derivation ) ) for _ , kd := range pi . Bip32Derivation { err := serializeKVPairWithType ( w , uint8 ( Bip32DerivationInputType ) , kd . PubKey , SerializeBIP32Derivation ( kd . MasterKeyFingerprint , kd . Bip32Path , ) , ) if err != nil { return err } } } if pi . FinalScriptSig != nil { err := serializeKVPairWithType ( w , uint8 ( FinalScriptSigType ) , nil , pi . FinalScriptSig , ) if err != nil { return err } } if pi . FinalScriptWitness != nil { err := serializeKVPairWithType ( w , uint8 ( FinalScriptWitnessType ) , nil , pi . FinalScriptWitness , ) if err != nil { return err } } for _ , kv := range pi . Unknowns { err := serializeKVpair ( w , kv . Key , kv . Value ) if err != nil { return err } } return nil }
serialize attempts to serialize the target PInput into the passed io.Writer.
serialize attempts to serialize the target PInput into the passed io . Writer .
// Unknown is a special case; we don't have a key type, only a key and // a value field