From b8ead01f2eb6815006154e19ab7be197b8d32e36 Mon Sep 17 00:00:00 2001 From: voisine Date: Tue, 26 Dec 2017 16:21:53 -0800 Subject: [PATCH] update tx ordering on confirmation --- BRBIP38Key.c | 36 ++++++++++++++++++------------------ BRWallet.c | 9 ++++++++- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/BRBIP38Key.c b/BRBIP38Key.c index 17d25c59c..6d0cc768f 100644 --- a/BRBIP38Key.c +++ b/BRBIP38Key.c @@ -90,14 +90,14 @@ static const uint8_t sboxi[256] = { static void _BRAES256ECBEncrypt(const void *key32, void *buf16) { size_t i, j; - uint32_t _k[32/4], _x[16/4]; - uint8_t *x = (uint8_t *)_x, *k = (uint8_t *)_k, r = 1, a, b, c, d, e; + uint32_t key[32/4], buf[16/4]; + uint8_t *x = (uint8_t *)buf, *k = (uint8_t *)key, r = 1, a, b, c, d, e; - memcpy(_k, key32, sizeof(_k)); - memcpy(_x, buf16, sizeof(_x)); + memcpy(key, key32, sizeof(key)); + memcpy(buf, buf16, sizeof(buf)); for (i = 0; i < 14; i++) { - for (j = 0; j < 4; j++) _x[j] ^= _k[j+(i & 1)*4]; // add round key + for (j = 0; j < 4; j++) buf[j] ^= key[j+(i & 1)*4]; // add round key for (j = 0; j < 16; j++) x[j] = sbox[x[j]]; // sub bytes @@ -119,20 +119,20 @@ static void _BRAES256ECBEncrypt(const void *key32, void *buf16) } var_clean(&r, &a, &b, &c, &d, &e); - for (i = 0; i < 4; i++) _x[i] ^= _k[i]; // final add round key - mem_clean(_k, sizeof(_k)); - memcpy(buf16, _x, sizeof(_x)); - mem_clean(_x, sizeof(_x)); + for (i = 0; i < 4; i++) buf[i] ^= key[i]; // final add round key + mem_clean(key, sizeof(key)); + memcpy(buf16, buf, sizeof(buf)); + mem_clean(buf, sizeof(buf)); } static void _BRAES256ECBDecrypt(const void *key32, void *buf16) { size_t i, j; - uint32_t _k[32/4], _x[16/4]; - uint8_t *x = (uint8_t *)_x, *k = (uint8_t *)_k, r = 1, a, b, c, d, e, f, g, h; + uint32_t key[32/4], buf[16/4]; + uint8_t *x = (uint8_t *)buf, *k = (uint8_t *)key, r = 1, a, b, c, d, e, f, g, h; - memcpy(_k, key32, sizeof(_k)); - memcpy(_x, buf16, sizeof(_x)); + memcpy(key, key32, sizeof(key)); + memcpy(buf, buf16, sizeof(buf)); for (i = 0; i < 7; i++) { // expand key k[0] ^= sbox[k[29]] ^ r, k[1] ^= sbox[k[30]], k[2] ^= sbox[k[31]], k[3] ^= sbox[k[28]], r = xt(r); @@ -142,7 +142,7 @@ static void _BRAES256ECBDecrypt(const void *key32, void *buf16) } for (i = 0; i < 14; i++) { - for (j = 0; j < 4; j++) _x[j] ^= _k[j+(i & 1)*4]; // add round key + for (j = 0; j < 4; j++) buf[j] ^= key[j+(i & 1)*4]; // add round key for (j = 0; i > 0 && j < 16; j += 4) { // unmix columns a = x[j], b = x[j+1], c = x[j+2], d = x[j+3], e = a ^ b ^ c ^ d; @@ -166,10 +166,10 @@ static void _BRAES256ECBDecrypt(const void *key32, void *buf16) } var_clean(&r, &a, &b, &c, &d, &e, &f, &g, &h); - for (i = 0; i < 4; i++) _x[i] ^= _k[i]; // final add round key - mem_clean(_k, sizeof(_k)); - memcpy(buf16, _x, sizeof(_x)); - mem_clean(_x, sizeof(_x)); + for (i = 0; i < 4; i++) buf[i] ^= key[i]; // final add round key + mem_clean(key, sizeof(key)); + memcpy(buf16, buf, sizeof(buf)); + mem_clean(buf, sizeof(buf)); } static UInt256 _BRBIP38DerivePassfactor(uint8_t flag, const uint8_t *entropy, const char *passphrase) diff --git a/BRWallet.c b/BRWallet.c index e9dd05d3d..dcedede28 100644 --- a/BRWallet.c +++ b/BRWallet.c @@ -935,7 +935,7 @@ void BRWalletUpdateTransactions(BRWallet *wallet, const UInt256 txHashes[], size BRTransaction *tx; UInt256 hashes[txCount]; int needsUpdate = 0; - size_t i, j; + size_t i, j, k; assert(wallet != NULL); assert(txHashes != NULL || txCount == 0); @@ -949,6 +949,13 @@ void BRWalletUpdateTransactions(BRWallet *wallet, const UInt256 txHashes[], size tx->blockHeight = blockHeight; if (_BRWalletContainsTx(wallet, tx)) { + for (k = array_count(wallet->transactions); k > 0; k--) { // remove and re-insert tx to keep wallet sorted + if (! BRTransactionEq(wallet->transactions[k - 1], tx)) continue; + array_rm(wallet->transactions, k - 1); + _BRWalletInsertTx(wallet, tx); + break; + } + hashes[j++] = txHashes[i]; if (BRSetContains(wallet->pendingTx, tx) || BRSetContains(wallet->invalidTx, tx)) needsUpdate = 1; }