From 584cf6f3a732d1a057e11e9df0dd65e6dbb427e2 Mon Sep 17 00:00:00 2001 From: Islam El-Ashi Date: Mon, 14 Aug 2023 09:57:34 +0200 Subject: [PATCH 1/3] fix: Motoko Bitcoin example * Update dfx.json to set the Bitcoin flag to true * Switch casing of the network from camel case to snake case, which is more in line with the specification. --- motoko/basic_bitcoin/dfx.json | 14 ++++++++++- .../src/basic_bitcoin/basic_bitcoin.did | 6 ++--- .../src/basic_bitcoin/src/BitcoinWallet.mo | 2 +- .../src/basic_bitcoin/src/Main.mo | 8 +++---- .../src/basic_bitcoin/src/Types.mo | 24 ++++++++++++++++++- 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/motoko/basic_bitcoin/dfx.json b/motoko/basic_bitcoin/dfx.json index 89a8174be..9c96c75dc 100644 --- a/motoko/basic_bitcoin/dfx.json +++ b/motoko/basic_bitcoin/dfx.json @@ -6,9 +6,21 @@ } }, "defaults": { + "bitcoin": { + "enabled": true, + "nodes": [ + "127.0.0.1:18444" + ], + "log_level": "info" + }, "build": { "packtool": "", "args": "" } + }, + "networks": { + "local": { + "bind": "127.0.0.1:4943" + } } -} \ No newline at end of file +} diff --git a/motoko/basic_bitcoin/src/basic_bitcoin/basic_bitcoin.did b/motoko/basic_bitcoin/src/basic_bitcoin/basic_bitcoin.did index cb26dc90f..4f05ab0c6 100644 --- a/motoko/basic_bitcoin/src/basic_bitcoin/basic_bitcoin.did +++ b/motoko/basic_bitcoin/src/basic_bitcoin/basic_bitcoin.did @@ -7,9 +7,9 @@ type bitcoin_address = text; type block_hash = blob; type network = variant { - Regtest; - Testnet; - Mainnet; + regtest; + testnet; + mainnet; }; type outpoint = record { diff --git a/motoko/basic_bitcoin/src/basic_bitcoin/src/BitcoinWallet.mo b/motoko/basic_bitcoin/src/basic_bitcoin/src/BitcoinWallet.mo index 3a0fd6514..d8a557d63 100644 --- a/motoko/basic_bitcoin/src/basic_bitcoin/src/BitcoinWallet.mo +++ b/motoko/basic_bitcoin/src/basic_bitcoin/src/BitcoinWallet.mo @@ -213,7 +213,7 @@ public func build_transaction( let public_key = public_key_bytes_to_public_key(public_key_bytes); // Compute the P2PKH address from our public key. - P2pkh.deriveAddress(network, Publickey.toSec1(public_key, true)) + P2pkh.deriveAddress(Types.network_to_network_camel_case(network), Publickey.toSec1(public_key, true)) }; // A mock for rubber-stamping ECDSA signatures. diff --git a/motoko/basic_bitcoin/src/basic_bitcoin/src/Main.mo b/motoko/basic_bitcoin/src/basic_bitcoin/src/Main.mo index b918f3a85..315c9e23b 100644 --- a/motoko/basic_bitcoin/src/basic_bitcoin/src/Main.mo +++ b/motoko/basic_bitcoin/src/basic_bitcoin/src/Main.mo @@ -15,9 +15,9 @@ actor class BasicBitcoin(_network : Types.Network) { // The Bitcoin network to connect to. // - // When developing locally this should be `Regtest`. - // When deploying to the IC this should be `Testnet`. - // `Mainnet` is currently unsupported. + // When developing locally this should be `regtest`. + // When deploying to the IC this should be `testnet`. + // `mainnet` is currently unsupported. stable let NETWORK : Network = _network; // The derivation path to use for ECDSA secp256k1. @@ -26,7 +26,7 @@ actor class BasicBitcoin(_network : Types.Network) { // The ECDSA key name. let KEY_NAME : Text = switch NETWORK { // For local development, we use a special test key with dfx. - case (#Regtest) "dfx_test_key"; + case (#regtest) "dfx_test_key"; // On the IC we're using a test ECDSA key. case _ "test_key_1" }; diff --git a/motoko/basic_bitcoin/src/basic_bitcoin/src/Types.mo b/motoko/basic_bitcoin/src/basic_bitcoin/src/Types.mo index de339513e..a2f7aeb1d 100644 --- a/motoko/basic_bitcoin/src/basic_bitcoin/src/Types.mo +++ b/motoko/basic_bitcoin/src/basic_bitcoin/src/Types.mo @@ -47,11 +47,33 @@ module Types { /// The type of Bitcoin network the dapp will be interacting with. public type Network = { + #mainnet; + #testnet; + #regtest; + }; + + /// The type of Bitcoin network as defined by the Bitcoin Motoko library + /// (Note the difference in casing compared to `Network`) + public type NetworkCamelCase = { #Mainnet; #Testnet; #Regtest; }; + public func network_to_network_camel_case(network: Network) : NetworkCamelCase { + switch (network) { + case (#regtest) { + #Regtest + }; + case (#testnet) { + #Testnet + }; + case (#mainnet) { + #Mainnet + }; + } + }; + /// A reference to a transaction output. public type OutPoint = { txid : Blob; @@ -102,4 +124,4 @@ module Types { transaction : [Nat8]; network : Network; }; -} \ No newline at end of file +} From 9f5312961b15654ad6611b0facf08937b911b834 Mon Sep 17 00:00:00 2001 From: Moritz Fuller <32162112+letmejustputthishere@users.noreply.github.com> Date: Mon, 14 Aug 2023 13:01:08 +0200 Subject: [PATCH 2/3] Update motoko-basic-bitcoin.yaml updated ci to reflect latest changes to example --- .github/workflows/motoko-basic-bitcoin.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/motoko-basic-bitcoin.yaml b/.github/workflows/motoko-basic-bitcoin.yaml index be2fea105..3364ceecf 100644 --- a/.github/workflows/motoko-basic-bitcoin.yaml +++ b/.github/workflows/motoko-basic-bitcoin.yaml @@ -25,7 +25,7 @@ jobs: run: | dfx start --background pushd motoko/basic_bitcoin - dfx deploy basic_bitcoin --argument '(variant { Regtest })' + dfx deploy basic_bitcoin --argument '(variant { regtest })' popd rust-basic-bitcoin-linux: runs-on: ubuntu-20.04 From 29766a351dcf1781b65947b11c3e2c8ebef61581 Mon Sep 17 00:00:00 2001 From: Moritz Fuller <32162112+letmejustputthishere@users.noreply.github.com> Date: Mon, 14 Aug 2023 13:08:32 +0200 Subject: [PATCH 3/3] Update motoko-basic-bitcoin.yaml --- .github/workflows/motoko-basic-bitcoin.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/motoko-basic-bitcoin.yaml b/.github/workflows/motoko-basic-bitcoin.yaml index 3364ceecf..cdf457627 100644 --- a/.github/workflows/motoko-basic-bitcoin.yaml +++ b/.github/workflows/motoko-basic-bitcoin.yaml @@ -39,5 +39,5 @@ jobs: run: | dfx start --background pushd motoko/basic_bitcoin - dfx deploy basic_bitcoin --argument '(variant { Regtest })' + dfx deploy basic_bitcoin --argument '(variant { regtest })' popd