mirror of
https://github.com/sparrowwallet/hummingbird.git
synced 2024-11-02 18:46:45 +00:00
fix cbor exception exposure and improve crypto-psbt type handling
This commit is contained in:
parent
d56dae6980
commit
4b012d1dce
3 changed files with 28 additions and 13 deletions
|
@ -39,8 +39,9 @@ Encoding a UR:
|
||||||
```java
|
```java
|
||||||
final int MIN_FRAGMENT_LENGTH = 10;
|
final int MIN_FRAGMENT_LENGTH = 10;
|
||||||
final int MAX_FRAGMENT_LENGTH = 100;
|
final int MAX_FRAGMENT_LENGTH = 100;
|
||||||
|
String type = UR.BYTES_TYPE;
|
||||||
|
|
||||||
UR ur = UR.fromBytes(data);
|
UR ur = UR.fromBytes(type, data);
|
||||||
UREncoder encoder = new UREncoder(ur, MAX_FRAGMENT_LENGTH, MIN_FRAGMENT_LENGTH, 0);
|
UREncoder encoder = new UREncoder(ur, MAX_FRAGMENT_LENGTH, MIN_FRAGMENT_LENGTH, 0);
|
||||||
while(true) {
|
while(true) {
|
||||||
String fragment = encoder.nextPart();
|
String fragment = encoder.nextPart();
|
||||||
|
|
|
@ -16,7 +16,7 @@ apply plugin: 'com.bmuschko.nexus'
|
||||||
|
|
||||||
archivesBaseName = 'hummingbird'
|
archivesBaseName = 'hummingbird'
|
||||||
group 'com.sparrowwallet'
|
group 'com.sparrowwallet'
|
||||||
version '1.1'
|
version '1.2'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|
|
@ -41,14 +41,18 @@ public class UR {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] toBytes() throws InvalidTypeException, CborException {
|
public byte[] toBytes() throws InvalidCBORException {
|
||||||
if(!BYTES_TYPE.equals(getType())) {
|
try {
|
||||||
throw new InvalidTypeException("Not a " + BYTES_TYPE + " type");
|
ByteArrayInputStream bais = new ByteArrayInputStream(getCbor());
|
||||||
}
|
List<DataItem> dataItems = new CborDecoder(bais).decode();
|
||||||
|
if(!(dataItems.get(0) instanceof ByteString)) {
|
||||||
|
throw new IllegalArgumentException("First element of CBOR is not a byte string");
|
||||||
|
}
|
||||||
|
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(getCbor());
|
return ((ByteString)dataItems.get(0)).getBytes();
|
||||||
List<DataItem> dataItems = new CborDecoder(bais).decode();
|
} catch(CborException e) {
|
||||||
return ((ByteString)dataItems.get(0)).getBytes();
|
throw new InvalidCBORException(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isURType(String type) {
|
public static boolean isURType(String type) {
|
||||||
|
@ -67,7 +71,11 @@ public class UR {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UR fromBytes(byte[] data) {
|
public static UR fromBytes(byte[] data) throws InvalidTypeException, InvalidCBORException {
|
||||||
|
return fromBytes(BYTES_TYPE, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UR fromBytes(String type, byte[] data) throws InvalidTypeException, InvalidCBORException {
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
new CborEncoder(baos).encode(new CborBuilder()
|
new CborEncoder(baos).encode(new CborBuilder()
|
||||||
|
@ -75,9 +83,9 @@ public class UR {
|
||||||
.build());
|
.build());
|
||||||
byte[] cbor = baos.toByteArray();
|
byte[] cbor = baos.toByteArray();
|
||||||
|
|
||||||
return new UR("bytes", cbor);
|
return new UR(type, cbor);
|
||||||
} catch(InvalidTypeException | CborException e) {
|
} catch(CborException e) {
|
||||||
return null;
|
throw new InvalidCBORException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,4 +142,10 @@ public class UR {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class InvalidCBORException extends URException {
|
||||||
|
public InvalidCBORException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue