Skip to content

Commit

Permalink
fix: Motoko Bitcoin example
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ielashi committed Aug 14, 2023
1 parent d54cd27 commit 584cf6f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
14 changes: 13 additions & 1 deletion motoko/basic_bitcoin/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
6 changes: 3 additions & 3 deletions motoko/basic_bitcoin/src/basic_bitcoin/basic_bitcoin.did
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ type bitcoin_address = text;
type block_hash = blob;

type network = variant {
Regtest;
Testnet;
Mainnet;
regtest;
testnet;
mainnet;
};

type outpoint = record {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions motoko/basic_bitcoin/src/basic_bitcoin/src/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
};
Expand Down
24 changes: 23 additions & 1 deletion motoko/basic_bitcoin/src/basic_bitcoin/src/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,4 +124,4 @@ module Types {
transaction : [Nat8];
network : Network;
};
}
}

0 comments on commit 584cf6f

Please sign in to comment.