Skip to content

Commit

Permalink
Add segwit tests (#854)
Browse files Browse the repository at this point in the history
* Remove BitcoinLikeTransactionBuilder::addInput
* Add send to P2WPKH and P2WSH tests
* An attempt to fix "ComputeOperationUidWithValidTransaction"
  • Loading branch information
viktorb-ledger authored Feb 17, 2022
1 parent 5fd24a3 commit 6b441d3
Show file tree
Hide file tree
Showing 19 changed files with 469 additions and 95 deletions.
13 changes: 3 additions & 10 deletions core/idl/wallet/bitcoin/bitcoin_like_wallet.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ BitcoinLikeTransaction = interface +c {
# through the full ledger stack
# @return the OLD Correlation ID, if it was set (empty string if it was unset)
setCorrelationId(correlationId : string) : string;
# Sign all inputs for given transaction.
# Sign all inputs for given transaction.
# Build DER encoded signature from RSV data.
# @return SIGNING_SUCCEED if succeed case else refers to BitcoinLikeSignatureState enumeration
setSignatures(signatures: list<BitcoinLikeSignature>, override: bool): BitcoinLikeSignatureState;
# Sign all inputs for given transaction.
# Sign all inputs for given transaction.
# @return SIGNING_SUCCEED if succeed case else refers to BitcoinLikeSignatureState enumeration
setDERSignatures(signatures: list<binary>, override: bool): BitcoinLikeSignatureState;
}
Expand Down Expand Up @@ -235,13 +235,6 @@ BitcoinLikePickingStrategy = enum {
}

BitcoinLikeTransactionBuilder = interface +c {
# Add the given input to the final transaction.
# @param transactionhash The hash of the transaction in where the UTXO can be located.
# @params index Index of the UTXO in the previous transaction
# @params sequence Sequence number to add at the end of the input serialization. This can be used for RBF transaction
# @return A reference on the same builder in order to chain calls.
addInput(transactionHash: string, index: i32, sequence: i32): BitcoinLikeTransactionBuilder;

# Add the given output to the final transaction.
# @return A reference on the same builder in order to chain calls.
addOutput(amount: Amount, script: BitcoinLikeScript): BitcoinLikeTransactionBuilder;
Expand Down Expand Up @@ -303,7 +296,7 @@ BitcoinLikeTransactionBuilder = interface +c {
reset();

# Set the correlation id which can be used to debug transaction errors
# through the full ledger stack
# through the full ledger stack
# @return A reference on the same builder in order to chain calls.
setCorrelationId(correlationId : string) : BitcoinLikeTransactionBuilder;

Expand Down
4 changes: 2 additions & 2 deletions core/src/api/BitcoinLikeTransaction.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions core/src/api/BitcoinLikeTransactionBuilder.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions core/src/jni/jni/BitcoinLikeTransactionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ CJNIEXPORT void JNICALL Java_co_ledger_core_BitcoinLikeTransactionBuilder_00024C
} JNI_TRANSLATE_EXCEPTIONS_RETURN(jniEnv, )
}

CJNIEXPORT jobject JNICALL Java_co_ledger_core_BitcoinLikeTransactionBuilder_00024CppProxy_native_1addInput(JNIEnv* jniEnv, jobject /*this*/, jlong nativeRef, jstring j_transactionHash, jint j_index, jint j_sequence)
{
try {
DJINNI_FUNCTION_PROLOGUE1(jniEnv, nativeRef);
const auto& ref = ::djinni::objectFromHandleAddress<::ledger::core::api::BitcoinLikeTransactionBuilder>(nativeRef);
auto r = ref->addInput(::djinni::String::toCpp(jniEnv, j_transactionHash),
::djinni::I32::toCpp(jniEnv, j_index),
::djinni::I32::toCpp(jniEnv, j_sequence));
return ::djinni::release(::djinni_generated::BitcoinLikeTransactionBuilder::fromCpp(jniEnv, r));
} JNI_TRANSLATE_EXCEPTIONS_RETURN(jniEnv, 0 /* value doesn't matter */)
}

CJNIEXPORT jobject JNICALL Java_co_ledger_core_BitcoinLikeTransactionBuilder_00024CppProxy_native_1addOutput(JNIEnv* jniEnv, jobject /*this*/, jlong nativeRef, jobject j_amount, jobject j_script)
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace ledger {
)); // Max ui512

