From 8f7f0d4c61025827f7d3bb163558871b643eab9b Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 16 Aug 2021 10:15:14 +0200 Subject: [PATCH] truncate labels over 255 chars before persisting to db --- .../sparrow/io/db/BlockTransactionDao.java | 8 ++++++-- .../sparrowwallet/sparrow/io/db/KeystoreDao.java | 6 +++++- .../sparrowwallet/sparrow/io/db/WalletDao.java | 6 +++++- .../sparrow/io/db/WalletNodeDao.java | 16 ++++++++++------ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/db/BlockTransactionDao.java b/src/main/java/com/sparrowwallet/sparrow/io/db/BlockTransactionDao.java index 39bba5a5..efb01490 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/db/BlockTransactionDao.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/db/BlockTransactionDao.java @@ -45,16 +45,20 @@ public interface BlockTransactionDao { Map existing = getForTxId(txid.getBytes()); if(existing.isEmpty() && blkTx.getId() == null) { - long id = insertBlockTransaction(txid.getBytes(), blkTx.getHash().getBytes(), blkTx.getHeight(), blkTx.getDate(), blkTx.getFee(), blkTx.getLabel(), + long id = insertBlockTransaction(txid.getBytes(), blkTx.getHash().getBytes(), blkTx.getHeight(), blkTx.getDate(), blkTx.getFee(), truncate(blkTx.getLabel()), blkTx.getTransaction() == null ? null : blkTx.getTransaction().bitcoinSerialize(), blkTx.getBlockHash() == null ? null : blkTx.getBlockHash().getBytes(), wallet.getId()); blkTx.setId(id); } else { Long existingId = existing.get(txid) != null ? existing.get(txid).getId() : blkTx.getId(); - updateBlockTransaction(txid.getBytes(), blkTx.getHash().getBytes(), blkTx.getHeight(), blkTx.getDate(), blkTx.getFee(), blkTx.getLabel(), + updateBlockTransaction(txid.getBytes(), blkTx.getHash().getBytes(), blkTx.getHeight(), blkTx.getDate(), blkTx.getFee(), truncate(blkTx.getLabel()), blkTx.getTransaction() == null ? null : blkTx.getTransaction().bitcoinSerialize(), blkTx.getBlockHash() == null ? null : blkTx.getBlockHash().getBytes(), wallet.getId(), existingId); blkTx.setId(existingId); } } + + default String truncate(String label) { + return (label != null && label.length() > 255 ? label.substring(0, 255) : label); + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/db/KeystoreDao.java b/src/main/java/com/sparrowwallet/sparrow/io/db/KeystoreDao.java index 2ac00997..b6e4929b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/db/KeystoreDao.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/db/KeystoreDao.java @@ -68,7 +68,7 @@ public interface KeystoreDao { } } - long id = insert(keystore.getLabel(), keystore.getSource().ordinal(), keystore.getWalletModel().ordinal(), + long id = insert(truncate(keystore.getLabel()), keystore.getSource().ordinal(), keystore.getWalletModel().ordinal(), keystore.hasPrivateKey() ? null : keystore.getKeyDerivation().getMasterFingerprint(), keystore.getKeyDerivation().getDerivationPath(), keystore.hasPrivateKey() ? null : keystore.getExtendedPublicKey().toString(), @@ -99,4 +99,8 @@ public interface KeystoreDao { } } } + + default String truncate(String label) { + return (label != null && label.length() > 255 ? label.substring(0, 255) : label); + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/db/WalletDao.java b/src/main/java/com/sparrowwallet/sparrow/io/db/WalletDao.java index a966c451..c2bcf6e4 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/db/WalletDao.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/db/WalletDao.java @@ -93,7 +93,7 @@ public interface WalletDao { setSchema(schema); createPolicyDao().addPolicy(wallet.getDefaultPolicy()); - long id = insert(wallet.getName(), wallet.getNetwork().ordinal(), wallet.getPolicyType().ordinal(), wallet.getScriptType().ordinal(), wallet.getStoredBlockHeight(), wallet.gapLimit(), wallet.getBirthDate(), wallet.getDefaultPolicy().getId()); + long id = insert(truncate(wallet.getName()), wallet.getNetwork().ordinal(), wallet.getPolicyType().ordinal(), wallet.getScriptType().ordinal(), wallet.getStoredBlockHeight(), wallet.gapLimit(), wallet.getBirthDate(), wallet.getDefaultPolicy().getId()); wallet.setId(id); createKeystoreDao().addKeystores(wallet); @@ -103,4 +103,8 @@ public interface WalletDao { setSchema(DbPersistence.DEFAULT_SCHEMA); } } + + default String truncate(String label) { + return (label != null && label.length() > 255 ? label.substring(0, 255) : label); + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/db/WalletNodeDao.java b/src/main/java/com/sparrowwallet/sparrow/io/db/WalletNodeDao.java index e120837c..1dc0b65b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/db/WalletNodeDao.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/db/WalletNodeDao.java @@ -58,10 +58,10 @@ public interface WalletNodeDao { default void addWalletNodes(Wallet wallet) { for(WalletNode purposeNode : wallet.getPurposeNodes()) { - long purposeNodeId = insertWalletNode(purposeNode.getDerivationPath(), purposeNode.getLabel(), wallet.getId(), null); + long purposeNodeId = insertWalletNode(purposeNode.getDerivationPath(), truncate(purposeNode.getLabel()), wallet.getId(), null); purposeNode.setId(purposeNodeId); for(WalletNode addressNode : purposeNode.getChildren()) { - long addressNodeId = insertWalletNode(addressNode.getDerivationPath(), addressNode.getLabel(), wallet.getId(), purposeNodeId); + long addressNodeId = insertWalletNode(addressNode.getDerivationPath(), truncate(addressNode.getLabel()), wallet.getId(), purposeNodeId); addressNode.setId(addressNodeId); addTransactionOutputs(addressNode); } @@ -84,22 +84,22 @@ public interface WalletNodeDao { if(txo.isSpent()) { BlockTransactionHashIndex spentBy = txo.getSpentBy(); if(spentBy.getId() == null) { - spentById = insertBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), spentBy.getLabel(), spentBy.getIndex(), spentBy.getValue(), + spentById = insertBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), truncate(spentBy.getLabel()), spentBy.getIndex(), spentBy.getValue(), spentBy.getStatus() == null ? null : spentBy.getStatus().ordinal(), null, addressNode.getId()); spentBy.setId(spentById); } else { - updateBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), spentBy.getLabel(), spentBy.getIndex(), spentBy.getValue(), + updateBlockTransactionHashIndex(spentBy.getHash().getBytes(), spentBy.getHeight(), spentBy.getDate(), spentBy.getFee(), truncate(spentBy.getLabel()), spentBy.getIndex(), spentBy.getValue(), spentBy.getStatus() == null ? null : spentBy.getStatus().ordinal(), null, addressNode.getId(), spentBy.getId()); spentById = spentBy.getId(); } } if(txo.getId() == null) { - long txoId = insertBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), txo.getLabel(), txo.getIndex(), txo.getValue(), + long txoId = insertBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), truncate(txo.getLabel()), txo.getIndex(), txo.getValue(), txo.getStatus() == null ? null : txo.getStatus().ordinal(), spentById, addressNode.getId()); txo.setId(txoId); } else { - updateBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), txo.getLabel(), txo.getIndex(), txo.getValue(), + updateBlockTransactionHashIndex(txo.getHash().getBytes(), txo.getHeight(), txo.getDate(), txo.getFee(), truncate(txo.getLabel()), txo.getIndex(), txo.getValue(), txo.getStatus() == null ? null : txo.getStatus().ordinal(), spentById, addressNode.getId(), txo.getId()); } } @@ -113,4 +113,8 @@ public interface WalletNodeDao { clearSpentHistory(wallet.getId()); clearHistory(wallet.getId()); } + + default String truncate(String label) { + return (label != null && label.length() > 255 ? label.substring(0, 255) : label); + } }