From 784d92ba4741c5a84b11e8930061ab7394fa1ab5 Mon Sep 17 00:00:00 2001 From: Jorge Rego Date: Tue, 11 Oct 2022 21:50:55 +0200 Subject: [PATCH] feat(Crypto-sskr): added crypto-sskr UR support Added tests --- build.gradle | 2 +- .../com/sparrowwallet/hummingbird/UR.java | 2 + .../hummingbird/registry/CryptoSskr.java | 34 +++++++++++++++++ .../hummingbird/registry/RegistryType.java | 2 +- .../hummingbird/registry/CryptoSskrTest.java | 37 +++++++++++++++++++ 5 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/hummingbird/registry/CryptoSskr.java create mode 100644 src/test/java/com/sparrowwallet/hummingbird/registry/CryptoSskrTest.java diff --git a/build.gradle b/build.gradle index 70bdf7f..53fe069 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ apply plugin: 'com.bmuschko.nexus' archivesBaseName = 'hummingbird' group 'com.sparrowwallet' -version '1.6.5' +version '1.6.6' repositories { mavenCentral() diff --git a/src/main/java/com/sparrowwallet/hummingbird/UR.java b/src/main/java/com/sparrowwallet/hummingbird/UR.java index 91e885f..dacfdb8 100644 --- a/src/main/java/com/sparrowwallet/hummingbird/UR.java +++ b/src/main/java/com/sparrowwallet/hummingbird/UR.java @@ -76,6 +76,8 @@ public class UR { return CryptoPSBT.fromCbor(item); } else if(registryType == RegistryType.CRYPTO_ACCOUNT) { return CryptoAccount.fromCbor(item); + } else if(registryType == RegistryType.CRYPTO_SSKR) { + return CryptoSskr.fromCbor(item); } } catch(CborException e) { throw new InvalidCBORException(e.getMessage()); diff --git a/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoSskr.java b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoSskr.java new file mode 100644 index 0000000..fa806b0 --- /dev/null +++ b/src/main/java/com/sparrowwallet/hummingbird/registry/CryptoSskr.java @@ -0,0 +1,34 @@ +package com.sparrowwallet.hummingbird.registry; + +import java.util.Arrays; + +import co.nstant.in.cbor.model.ByteString; +import co.nstant.in.cbor.model.DataItem; + +public class CryptoSskr extends RegistryItem { + + private final byte[] split; + + public CryptoSskr(byte[] split) { + this.split = split; + } + + public byte[] getSplit() { + return split; + } + + public DataItem toCbor() { + return new ByteString(split); + } + + @Override + public RegistryType getRegistryType() { + return RegistryType.CRYPTO_SSKR; + } + + public static CryptoSskr fromCbor(DataItem item) { + byte[] itemBytes = ((ByteString)item).getBytes(); + byte[] normalisedSplit = Arrays.copyOfRange(itemBytes, 1, itemBytes.length); + return new CryptoSskr(normalisedSplit); + } +} diff --git a/src/main/java/com/sparrowwallet/hummingbird/registry/RegistryType.java b/src/main/java/com/sparrowwallet/hummingbird/registry/RegistryType.java index 5e143a4..822a496 100644 --- a/src/main/java/com/sparrowwallet/hummingbird/registry/RegistryType.java +++ b/src/main/java/com/sparrowwallet/hummingbird/registry/RegistryType.java @@ -20,7 +20,7 @@ public enum RegistryType { CRYPTO_ECKEY("crypto-eckey", 306, CryptoECKey.class), CRYPTO_ADDRESS("crypto-address", 307, CryptoAddress.class), CRYPTO_OUTPUT("crypto-output", 308, CryptoOutput.class), - CRYPTO_SSKR("crypto-sskr", 309, null), + CRYPTO_SSKR("crypto-sskr", 309, CryptoSskr.class), CRYPTO_PSBT("crypto-psbt", 310, CryptoPSBT.class), CRYPTO_ACCOUNT("crypto-account", 311, CryptoAccount.class); diff --git a/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoSskrTest.java b/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoSskrTest.java new file mode 100644 index 0000000..38f3bb4 --- /dev/null +++ b/src/test/java/com/sparrowwallet/hummingbird/registry/CryptoSskrTest.java @@ -0,0 +1,37 @@ +package com.sparrowwallet.hummingbird.registry; + +import com.sparrowwallet.hummingbird.TestUtils; +import com.sparrowwallet.hummingbird.UR; +import com.sparrowwallet.hummingbird.URDecoder; + +import org.junit.Assert; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +import co.nstant.in.cbor.CborDecoder; +import co.nstant.in.cbor.CborException; +import co.nstant.in.cbor.model.ByteString; +import co.nstant.in.cbor.model.DataItem; +import co.nstant.in.cbor.model.UnicodeString; + +public class CryptoSskrTest { + + private final String splitVector = "4bbf1101025abd490ee65b6084859854ee67736e75"; + private final String urVector = "ur:crypto-sskr/gogrrsbyadaohtrygabavahphnlrlpmkghwyiojkjtkpmdkncfjp"; + + @Test + public void testSskrToUR() { + byte[] data = TestUtils.hexToBytes(splitVector); + CryptoSskr cryptoSskr = new CryptoSskr(data); + Assert.assertEquals(urVector, cryptoSskr.toUR().toString()); + } + + @Test + public void testURtoSskr() throws UR.URException { + UR ur = URDecoder.decode(urVector); + CryptoSskr cryptoSskr = CryptoSskr.fromCbor(new ByteString(ur.getCborBytes())); + Assert.assertEquals(splitVector, TestUtils.bytesToHex(cryptoSskr.getSplit())); + } +}