BitcoinLikeTransactionBuilder::BitcoinLikeTransactionBuilder(const BitcoinLikeTransactionBuilder &cpy)
: _request(std::make_shared<BigInt>(cpy._currency.bitcoinLikeNetworkParameters.value().Dust)) {
: _request(std::make_shared<BigInt>(cpy._currency.bitcoinLikeNetworkParameters.value().Dust)) {
_currency = cpy._currency;
_build = cpy._build;
_request = cpy._request;
Expand All @@ -60,22 +60,14 @@ namespace ledger {
const std::shared_ptr<api::ExecutionContext> &context, const api::Currency &currency,
const std::shared_ptr<spdlog::logger> &logger,
const BitcoinLikeTransactionBuildFunction &buildFunction) :
_request(std::make_shared<BigInt>(currency.bitcoinLikeNetworkParameters.value().Dust)) {
_request(std::make_shared<BigInt>(currency.bitcoinLikeNetworkParameters.value().Dust)) {
_currency = currency;
_build = buildFunction;
_context = context;
_logger = logger;
_request.wipe = false;
}

std::shared_ptr<api::BitcoinLikeTransactionBuilder>
BitcoinLikeTransactionBuilder::addInput(const std::string &transactionHash, int32_t index, int32_t sequence) {
_request.inputs.push_back(BitcoinLikeTransactionInputDescriptor{
transactionHash, static_cast<uint64_t>(index), static_cast<uint64_t>(sequence)});

return shared_from_this();
}

std::shared_ptr<api::BitcoinLikeTransactionBuilder>
BitcoinLikeTransactionBuilder::addOutput(const std::shared_ptr<api::Amount> &amount,
const std::shared_ptr<api::BitcoinLikeScript> &script) {
Expand Down Expand Up @@ -106,7 +98,7 @@ namespace ledger {
std::shared_ptr<api::BitcoinLikeTransactionBuilder>
BitcoinLikeTransactionBuilder::pickInputs(api::BitcoinLikePickingStrategy strategy, int32_t sequence, optional<int32_t> maxUtxo) {
//Fix: use uniform initialization

BitcoinUtxoPickerParams new_utxo_picker{strategy, sequence, maxUtxo};
_request.utxoPicker = Option<BitcoinUtxoPickerParams>(std::move(new_utxo_picker));
return shared_from_this();
Expand Down Expand Up @@ -167,7 +159,7 @@ namespace ledger {

void BitcoinLikeTransactionBuilder::reset() {
_request = BitcoinLikeTransactionBuildRequest(std::make_shared<BigInt>(
_currency.bitcoinLikeNetworkParameters.value().Dust)
_currency.bitcoinLikeNetworkParameters.value().Dust)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ namespace ledger {
const std::shared_ptr<spdlog::logger>& logger,
const BitcoinLikeTransactionBuildFunction& buildFunction);
BitcoinLikeTransactionBuilder(const BitcoinLikeTransactionBuilder& cpy);
std::shared_ptr<api::BitcoinLikeTransactionBuilder>
addInput(const std::string &transactionHash, int32_t index, int32_t sequence) override;

std::shared_ptr<api::BitcoinLikeTransactionBuilder> addOutput(const std::shared_ptr<api::Amount> &amount,
const std::shared_ptr<api::BitcoinLikeScript> &script) override;
Expand Down
31 changes: 31 additions & 0 deletions core/test/fixtures/txes_to_wpkh._fixtures.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "txes_to_wpkh_fixtures.h"

namespace ledger {
namespace testing {
namespace txes_to_wpkh {
core::api::ExtendedKeyAccountCreationInfo XPUB_INFO(
0, {"main"}, {"84'/0'/0'"}, {"xpub6C2qnauSiRRR8UG9PXraYrfBtXZHzFRNgwJgwG7ZDp7SbWAdMSiMFoYDukTw4ExE95H4VkgECnhDe2uyLawoqUkoSHEDnoDafC2R44dXs7p"}
);
// xpub from https://github.com/LedgerHQ/xpub-scan/blob/master/.github/workflows/regression_tests/datasets.json
const std::vector<std::string> TXes =
{
"{\"hash\":\"4b3a5535d24d882233bb991a2e6fa111e64294aadcd140960cdceada2fae1bb8\",\"received_at\":\"2020-06-26T13:58:11Z\",\"lock_time\":0,\"block\":{\"hash\":\"00000000000000000003f12608c713d887fc216753ba70b264cc54379415928b\",\"height\":636436,\"time\":\"2020-06-26T14:29:17Z\"},\"inputs\":[{\"input_index\":0,\"output_hash\":\"70dcda8357d590f0cfb6cd504731d427db76fe03616f57c7af8366106af67092\",\"output_index\":1,\"value\":37471,\"address\":\"3MVXMz1zCEBScDM1pMsifuYavjzEjxCfM6\",\"script_signature\":\"160014682a7e09d3f88f56f6e9ed0a9511f064b79ab722\",\"sequence\":0,\"txinwitness\":[\"30450221009575c1fc1890db00b7cc33426954b19e516d19b03fd9fc924579e718f8437e4902203f0f5b8763c389548c910bbd7cd72a8d0bb1f35ba67f5b53f9ca1620bc86139801\",\"0378b2de4c77eaa8c1285ff73837a4b7abe9babf30e1cbc522329e0e15db2c03bc\"]}],\"outputs\":[{\"output_index\":0,\"value\":27278,\"address\":\"bc1qr500ysrg653aaplftaac753srtt2jwtfvcr5vt\",\"script_hex\":\"00141d1ef24068d523de87e95f7b8f52301ad6a93969\"},{\"output_index\":1,\"value\":9017,\"address\":\"3Nq7i7JRT7rzoKhhnxE3F8hbp1FpjN9jmV\",\"script_hex\":\"a914e7e2cc7a5d070144867183cd27b13db3b074883b87\"}],\"fees\":1176,\"amount\":36295,\"confirmations\":86334}",
"{\"hash\":\"673f7e1155dd2cf61c961cedd24608274c0f20cfaeaa1154c2b5ef94ec7b81d1\",\"received_at\":\"2021-09-08T12:45:21Z\",\"lock_time\":0,\"block\":{\"hash\":\"00000000000000000003fa8ebda5058dca475e65a8aaf692dc5a7449cf6e5a89\",\"height\":699622,\"time\":\"2021-09-08T12:58:43Z\"},\"inputs\":[{\"input_index\":0,\"output_hash\":\"4b3a5535d24d882233bb991a2e6fa111e64294aadcd140960cdceada2fae1bb8\",\"output_index\":0,\"value\":27278,\"address\":\"bc1qr500ysrg653aaplftaac753srtt2jwtfvcr5vt\",\"script_signature\":\"\",\"sequence\":0,\"txinwitness\":[\"304402205e18af8a590bdfd5c9e4f2e349b105ec5efb56b5fd3cf07e69786ef979d53ff4022069f8fd41fb6e9edee10d80f5bd8144a72ee1c609d93512487e362733103f78d201\",\"026f85f41f6a4ba5a1fc6819ffcfd9013e2ccd372b94e31fe867362035b38f1652\"]}],\"outputs\":[{\"output_index\":0,\"value\":1000,\"address\":\"bc1qrewjj96rjfzc9z2al0hvs2jtdc58nkgvrr6fgv\",\"script_hex\":\"00141e5d291743924582895dfbeec82a4b6e2879d90c\"},{\"output_index\":1,\"value\":25402,\"address\":\"bc1qz2z9dnhzwhveqemt9utryeucqnjuupenmfzsxv\",\"script_hex\":\"0014128456cee275d990676b2f1632679804e5ce0733\"}],\"fees\":876,\"amount\":26402,\"confirmations\":23148}",
"{\"hash\":\"5464631456754e6b410d5d9eb7cff4f82d1dc9aec0e2ec8fe759df6118e0112f\",\"received_at\":\"2021-11-20T12:48:08Z\",\"lock_time\":0,\"block\":{\"hash\":\"0000000000000000000b96dc8d6e75dc3eb4f995a10545f84ad1b8f1de4b9893\",\"height\":710566,\"time\":\"2021-11-20T12:52:04Z\"},\"inputs\":[{\"input_index\":0,\"output_hash\":\"4fadad2c103014ebaa3073ea63ff060b27d2d30b4e7f956727b97a44d1cf8ad7\",\"output_index\":0,\"value\":67218,\"address\":\"bc1qr500ysrg653aaplftaac753srtt2jwtfvcr5vt\",\"script_signature\":\"\",\"sequence\":4294967295,\"txinwitness\":[\"304402201668900493e07d2a4c2cf2b9c98a2b3ad1afc83da34cf727098077f34111209202202da599d4368331def39c95eab86480a9e9772fe4a393ad97d7174358becf422a01\",\"026f85f41f6a4ba5a1fc6819ffcfd9013e2ccd372b94e31fe867362035b38f1652\"]}],\"outputs\":[{\"output_index\":0,\"value\":60000,\"address\":\"1MD43R5k9qoAch5nUxk3BxNkVxpGbDS8iw\",\"script_hex\":\"76a914ddaa0bd7223bd2b5e61e8e7541fc22b9197440d288ac\"},{\"output_index\":1,\"value\":6780,\"address\":\"bc1q98csrhfkzrvkeee0k6jn70xdl2ghtewlkxva87\",\"script_hex\":\"001429f101dd3610d96ce72fb6a53f3ccdfa9175e5df\"}],\"fees\":438,\"amount\":66780,\"confirmations\":12204}",
"{\"hash\":\"4fadad2c103014ebaa3073ea63ff060b27d2d30b4e7f956727b97a44d1cf8ad7\",\"received_at\":\"2021-11-20T12:43:20Z\",\"lock_time\":0,\"block\":{\"hash\":\"0000000000000000000b96dc8d6e75dc3eb4f995a10545f84ad1b8f1de4b9893\",\"height\":710566,\"time\":\"2021-11-20T12:52:04Z\"},\"inputs\":[{\"input_index\":0,\"output_hash\":\"6b1c1bab2ce9a3429c4ddbbecf70d5ca2ac9a27634ebc8dd2dff347073eb1e39\",\"output_index\":0,\"value\":67554,\"address\":\"bc1qrd58306xwnpxegh76tp8gns4jxvjm7zxv3zzlc\",\"script_signature\":\"\",\"sequence\":4294967295,\"txinwitness\":[\"3045022100c27cd772986b8b82af189be7260a1e436e1a0573f8e908570b0fc55cd33541f4022059c575c3738acc502651ac0806816e91c9775cc253ee1f6798c2cf87901bb6c501\",\"0242dedafae7bdaf0c2b0ec74d2e1d83f874c11ad71e4a00031301e3aeb3322a6f\"]}],\"outputs\":[{\"output_index\":0,\"value\":67218,\"address\":\"bc1qr500ysrg653aaplftaac753srtt2jwtfvcr5vt\",\"script_hex\":\"00141d1ef24068d523de87e95f7b8f52301ad6a93969\"}],\"fees\":336,\"amount\":67218,\"confirmations\":12204}"
};
std::shared_ptr<core::BitcoinLikeAccount> inflate(const std::shared_ptr<core::WalletPool>& pool, const std::shared_ptr<core::AbstractWallet>& wallet) {
auto account
= std::dynamic_pointer_cast<ledger::core::BitcoinLikeAccount>(
uv::wait(wallet->newAccountWithExtendedKeyInfo(XPUB_INFO)));
for (const std::string& tx : TXes) {
std::vector<ledger::core::Operation> operations;
const auto parsedTx = ledger::core::JSONUtils::parse<ledger::core::TransactionParser>(tx);
account->interpretTransaction(*parsedTx, operations, true);
account->bulkInsert(operations);
}
return account;
}
}
}
}
41 changes: 41 additions & 0 deletions core/test/fixtures/txes_to_wpkh_fixtures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef LEDGER_FIXTURES_TXES_TO_WPKH
#define LEDGER_FIXTURES_TXES_TO_WPKH
#include <gtest/gtest.h>
#include <UvThreadDispatcher.hpp>
#include <src/database/DatabaseSessionPool.hpp>
#include <NativePathResolver.hpp>
#include <unordered_set>
#include <src/wallet/pool/WalletPool.hpp>
#include <CoutLogPrinter.hpp>
#include <src/api/DynamicObject.hpp>
#include <wallet/common/CurrencyBuilder.hpp>
#include <wallet/bitcoin/BitcoinLikeWallet.hpp>
#include <wallet/bitcoin/database/BitcoinLikeWalletDatabase.h>
#include <wallet/bitcoin/database/BitcoinLikeTransactionDatabaseHelper.h>
#include <wallet/common/database/AccountDatabaseHelper.h>
#include <wallet/pool/database/PoolDatabaseHelper.hpp>
#include <utils/JSONUtils.h>
#include <wallet/bitcoin/explorers/api/TransactionParser.hpp>
#include <wallet/bitcoin/BitcoinLikeAccount.hpp>
#include <api/BitcoinLikeOperation.hpp>
#include <api/BitcoinLikeTransaction.hpp>
#include <api/BitcoinLikeInput.hpp>
#include <api/BitcoinLikeOutput.hpp>
#include <api/BigInt.hpp>
#include <CppHttpLibClient.hpp>
#include <events/LambdaEventReceiver.hpp>
#include <soci.h>
#include <api/Account.hpp>
#include <api/BitcoinLikeAccount.hpp>

namespace ledger {
namespace testing {
namespace txes_to_wpkh {
extern core::api::ExtendedKeyAccountCreationInfo XPUB_INFO;

std::shared_ptr<core::BitcoinLikeAccount> inflate(const std::shared_ptr<core::WalletPool>& pool, const std::shared_ptr<core::AbstractWallet>& wallet);
}
}
}

#endif // LEDGER_FIXTURES_TXES_TO_WPKH
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GET https://explorers.api.live.ledger.com/blockchain/v2/btc/blocks/current
0
1
{"hash":"00000000000000000008db19835048ecdd79d0974f4bd1a1d5492a5502f1d61c","height":722923,"time":"2022-02-12T11:49:27Z","txs":[]}

GET https://explorers.api.live.ledger.com/timestamp
0
1
{"timestamp":1644666863}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GET https://explorers.api.live.ledger.com/blockchain/v2/btc/blocks/current
0
1
{"hash":"00000000000000000008db19835048ecdd79d0974f4bd1a1d5492a5502f1d61c","height":722923,"time":"2022-02-12T11:49:27Z","txs":[]}

GET https://explorers.api.live.ledger.com/timestamp
0
1
{"timestamp":1644666863}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GET https://explorers.api.live.ledger.com/blockchain/v2/btc/blocks/current
0
1
{"hash":"00000000000000000008db19835048ecdd79d0974f4bd1a1d5492a5502f1d61c","height":722923,"time":"2022-02-12T11:49:27Z","txs":[]}

GET https://explorers.api.live.ledger.com/timestamp
0
1
{"timestamp":1644666863}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GET https://explorers.api.live.ledger.com/blockchain/v2/btc/blocks/current
0
1
{"hash":"00000000000000000008db19835048ecdd79d0974f4bd1a1d5492a5502f1d61c","height":722923,"time":"2022-02-12T11:49:27Z","txs":[]}

GET https://explorers.api.live.ledger.com/timestamp
0
1
{"timestamp":1644666863}

Loading

0 comments on commit 6b441d3

Please sign in to comment.