Search in sources :

Example 1 with VarInt

use of com.sparrowwallet.drongo.protocol.VarInt in project drongo by sparrowwallet.

the class PSBTEntry method parseTaprootKeyDerivation.

public static Map<KeyDerivation, List<Sha256Hash>> parseTaprootKeyDerivation(byte[] data) throws PSBTParseException {
    if (data.length < 1) {
        throw new PSBTParseException("Invalid taproot key derivation: no bytes");
    }
    VarInt varInt = new VarInt(data, 0);
    int offset = varInt.getOriginalSizeInBytes();
    if (data.length < offset + (varInt.value * 32)) {
        throw new PSBTParseException("Invalid taproot key derivation: not enough bytes for leaf hashes");
    }
    List<Sha256Hash> leafHashes = new ArrayList<>();
    for (int i = 0; i < varInt.value; i++) {
        leafHashes.add(Sha256Hash.wrap(Arrays.copyOfRange(data, offset + (i * 32), offset + (i * 32) + 32)));
    }
    KeyDerivation keyDerivation = parseKeyDerivation(Arrays.copyOfRange(data, offset + (leafHashes.size() * 32), data.length));
    return Map.of(keyDerivation, leafHashes);
}
Also used : VarInt(com.sparrowwallet.drongo.protocol.VarInt) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) Sha256Hash(com.sparrowwallet.drongo.protocol.Sha256Hash) ArrayList(java.util.ArrayList)

Example 2 with VarInt

use of com.sparrowwallet.drongo.protocol.VarInt in project drongo by sparrowwallet.

the class PSBTEntry method serializeTaprootKeyDerivation.

public static byte[] serializeTaprootKeyDerivation(List<Sha256Hash> leafHashes, KeyDerivation keyDerivation) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    VarInt hashesLen = new VarInt(leafHashes.size());
    baos.writeBytes(hashesLen.encode());
    for (Sha256Hash leafHash : leafHashes) {
        baos.writeBytes(leafHash.getBytes());
    }
    baos.writeBytes(serializeKeyDerivation(keyDerivation));
    return baos.toByteArray();
}
Also used : VarInt(com.sparrowwallet.drongo.protocol.VarInt) Sha256Hash(com.sparrowwallet.drongo.protocol.Sha256Hash) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Sha256Hash (com.sparrowwallet.drongo.protocol.Sha256Hash)2 VarInt (com.sparrowwallet.drongo.protocol.VarInt)2 KeyDerivation (com.sparrowwallet.drongo.KeyDerivation)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1