add tapsigner, minor refactoring

This commit is contained in:
Craig Raw 2023-01-25 09:20:37 +02:00
parent c642f7414a
commit 2168c56de9
4 changed files with 26 additions and 20 deletions

View file

@ -128,6 +128,20 @@ public class Utils {
return c;
}
public static byte[] xor(byte[] a, byte[] b) {
if(a.length != b.length) {
throw new IllegalArgumentException("Invalid length for xor: " + a.length + " vs " + b.length);
}
byte[] ret = new byte[a.length];
for(int i = 0; i < a.length; i++) {
ret[i] = (byte) ((int) b[i] ^ (int) a[i]);
}
return ret;
}
/** Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. */
public static long readUint32(byte[] bytes, int offset) {
return (bytes[offset] & 0xffl) |

View file

@ -20,6 +20,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static com.sparrowwallet.drongo.Utils.xor;
public class PaymentCode {
private static final Logger log = LoggerFactory.getLogger(PaymentCode.class);
@ -313,21 +315,6 @@ public class PaymentCode {
return HDKeyDerivation.createMasterPubKeyFromBytes(pubkey, chain);
}
private static byte[] xor(byte[] a, byte[] b) {
if(a.length != b.length) {
log.error("Invalid length for xor: " + a.length + " vs " + b.length);
return null;
}
byte[] ret = new byte[a.length];
for(int i = 0; i < a.length; i++) {
ret[i] = (byte) ((int) b[i] ^ (int) a[i]);
}
return ret;
}
public boolean isValid() {
try {
byte[] pcodeBytes = Base58.decodeChecked(strPaymentCode);

View file

@ -627,6 +627,14 @@ public class ECKey {
// This is what you get back from Bouncy Castle if base64 doesn't decode :(
throw new SignatureException("Could not decode base64", e);
}
byte[] messageBytes = formatMessageForSigning(message);
// Note that the C++ code doesn't actually seem to specify any character encoding. Presumably it's whatever
// JSON-SPIRIT hands back. Assume UTF-8 for now.
Sha256Hash messageHash = Sha256Hash.twiceOf(messageBytes);
return signedHashToKey(messageHash, signatureEncoded, electrumFormat);
}
public static ECKey signedHashToKey(Sha256Hash messageHash, byte[] signatureEncoded, boolean electrumFormat) throws SignatureException {
// Parse the signature bytes into r/s and the selector value.
if(signatureEncoded.length < 65) {
throw new SignatureException("Signature truncated, expected 65 bytes and got " + signatureEncoded.length);
@ -640,10 +648,7 @@ public class ECKey {
BigInteger r = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 1, 33));
BigInteger s = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 33, 65));
ECDSASignature sig = new ECDSASignature(r, s);
byte[] messageBytes = formatMessageForSigning(message);
// Note that the C++ code doesn't actually seem to specify any character encoding. Presumably it's whatever
// JSON-SPIRIT hands back. Assume UTF-8 for now.
Sha256Hash messageHash = Sha256Hash.twiceOf(messageBytes);
boolean compressed = false;
if(header >= 39) { // this is a bech32 signature
header -= 12;

View file

@ -3,7 +3,7 @@ package com.sparrowwallet.drongo.wallet;
import java.util.Locale;
public enum WalletModel {
SEED, SPARROW, BITCOIN_CORE, ELECTRUM, TREZOR_1, TREZOR_T, COLDCARD, LEDGER_NANO_S, LEDGER_NANO_X, DIGITALBITBOX_01, KEEPKEY, SPECTER_DESKTOP, COBO_VAULT, BITBOX_02, SPECTER_DIY, PASSPORT, BLUE_WALLET, KEYSTONE, SEEDSIGNER, CARAVAN, GORDIAN_SEED_TOOL, JADE, LEDGER_NANO_S_PLUS, EPS;
SEED, SPARROW, BITCOIN_CORE, ELECTRUM, TREZOR_1, TREZOR_T, COLDCARD, LEDGER_NANO_S, LEDGER_NANO_X, DIGITALBITBOX_01, KEEPKEY, SPECTER_DESKTOP, COBO_VAULT, BITBOX_02, SPECTER_DIY, PASSPORT, BLUE_WALLET, KEYSTONE, SEEDSIGNER, CARAVAN, GORDIAN_SEED_TOOL, JADE, LEDGER_NANO_S_PLUS, EPS, TAPSIGNER;
public static WalletModel getModel(String model) {
return valueOf(model.toUpperCase(Locale.ROOT));