Skip to content

Commit

Permalink
🚀[Release v1.0.0] Merge into Main (#30)
Browse files Browse the repository at this point in the history
* typo :)

* va_list include

* enable ipv6

* default to ipv6 and fallback to ipv4

* fix ipv4 fallback

* Increase MAX_FEE_PER_KB

* Fix 0 change output

* sha3-256 and keccak-256

* const keywords

* update tx ordering on confirmation

* compiler warnings

* Litecoin: params

* Litecoin: scrypt pow & max_proof_of_work

* Litecoin: disable retarget check

* Litecoin: add new checkpoint

* BIP39_CREATION_TIME -> LoafWallet release date

* update secp

* make sure to call balanceChanged callback whenever balance is updated

* Change bitcoin-testnet with litecoin-testnet

* bech32: update files

* add missing files

* add BRPeerManagerStandardPort method which returns the standard port used for chain params

* 🦺Updated the checkpoints: Android branch (#24)

* Updated the checkpoints

* only 1 checkpoint added

* Added Testnet checkpoint

* added interface to set fpRate (#28)

fpRate: false positive rate for the bloomfilter

* fixed the signature (#29)

---------

Co-authored-by: LiteBit <[email protected]>
Co-authored-by: Aaron Voisine <[email protected]>
Co-authored-by: Adrian Corscadden <[email protected]>
Co-authored-by: Luca Vaccaro <[email protected]>
Co-authored-by: Loshan T <[email protected]>
Co-authored-by: Mohamed Barry <[email protected]>
  • Loading branch information
7 people authored Mar 29, 2023
1 parent 46bfe93 commit 4d2d088
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 37 deletions.
23 changes: 10 additions & 13 deletions BRAddress.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,11 @@ size_t BRAddressFromScriptPubKey(char *addr, size_t addrLen, const uint8_t *scri
const uint8_t *d, *elems[BRScriptElements(NULL, 0, script, scriptLen)];
char a[91];
size_t r = 0, l = 0, count = BRScriptElements(elems, sizeof(elems)/sizeof(*elems), script, scriptLen);

if (count == 5 && *elems[0] == OP_DUP && *elems[1] == OP_HASH160 && *elems[2] == 20 &&
*elems[3] == OP_EQUALVERIFY && *elems[4] == OP_CHECKSIG) {
// pay-to-pubkey-hash scriptPubKey
data[0] = BITCOIN_PUBKEY_ADDRESS;
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
data[0] = BITCOIN_PUBKEY_ADDRESS_TEST;
#endif
memcpy(&data[1], BRScriptData(elems[2], &l), 20);
Expand All @@ -259,7 +258,7 @@ size_t BRAddressFromScriptPubKey(char *addr, size_t addrLen, const uint8_t *scri
else if (count == 3 && *elems[0] == OP_HASH160 && *elems[1] == 20 && *elems[2] == OP_EQUAL) {
// pay-to-script-hash scriptPubKey
data[0] = BITCOIN_SCRIPT_ADDRESS;
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
data[0] = BITCOIN_SCRIPT_ADDRESS_TEST;
#endif
memcpy(&data[1], BRScriptData(elems[1], &l), 20);
Expand All @@ -268,7 +267,7 @@ size_t BRAddressFromScriptPubKey(char *addr, size_t addrLen, const uint8_t *scri
else if (count == 2 && (*elems[0] == 65 || *elems[0] == 33) && *elems[1] == OP_CHECKSIG) {
// pay-to-pubkey scriptPubKey
data[0] = BITCOIN_PUBKEY_ADDRESS;
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
data[0] = BITCOIN_PUBKEY_ADDRESS_TEST;
#endif
d = BRScriptData(elems[0], &l);
Expand All @@ -279,7 +278,7 @@ size_t BRAddressFromScriptPubKey(char *addr, size_t addrLen, const uint8_t *scri
(*elems[0] >= OP_1 && *elems[0] <= OP_16 && *elems[1] >= 2 && *elems[1] <= 40))) {
// pay-to-witness scriptPubKey
r = BRBech32Encode(a, "ltc", script);
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
r = BRBech32Encode(a, "tltc", script);
#endif
if (addr && r > addrLen) r = 0;
Expand All @@ -301,7 +300,7 @@ size_t BRAddressFromScriptSig(char *addr, size_t addrLen, const uint8_t *script,
size_t count = BRScriptElements(elems, sizeof(elems)/sizeof(*elems), script, scriptLen), l = 0;

data[0] = BITCOIN_PUBKEY_ADDRESS;
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
data[0] = BITCOIN_PUBKEY_ADDRESS_TEST;
#endif

Expand All @@ -314,7 +313,7 @@ size_t BRAddressFromScriptSig(char *addr, size_t addrLen, const uint8_t *script,
else if (count >= 2 && *elems[count - 2] <= OP_PUSHDATA4 && *elems[count - 1] <= OP_PUSHDATA4 &&
*elems[count - 1] > 0) { // pay-to-script-hash scriptSig
data[0] = BITCOIN_SCRIPT_ADDRESS;
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
data[0] = BITCOIN_SCRIPT_ADDRESS_TEST;
#endif
d = BRScriptData(elems[count - 1], &l);
Expand All @@ -324,7 +323,6 @@ size_t BRAddressFromScriptSig(char *addr, size_t addrLen, const uint8_t *script,
// TODO: implement Peter Wullie's pubKey recovery from signature
}
// pay-to-witness scriptSig's are empty

return (d) ? BRBase58CheckEncode(addr, addrLen, data, 21) : 0;
}

Expand All @@ -345,7 +343,7 @@ size_t BRAddressScriptPubKey(uint8_t *script, size_t scriptLen, const char *addr

assert(addr != NULL);

#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
pubkeyAddress = BITCOIN_PUBKEY_ADDRESS_TEST;
scriptAddress = BITCOIN_SCRIPT_ADDRESS_TEST;
bech32Prefix = "tltc";
Expand Down Expand Up @@ -377,7 +375,6 @@ size_t BRAddressScriptPubKey(uint8_t *script, size_t scriptLen, const char *addr
}
else {
dataLen = BRBech32Decode(hrp, data, addr);

if (dataLen > 2 && strcmp(hrp, bech32Prefix) == 0 && (data[0] != OP_0 || data[1] == 20 || data[1] == 32)) {
if (script && dataLen <= scriptLen) memcpy(script, data, dataLen);
r = (! script || dataLen <= scriptLen) ? dataLen : 0;
Expand All @@ -399,18 +396,18 @@ int BRAddressIsValid(const char *addr)
if (BRBase58CheckDecode(data, sizeof(data), addr) == 21) {
r = (data[0] == BITCOIN_PUBKEY_ADDRESS || data[0] == BITCOIN_SCRIPT_ADDRESS);

#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
r = (data[0] == BITCOIN_PUBKEY_ADDRESS_TEST || data[0] == BITCOIN_SCRIPT_ADDRESS_TEST);
#endif
}
else if (BRBech32Decode(hrp, data, addr) > 2) {
r = (strcmp(hrp, "ltc") == 0 && (data[0] != OP_0 || data[1] == 20 || data[1] == 32));

#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
r = (strcmp(hrp, "tltc") == 0 && (data[0] != OP_0 || data[1] == 20 || data[1] == 32));
#endif
}

return r;
}

Expand Down
2 changes: 1 addition & 1 deletion BRAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
extern "C" {
#endif

#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
#pragma message "testnet build"
#endif

Expand Down
1 change: 0 additions & 1 deletion BRBech32.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// BRBech32.c
// breadwallet-core
//
// Created by Aaron Voisine on 1/20/18.
// Copyright (c) 2018 breadwallet LLC
Expand Down
1 change: 0 additions & 1 deletion BRBech32.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// BRBech32.h
// breadwallet-core
//
// Created by Aaron Voisine on 1/20/18.
// Copyright (c) 2018 breadwallet LLC
Expand Down
8 changes: 4 additions & 4 deletions BRKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int BRPrivKeyIsValid(const char *privKey)
strLen = strlen(privKey);

if (dataLen == 33 || dataLen == 34) { // wallet import format: https://en.bitcoin.it/wiki/Wallet_import_format
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
r = (data[0] == BITCOIN_PRIVKEY_TEST);
#else
r = (data[0] == BITCOIN_PRIVKEY);
Expand Down Expand Up @@ -172,7 +172,7 @@ int BRKeySetPrivKey(BRKey *key, const char *privKey)
uint8_t data[34], version = BITCOIN_PRIVKEY;
int r = 0;

#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
version = BITCOIN_PRIVKEY_TEST;
#endif

Expand Down Expand Up @@ -233,7 +233,7 @@ size_t BRKeyPrivKey(const BRKey *key, char *privKey, size_t pkLen)

if (secp256k1_ec_seckey_verify(_ctx, key->secret.u8)) {
data[0] = BITCOIN_PRIVKEY;
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
data[0] = BITCOIN_PRIVKEY_TEST;
#endif

Expand Down Expand Up @@ -292,7 +292,7 @@ size_t BRKeyAddress(BRKey *key, char *addr, size_t addrLen)

hash = BRKeyHash160(key);
data[0] = BITCOIN_PUBKEY_ADDRESS;
#if BITCOIN_TESTNET
#if LITECOIN_TESTNET
data[0] = BITCOIN_PUBKEY_ADDRESS_TEST;
#endif
UInt160Set(&data[1], hash);
Expand Down
3 changes: 1 addition & 2 deletions BRMerkleBlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,13 @@ int BRMerkleBlockVerifyDifficulty(const BRMerkleBlock *block, const BRMerkleBloc

if (! previous || !UInt256Eq(block->prevBlock, previous->blockHash) || block->height != previous->height + 1) r = 0;
if (r && (block->height % BLOCK_DIFFICULTY_INTERVAL) == 0 && transitionTime == 0) r = 0;

// if (r && (block->height % BLOCK_DIFFICULTY_INTERVAL) == 0) {
// // target is in "compact" format, where the most significant byte is the size of resulting value in bytes, next
// // bit is the sign, and the remaining 23bits is the value after having been right shifted by (size - 3)*8 bits
// static const uint32_t maxsize = MAX_PROOF_OF_WORK >> 24, maxtarget = MAX_PROOF_OF_WORK & 0x00ffffff;
// int timespan = (int)((int64_t)previous->timestamp - (int64_t)transitionTime), size = previous->target >> 24;
// uint64_t target = previous->target & 0x00ffffff;

// // limit difficulty transition to -75% or +400%
// if (timespan < TARGET_TIMESPAN/4) timespan = TARGET_TIMESPAN/4;
// if (timespan > TARGET_TIMESPAN*4) timespan = TARGET_TIMESPAN*4;
Expand Down
2 changes: 1 addition & 1 deletion BRPeer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extern "C" {
#define SERVICES_NODE_BCASH 0x20 // https://github.com/Bitcoin-UAHF/spec/blob/master/uahf-technical-spec.md

#define BR_VERSION "2.1"
#define USER_AGENT "/loaf:" BR_VERSION "/"
#define USER_AGENT "/litewallet-loafwallet-core:" BR_VERSION "/"

// explanation of message types at: https://en.bitcoin.it/wiki/Protocol_specification
#define MSG_VERSION "version"
Expand Down
15 changes: 8 additions & 7 deletions BRPeerManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,8 @@ static void _BRPeerManagerLoadBloomFilter(BRPeerManager *manager, BRPeer *peer)
BRSetApply(manager->orphans, NULL, _setApplyFreeBlock);
BRSetClear(manager->orphans); // clear out orphans that may have been received on an old filter
manager->lastOrphan = NULL;
manager->filterUpdateHeight = manager->lastBlock->height;
manager->fpRate = BLOOM_REDUCED_FALSEPOSITIVE_RATE;

manager->filterUpdateHeight = manager->lastBlock->height;

size_t addrsCount = BRWalletAllAddrs(manager->wallet, NULL, 0);
BRAddress *addrs = malloc(addrsCount*sizeof(*addrs));
size_t utxosCount = BRWalletUTXOs(manager->wallet, NULL, 0);
Expand Down Expand Up @@ -796,7 +795,6 @@ static void _peerConnected(void *info)
peer_log(peer, "selecting new download peer with higher reported lastblock");
BRPeerDisconnect(manager->downloadPeer);
}

manager->downloadPeer = peer;
manager->isConnected = 1;
manager->estimatedHeight = BRPeerLastBlock(peer);
Expand Down Expand Up @@ -1118,7 +1116,7 @@ static void _peerRejectedTx(void *info, UInt256 txHash, uint8_t code)
static int _BRPeerManagerVerifyBlock(BRPeerManager *manager, BRMerkleBlock *block, BRMerkleBlock *prev, BRPeer *peer)
{
int r = 1;

if (! prev || ! UInt256Eq(block->prevBlock, prev->blockHash) || block->height != prev->height + 1) r = 0;

// check if we hit a difficulty transition, and find previous transition time
Expand All @@ -1135,7 +1133,7 @@ static int _BRPeerManagerVerifyBlock(BRPeerManager *manager, BRMerkleBlock *bloc
r = 0;
}
else prevBlock = b->prevBlock;

while (b) { // free up some memory
b = BRSetGet(manager->blocks, &prevBlock);
if (b) prevBlock = b->prevBlock;
Expand Down Expand Up @@ -1525,7 +1523,9 @@ static void _dummyThreadCleanup(void *info)

// returns a newly allocated BRPeerManager struct that must be freed by calling BRPeerManagerFree()
BRPeerManager *BRPeerManagerNew(const BRChainParams *params, BRWallet *wallet, uint32_t earliestKeyTime,
BRMerkleBlock *blocks[], size_t blocksCount, const BRPeer peers[], size_t peersCount)
BRMerkleBlock *blocks[], size_t blocksCount,
const BRPeer peers[], size_t peersCount,
double fpRate)
{
BRPeerManager *manager = calloc(1, sizeof(*manager));
BRMerkleBlock orphan, *block = NULL;
Expand All @@ -1548,6 +1548,7 @@ BRPeerManager *BRPeerManagerNew(const BRChainParams *params, BRWallet *wallet, u
manager->blocks = BRSetNew(BRMerkleBlockHash, BRMerkleBlockEq, blocksCount);
manager->orphans = BRSetNew(_BRPrevBlockHash, _BRPrevBlockEq, blocksCount); // orphans are indexed by prevBlock
manager->checkpoints = BRSetNew(_BRBlockHeightHash, _BRBlockHeightEq, 100); // checkpoints are indexed by height
manager->fpRate = fpRate; //loading the preferred rate

for (size_t i = 0; i < manager->params->checkpointsCount; i++) {
block = BRMerkleBlockNew();
Expand Down
3 changes: 2 additions & 1 deletion BRPeerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ typedef struct BRPeerManagerStruct BRPeerManager;

// returns a newly allocated BRPeerManager struct that must be freed by calling BRPeerManagerFree()
BRPeerManager *BRPeerManagerNew(const BRChainParams *params, BRWallet *wallet, uint32_t earliestKeyTime,
BRMerkleBlock *blocks[], size_t blocksCount, const BRPeer peers[], size_t peersCount);
BRMerkleBlock *blocks[], size_t blocksCount, const BRPeer peers[], size_t peersCount,
double fpRate);

// not thread-safe, set callbacks once before calling BRPeerManagerConnect()
// info is a void pointer that will be passed along with each callback call
Expand Down
2 changes: 0 additions & 2 deletions BRTransaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ BRTransaction *BRTransactionCopy(const BRTransaction *tx)
BRTransaction *cpy = BRTransactionNew();
BRTxInput *inputs = cpy->inputs;
BRTxOutput *outputs = cpy->outputs;

assert(tx != NULL);
*cpy = *tx;
cpy->inputs = inputs;
Expand All @@ -353,7 +352,6 @@ BRTransaction *BRTransactionCopy(const BRTransaction *tx)
tx->inputs[i].script, tx->inputs[i].scriptLen,
tx->inputs[i].signature, tx->inputs[i].sigLen, tx->inputs[i].sequence);
}

for (size_t i = 0; i < tx->outCount; i++) {
BRTransactionAddOutput(cpy, tx->outputs[i].amount, tx->outputs[i].script, tx->outputs[i].scriptLen);
}
Expand Down
2 changes: 1 addition & 1 deletion secp256k1
Submodule secp256k1 updated 0 files
Loading

0 comments on commit 4d2d088

Please sign in to comment.