Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update basic_bitcoin example to use bitcoinjs-lib #1775

Merged
merged 17 commits into from
May 20, 2024
Merged
2 changes: 0 additions & 2 deletions examples/basic_bitcoin/.gitignore
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
.azle
.bitcoin
.dfx
*.wasm
*.wasm.gz
dfx_generated
node_modules
20 changes: 10 additions & 10 deletions examples/basic_bitcoin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ This is an implementation of the [basic_bitcoin example](https://github.com/dfin
## bitcoind

```bash
mkdir .bitcoin
mkdir .bitcoin/data

curl https://bitcoincore.org/bin/bitcoin-core-23.0/bitcoin-23.0-x86_64-linux-gnu.tar.gz -o bitcoin.tar.gz

tar xzf bitcoin.tar.gz --overwrite --strip-components=1 --directory=.bitcoin/ bitcoin-23.0/bin/

rm -rf bitcoin.tar.gz
mkdir -p .bitcoin/data

# Check if bitcoind executable exists; if not, download and extract it
if [ ! -f ".bitcoin/bin/bitcoind" ]; then
curl -o bitcoin.tar.gz https://bitcoincore.org/bin/bitcoin-core-23.0/bitcoin-23.0-x86_64-linux-gnu.tar.gz
tar xzf bitcoin.tar.gz --overwrite --strip-components=1 --directory=.bitcoin/ bitcoin-23.0/bin/
rm -rf bitcoin.tar.gz
fi
```

# Deployment
Expand All @@ -28,13 +28,13 @@ rm -rf bitcoin.tar.gz

```bash
# Do this in its own terminal
dfx start --clean --host 127.0.0.1:8000 --enable-bitcoin
dfx start --clean --host 127.0.0.1:8000
```

## basic_bitcion

```bash
dfx deploy --argument='(variant { Regtest })'
BITCOIN_NETWORK=regtest dfx deploy'
```

# Usage
Expand Down
23 changes: 4 additions & 19 deletions examples/basic_bitcoin/dfx.json
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
{
"canisters": {
"backend": {
"type": "custom",
"main": "src/backend/index.ts",
"candid": "src/backend/index.did",
"init_arg": "(variant { regtest })",
"build": "npx azle backend",
"env": ["BITCOIN_NETWORK"],
"wasm": ".azle/backend/backend.wasm",
"gzip": true,
"metadata": [
{
"name": "candid:service",
"path": "src/backend/index.did"
},
{
"name": "cdk:name",
"content": "azle"
}
]
"basic_bitcoin": {
"type": "azle",
"main": "src/index.ts",
"env": ["BITCOIN_NETWORK"]
}
},
"networks": {
Expand Down
82 changes: 1 addition & 81 deletions examples/basic_bitcoin/package-lock.json

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

12 changes: 3 additions & 9 deletions examples/basic_bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
{
"scripts": {
"bitcoin": ".bitcoin/bin/bitcoind -conf=$(pwd)/.bitcoin.conf -datadir=$(pwd)/.bitcoin/data --port=18444",
"ic": "dfx start --clean --host 127.0.0.1:8000 --enable-bitcoin",
"install": "./scripts/install.sh",
"deploy": "dfx deploy backend --specified-id bkyz2-fmaaa-aaaaa-qaaaq-cai",
"pre_tests": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "BITCOIN_NETWORK=regtest npm run pre_tests && ts-node --transpile-only --ignore=false test/test.ts"
"pretest": "BITCOIN_NETWORK=regtest ts-node --transpile-only --ignore=false test/pretest.ts",
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
"test": "ts-node --transpile-only --ignore=false test/test.ts"
},
"dependencies": {
"@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3",
"azle": "^0.21.1",
"bitcoinjs-lib": "^6.1.5",
"ecpair": "^2.1.0",
"express": "^4.18.2",
"tiny-secp256k1": "^2.2.3"
"express": "^4.18.2"
},
"devDependencies": {
"@dfinity/agent": "^0.19.2",
Expand Down
10 changes: 1 addition & 9 deletions examples/basic_bitcoin/scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#!/bin/bash

# Check if .bitcoin directory exists; if not, create it
if [ ! -d ".bitcoin" ]; then
mkdir .bitcoin
fi

# Check if .bitcoin/data directory exists; if not, create it
if [ ! -d ".bitcoin/data" ]; then
mkdir .bitcoin/data
fi
mkdir -p .bitcoin/data

# Check if bitcoind executable exists; if not, download and extract it
if [ ! -f ".bitcoin/bin/bitcoind" ]; then
Expand Down
4 changes: 0 additions & 4 deletions examples/basic_bitcoin/src/backend/index.did
lastmjs marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

9 changes: 0 additions & 9 deletions examples/basic_bitcoin/src/backend/types.ts
lastmjs marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { blob, nat64, serialize, Vec } from 'azle';
import { serialize } from 'azle';
import {
BitcoinNetwork,
GetUtxosResult,
MillisatoshiPerByte
} from 'azle/canisters/management/bitcoin';

// The fees for the various bitcoin endpoints.
const GET_BALANCE_COST_CYCLES: nat64 = 100_000_000n;
const GET_UTXOS_COST_CYCLES: nat64 = 10_000_000_000n;
const GET_CURRENT_FEE_PERCENTILES_CYCLES: nat64 = 100_000_000n;
const SEND_TRANSACTION_BASE_CYCLES: nat64 = 5_000_000_000n;
const SEND_TRANSACTION_PER_BYTE_CYCLES: nat64 = 20_000_000n;
const GET_BALANCE_COST_CYCLES: bigint = 100_000_000n;
const GET_UTXOS_COST_CYCLES: bigint = 10_000_000_000n;
const GET_CURRENT_FEE_PERCENTILES_CYCLES: bigint = 100_000_000n;
const SEND_TRANSACTION_BASE_CYCLES: bigint = 5_000_000_000n;
const SEND_TRANSACTION_PER_BYTE_CYCLES: bigint = 20_000_000n;

/// Returns the balance of the given bitcoin address.
///
Expand All @@ -20,7 +20,7 @@ export async function getBalance(
network: BitcoinNetwork,
address: string
): Promise<bigint> {
const response = await fetch(`icp://aaaaa-aa/bitcoin_get_balance`, {
const balanceRes = await fetch(`icp://aaaaa-aa/bitcoin_get_balance`, {
body: serialize({
args: [
{
Expand All @@ -32,20 +32,18 @@ export async function getBalance(
cycles: GET_BALANCE_COST_CYCLES
})
});
return await response.json();
return await balanceRes.json();
}

/// Returns the UTXOs of the given bitcoin address.
///
/// NOTE: Pagination is ignored in this example. If an address has many thousands
/// of UTXOs, then subsequent calls to `bitcoin_get_utxos` are required.
///
/// NOTE: Relies on the `bitcoin_get_utxos` endpoint.
/// See https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_utxos
export async function getUtxos(
network: BitcoinNetwork,
address: string
): Promise<GetUtxosResult> {
const response = await fetch(`icp://aaaaa-aa/bitcoin_get_utxos`, {
const utxoRes = await fetch(`icp://aaaaa-aa/bitcoin_get_utxos`, {
body: serialize({
args: [
{
Expand All @@ -57,7 +55,7 @@ export async function getUtxos(
cycles: GET_UTXOS_COST_CYCLES
})
});
return await response.json();
return await utxoRes.json();
}

/// Returns the 100 fee percentiles measured in millisatoshi/byte.
Expand All @@ -67,8 +65,8 @@ export async function getUtxos(
/// See https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_current_fee_percentiles
export async function getCurrentFeePercentiles(
network: BitcoinNetwork
): Promise<Vec<MillisatoshiPerByte>> {
const response = await fetch(
): Promise<MillisatoshiPerByte[]> {
const res = await fetch(
`icp://aaaaa-aa/bitcoin_get_current_fee_percentiles`,
{
body: serialize({
Expand All @@ -81,7 +79,7 @@ export async function getCurrentFeePercentiles(
})
}
);
return await response.json();
return await res.json();
}

/// Sends a (signed) transaction to the bitcoin network.
Expand All @@ -90,7 +88,7 @@ export async function getCurrentFeePercentiles(
/// See https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_send_transaction
export async function sendTransaction(
network: BitcoinNetwork,
transaction: blob
transaction: Uint8Array
): Promise<void> {
const transactionFee =
SEND_TRANSACTION_BASE_CYCLES +
Expand Down
Loading
Loading