func (pi *PInput) deserialize(r io.Reader) error {
	for {
		keyint, keydata, err := getKey(r)
		if err != nil {
			return err
		}
		if keyint == -1 {
			// Reached separator byte
			break
		}
		value, err := wire.ReadVarBytes(
			r, 0, MaxPsbtValueLength, "PSBT value",
		)
		if err != nil {
			return err
		}

		switch InputType(keyint) {

		case NonWitnessUtxoType:
			if pi.NonWitnessUtxo != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			tx := wire.NewMsgTx(2)

			err := tx.Deserialize(bytes.NewReader(value))
			if err != nil {
				return err
			}
			pi.NonWitnessUtxo = tx

		case WitnessUtxoType:
			if pi.WitnessUtxo != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			txout, err := readTxOut(value)
			if err != nil {
				return err
			}
			pi.WitnessUtxo = txout

		case PartialSigType:
			newPartialSig := PartialSig{
				PubKey:    keydata,
				Signature: value,
			}

			if !newPartialSig.checkValid() {
				return ErrInvalidPsbtFormat
			}

			// Duplicate keys are not allowed
			for _, x := range pi.PartialSigs {
				if bytes.Equal(x.PubKey, newPartialSig.PubKey) {
					return ErrDuplicateKey
				}
			}

			pi.PartialSigs = append(pi.PartialSigs, &newPartialSig)

		case SighashType:
			if pi.SighashType != 0 {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}

			// Bounds check on value here since the sighash type must be a
			// 32-bit unsigned integer.
			if len(value) != 4 {
				return ErrInvalidKeydata
			}

			shtype := txscript.SigHashType(
				binary.LittleEndian.Uint32(value),
			)
			pi.SighashType = shtype

		case RedeemScriptInputType:
			if pi.RedeemScript != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			pi.RedeemScript = value

		case WitnessScriptInputType:
			if pi.WitnessScript != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			pi.WitnessScript = value

		case Bip32DerivationInputType:
			if !validatePubkey(keydata) {
				return ErrInvalidPsbtFormat
			}
			master, derivationPath, err := readBip32Derivation(value)
			if err != nil {
				return err
			}

			// Duplicate keys are not allowed
			for _, x := range pi.Bip32Derivation {
				if bytes.Equal(x.PubKey, keydata) {
					return ErrDuplicateKey
				}
			}

			pi.Bip32Derivation = append(
				pi.Bip32Derivation,
				&Bip32Derivation{
					PubKey:               keydata,
					MasterKeyFingerprint: master,
					Bip32Path:            derivationPath,
				},
			)

		case FinalScriptSigType:
			if pi.FinalScriptSig != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}

			pi.FinalScriptSig = value

		case FinalScriptWitnessType:
			if pi.FinalScriptWitness != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}

			pi.FinalScriptWitness = value

		default:
			// A fall through case for any proprietary types.
			keyintanddata := []byte{byte(keyint)}
			keyintanddata = append(keyintanddata, keydata...)
			newUnknown := &Unknown{
				Key:   keyintanddata,
				Value: value,
			}

			// Duplicate key+keydata are not allowed
			for _, x := range pi.Unknowns {
				if bytes.Equal(x.Key, newUnknown.Key) &&
					bytes.Equal(x.Value, newUnknown.Value) {
					return ErrDuplicateKey
				}
			}

			pi.Unknowns = append(pi.Unknowns, newUnknown)
		}
	}

	return nil
}
// deserialize attempts to deserialize a new PInput from the passed io.Reader.
deserialize attempts to deserialize a new PInput from the passed io.Reader.
// deserialize attempts to deserialize a new PInput from the passed io . Reader .
func (pi *PInput) deserialize(r io.Reader) error {
	for {
		keyint, keydata, err := getKey(r)
		if err != nil {
			return err
		}
		if keyint == -1 {
			break
		}
		value, err := wire.ReadVarBytes(
			r, 0, MaxPsbtValueLength, "PSBT value",
		)
		if err != nil {
			return err
		}
		switch InputType(keyint) {
		case NonWitnessUtxoType:
			if pi.NonWitnessUtxo != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			tx := wire.NewMsgTx(2)
			err := tx.Deserialize(bytes.NewReader(value))
			if err != nil {
				return err
			}
			pi.NonWitnessUtxo = tx
		case WitnessUtxoType:
			if pi.WitnessUtxo != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			txout, err := readTxOut(value)
			if err != nil {
				return err
			}
			pi.WitnessUtxo = txout
		case PartialSigType:
			newPartialSig := PartialSig{
				PubKey:    keydata,
				Signature: value,
			}
			if !newPartialSig.checkValid() {
				return ErrInvalidPsbtFormat
			}
			for _, x := range pi.PartialSigs {
				if bytes.Equal(x.PubKey, newPartialSig.PubKey) {
					return ErrDuplicateKey
				}
			}
			pi.PartialSigs = append(pi.PartialSigs, &newPartialSig)
		case SighashType:
			if pi.SighashType != 0 {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			if len(value) != 4 {
				return ErrInvalidKeydata
			}
			shtype := txscript.SigHashType(
				binary.LittleEndian.Uint32(value),
			)
			pi.SighashType = shtype
		case RedeemScriptInputType:
			if pi.RedeemScript != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			pi.RedeemScript = value
		case WitnessScriptInputType:
			if pi.WitnessScript != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			pi.WitnessScript = value
		case Bip32DerivationInputType:
			if !validatePubkey(keydata) {
				return ErrInvalidPsbtFormat
			}
			master, derivationPath, err := readBip32Derivation(value)
			if err != nil {
				return err
			}
			for _, x := range pi.Bip32Derivation {
				if bytes.Equal(x.PubKey, keydata) {
					return ErrDuplicateKey
				}
			}
			pi.Bip32Derivation = append(
				pi.Bip32Derivation,
				&Bip32Derivation{
					PubKey:               keydata,
					MasterKeyFingerprint: master,
					Bip32Path:            derivationPath,
				},
			)
		case FinalScriptSigType:
			if pi.FinalScriptSig != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			pi.FinalScriptSig = value
		case FinalScriptWitnessType:
			if pi.FinalScriptWitness != nil {
				return ErrDuplicateKey
			}
			if keydata != nil {
				return ErrInvalidKeydata
			}
			pi.FinalScriptWitness = value
		default:
			keyintanddata := []byte{byte(keyint)}
			keyintanddata = append(keyintanddata, keydata...)
			newUnknown := &Unknown{
				Key:   keyintanddata,
				Value: value,
			}
			for _, x := range pi.Unknowns {
				if bytes.Equal(x.Key, newUnknown.Key) &&
					bytes.Equal(x.Value, newUnknown.Value) {
					return ErrDuplicateKey
				}
			}
			pi.Unknowns = append(pi.Unknowns, newUnknown)
		}
	}
	return nil
}
func ( pi * PInput ) deserialize ( r io . Reader ) error { for { keyint , keydata , err := getKey ( r ) if err != nil { return err } if keyint == - 1 { break } value , err := wire . ReadVarBytes ( r , 0 , MaxPsbtValueLength , " " , ) if err != nil { return err } switch InputType ( keyint ) { case NonWitnessUtxoType : if pi . NonWitnessUtxo != nil { return ErrDuplicateKey } if keydata != nil { return ErrInvalidKeydata } tx := wire . NewMsgTx ( 2 ) err := tx . Deserialize ( bytes . NewReader ( value ) ) if err != nil { return err } pi . NonWitnessUtxo = tx case WitnessUtxoType : if pi . WitnessUtxo != nil { return ErrDuplicateKey } if keydata != nil { return ErrInvalidKeydata } txout , err := readTxOut ( value ) if err != nil { return err } pi . WitnessUtxo = txout case PartialSigType : newPartialSig := PartialSig { PubKey : keydata , Signature : value , } if ! newPartialSig . checkValid ( ) { return ErrInvalidPsbtFormat } for _ , x := range pi . PartialSigs { if bytes . Equal ( x . PubKey , newPartialSig . PubKey ) { return ErrDuplicateKey } } pi . PartialSigs = append ( pi . PartialSigs , & newPartialSig ) case SighashType : if pi . SighashType != 0 { return ErrDuplicateKey } if keydata != nil { return ErrInvalidKeydata } if len ( value ) != 4 { return ErrInvalidKeydata } shtype := txscript . SigHashType ( binary . LittleEndian . Uint32 ( value ) , ) pi . SighashType = shtype case RedeemScriptInputType : if pi . RedeemScript != nil { return ErrDuplicateKey } if keydata != nil { return ErrInvalidKeydata } pi . RedeemScript = value case WitnessScriptInputType : if pi . WitnessScript != nil { return ErrDuplicateKey } if keydata != nil { return ErrInvalidKeydata } pi . WitnessScript = value case Bip32DerivationInputType : if ! validatePubkey ( keydata ) { return ErrInvalidPsbtFormat } master , derivationPath , err := readBip32Derivation ( value ) if err != nil { return err } for _ , x := range pi . Bip32Derivation { if bytes . Equal ( x . PubKey , keydata ) { return ErrDuplicateKey } } pi . Bip32Derivation = append ( pi . Bip32Derivation , & Bip32Derivation { PubKey : keydata , MasterKeyFingerprint : master , Bip32Path : derivationPath , } , ) case FinalScriptSigType : if pi . FinalScriptSig != nil { return ErrDuplicateKey } if keydata != nil { return ErrInvalidKeydata } pi . FinalScriptSig = value case FinalScriptWitnessType : if pi . FinalScriptWitness != nil { return ErrDuplicateKey } if keydata != nil { return ErrInvalidKeydata } pi . FinalScriptWitness = value default : keyintanddata := [ ] byte { byte ( keyint ) } keyintanddata = append ( keyintanddata , keydata ... ) newUnknown := & Unknown { Key : keyintanddata , Value : value , } for _ , x := range pi . Unknowns { if bytes . Equal ( x . Key , newUnknown . Key ) && bytes . Equal ( x . Value , newUnknown . Value ) { return ErrDuplicateKey } } pi . Unknowns = append ( pi . Unknowns , newUnknown ) } } return nil }
deserialize attempts to deserialize a new PInput from the passed io.Reader.
deserialize attempts to deserialize a new PInput from the passed io . Reader .
// Reached separator byte // Duplicate keys are not allowed // Bounds check on value here since the sighash type must be a // 32-bit unsigned integer. // Duplicate keys are not allowed // A fall through case for any proprietary types. // Duplicate key+keydata are not allowed