handle psbt keytypes with values greater than single byte compact integers

This commit is contained in:
Craig Raw 2024-07-11 11:48:32 +02:00
parent 6b1a0bba95
commit d73d3439f6
2 changed files with 25 additions and 19 deletions

View file

@ -16,11 +16,11 @@ import java.util.Map;
public class PSBTEntry {
private final byte[] key;
private final byte keyType;
private final int keyType;
private final byte[] keyData;
private final byte[] data;
public PSBTEntry(byte[] key, byte keyType, byte[] keyData, byte[] data) {
public PSBTEntry(byte[] key, int keyType, byte[] keyData, byte[] data) {
this.key = key;
this.keyType = keyType;
this.keyData = keyData;
@ -39,12 +39,13 @@ public class PSBTEntry {
byte[] key = new byte[keyLen];
psbtByteBuffer.get(key);
byte keyType = key[0];
ByteBuffer keyBuf = ByteBuffer.wrap(key);
int keyType = readCompactInt(keyBuf);
byte[] keyData = null;
if (key.length > 1) {
keyData = new byte[key.length - 1];
System.arraycopy(key, 1, keyData, 0, keyData.length);
if(keyBuf.hasRemaining()) {
keyData = new byte[keyBuf.remaining()];
keyBuf.get(keyData);
}
int dataLen = readCompactInt(psbtByteBuffer);
@ -143,22 +144,20 @@ public class PSBTEntry {
return baos.toByteArray();
}
static PSBTEntry populateEntry(byte type, byte[] keydata, byte[] data) {
return new PSBTEntry(new byte[] {type}, type, keydata, data);
}
void serializeToStream(ByteArrayOutputStream baos) {
int keyLen = 1;
if(keyData != null) {
keyLen += keyData.length;
}
baos.writeBytes(writeCompactInt(keyLen));
baos.writeBytes(key);
static PSBTEntry populateEntry(int type, byte[] keyData, byte[] data) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1 + (keyData == null ? 0 : keyData.length));
baos.writeBytes(writeCompactInt(type));
if(keyData != null) {
baos.writeBytes(keyData);
}
return new PSBTEntry(baos.toByteArray(), type, keyData, data);
}
void serializeToStream(ByteArrayOutputStream baos) {
baos.writeBytes(writeCompactInt(key.length));
baos.writeBytes(key);
baos.writeBytes(writeCompactInt(data.length));
baos.writeBytes(data);
}
@ -167,7 +166,7 @@ public class PSBTEntry {
return key;
}
public byte getKeyType() {
public int getKeyType() {
return keyType;
}

View file

@ -313,6 +313,13 @@ public class PSBTTest {
Assertions.assertEquals(noWitnessDataPsbt, psbt4.toBase64String());
}
@Test
public void largeKeyType() throws PSBTParseException {
String psbt = "cHNidP8BAD8CAAAAAf//////////////////////////////////////////AAAAAAD/////AQAAAAAAAAAAA2oBAAAAAAAF/v//AAAAAAAA";
PSBT psbt1 = PSBT.fromString(psbt);
Assertions.assertEquals("cHNidP8BAD8CAAAAAf//////////////////////////////////////////AAAAAAD/////AQAAAAAAAAAAA2oBAAAAAAAAAAA=", psbt1.toBase64String());
}
@Test
public void creatorBip() throws PSBTParseException {
Transaction transaction = new Transaction();