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

feat: add Bitcoin plugin with Taproot and Ark #1553

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,9 @@ CRONOSZKEVM_PRIVATE_KEY=

# Fuel Ecosystem (FuelVM)
FUEL_WALLET_PRIVATE_KEY=

# Bitcoin Configuration
BITCOIN_PRIVATE_KEY= # Your Bitcoin private key in WIF format
BITCOIN_NETWORK= # Network type: bitcoin, testnet, regtest, signet, or mutinynet
ARK_SERVER_URL= # Ark protocol server URL
ARK_SERVER_PUBLIC_KEY= # Ark server public key for L2 operations
1 change: 1 addition & 0 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@elizaos/plugin-3d-generation": "workspace:*",
"@elizaos/plugin-fuel": "workspace:*",
"@elizaos/plugin-avalanche": "workspace:*",
"@elizaos/plugin-bitcoin": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
Expand Down
2 changes: 2 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import path from "path";
import { fileURLToPath } from "url";
import yargs from "yargs";
import net from "net";
import { bitcoinPlugin } from "@elizaos/plugin-bitcoin";

const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
Expand Down Expand Up @@ -600,6 +601,7 @@ export async function createAgent(
getSecret(character, "AVALANCHE_PRIVATE_KEY")
? avalanchePlugin
: null,
getSecret(character, "BITCOIN_PRIVATE_KEY") ? bitcoinPlugin : null,
].filter(Boolean),
providers: [],
actions: [],
Expand Down
29 changes: 29 additions & 0 deletions packages/plugin-bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@elizaos/plugin-bitcoin",
"version": "0.1.0",
"description": "Bitcoin on-chain plugin for Eliza",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"@elizaos/core": "workspace:*",
"@arklabs/wallet-sdk": "^0.0.1",
"node-fetch": "^2.6.9"
},
"devDependencies": {
"@types/node": "^20.8.7",
"@types/node-fetch": "^2.6.4",
"@vitest/coverage-v8": "^0.34.6",
"tsup": "^7.2.0",
"typescript": "^5.0.4",
"vitest": "^0.34.6"
}
}
70 changes: 70 additions & 0 deletions packages/plugin-bitcoin/src/actions/addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { IAgentRuntime, Memory, State, HandlerCallback } from "@elizaos/core";
import { BitcoinTaprootProvider } from "../providers/bitcoin";

export const showBitcoinAddressesAction = {
name: "SHOW_BITCOIN_ADDRESSES",
description: "Show Bitcoin wallet addresses",
similes: [
"SHOW_ADDRESSES",
"GET_ADDRESSES",
"DISPLAY_ADDRESSES",
"SHOW_WALLET",
],
handler: async (
runtime: IAgentRuntime,
_message: Memory,
_state: State,
_options: any,
callback?: HandlerCallback
) => {
try {
const bitcoinProvider = runtime.providers.find(
(p) => p instanceof BitcoinTaprootProvider
) as BitcoinTaprootProvider;

if (!bitcoinProvider) {
return {
text: "Bitcoin provider not found. Please check your configuration.",
};
}

const addresses = await bitcoinProvider.getAddress();

if (callback) {
callback({
text: `Here are your Bitcoin addresses:

⛓️ Onchain: ${addresses.onchain}
⚡️ Offchain: ${addresses.offchain || "Not configured"}`,
});
}

return {
text: `Here are your Bitcoin addresses:

⛓️ Onchain: ${addresses.onchain}
⚡️ Offchain: ${addresses.offchain || "Not configured"}`,
};
} catch (error) {
console.error("Error fetching addresses:", error);
return {
text: "Unable to fetch your Bitcoin addresses at the moment. Please try again later.",
};
}
},
validate: async (runtime: IAgentRuntime) => {
return runtime.providers.some(
(p) => p instanceof BitcoinTaprootProvider
);
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Show my Bitcoin addresses",
},
},
],
],
};
82 changes: 82 additions & 0 deletions packages/plugin-bitcoin/src/actions/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Action, IAgentRuntime, Memory, State } from "@elizaos/core";
import { BitcoinTaprootProvider } from "../providers/bitcoin";

export const balanceAction: Action = {
name: "BALANCE",
description: "Get Bitcoin wallet balance",
examples: [
[
{
user: "{{user1}}",
content: { text: "What's my Bitcoin balance?" },
},
],
[{ user: "{{user1}}", content: { text: "Show me my BTC balance" } }],
],
similes: [
"BALANCE",
"GETBALANCE",
"SHOWBALANCE",
"CHECKBALANCE",
"DISPLAYBALANCE",
"MYBALANCE",
"HOWMUCHBTC",
"WHATSMYBALANCE",
],

async validate(runtime: IAgentRuntime): Promise<boolean> {
return !!runtime.providers.find(
(p) => p instanceof BitcoinTaprootProvider
);
},

async handler(
runtime: IAgentRuntime,
_message: Memory,
_state: State,
_match: any
) {
try {
const provider = runtime.providers.find(
(p): p is BitcoinTaprootProvider =>
p instanceof BitcoinTaprootProvider
);

if (!provider) {
return {
text: "Bitcoin provider not found. Please check your configuration.",
};
}

const [balance, price] = await Promise.all([
provider.getWalletBalance(),
provider.getBitcoinPrice(),
]);

const onchainTotal = balance.onchain.total;
const offchainTotal = balance.offchain.total;
const total = balance.total;

const onchainUSD = (onchainTotal * price.USD.last) / 100_000_000;
const offchainUSD = (offchainTotal * price.USD.last) / 100_000_000;
const totalUSD = (total * price.USD.last) / 100_000_000;

return {
text: `Bitcoin Balance:
Total: ${total.toLocaleString()} sats (≈ $${totalUSD.toFixed(2)} USD)
├─ Onchain: ${onchainTotal.toLocaleString()} sats (≈ $${onchainUSD.toFixed(2)} USD)
│ ├─ Confirmed: ${balance.onchain.confirmed.toLocaleString()} sats
│ └─ Unconfirmed: ${balance.onchain.unconfirmed.toLocaleString()} sats
└─ Offchain: ${offchainTotal.toLocaleString()} sats (≈ $${offchainUSD.toFixed(2)} USD)
├─ Settled: ${balance.offchain.settled.toLocaleString()} sats
├─ Pending: ${balance.offchain.pending.toLocaleString()} sats
└─ Swept: ${balance.offchain.swept.toLocaleString()} sats`,
};
} catch (error) {
console.error("Error in balance action:", error);
return {
text: "Unable to fetch your Bitcoin balance at the moment. Please try again later.",
};
}
},
};
83 changes: 83 additions & 0 deletions packages/plugin-bitcoin/src/actions/coins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Action, IAgentRuntime } from "@elizaos/core";
import { BitcoinTaprootProvider } from "../providers/bitcoin";

export const coinsAction: Action = {
name: "COINS",
description: "List your Bitcoin UTXOs",
similes: ["LISTCOINS", "LISTUTXOS", "SHOWUTXOS"],
examples: [
[{ user: "{{user1}}", content: { text: "Show my coins" } }],
[{ user: "{{user1}}", content: { text: "List UTXOs" } }],
],
validate: async (runtime: IAgentRuntime) => {
return !!runtime.providers.find(
(p) => p instanceof BitcoinTaprootProvider
);
},
handler: async (runtime: IAgentRuntime) => {
try {
const provider = runtime.providers.find(
(p) => p instanceof BitcoinTaprootProvider
) as BitcoinTaprootProvider;

if (!provider) {
return {
text: "Bitcoin provider not found.\nPlease check your configuration.",
};
}

const coins = await provider.getCoins();

if (!coins || coins.length === 0) {
return {
text: "Bitcoin UTXOs:\n\n└─ No confirmed UTXOs\n└─ No unconfirmed UTXOs",
};
}

const confirmedCoins = coins.filter(
(coin) => coin.status.confirmed
);
const unconfirmedCoins = coins.filter(
(coin) => !coin.status.confirmed
);

const confirmedTotal = confirmedCoins.reduce(
(sum, coin) => sum + coin.value,
0
);
const unconfirmedTotal = unconfirmedCoins.reduce(
(sum, coin) => sum + coin.value,
0
);

let output = "Bitcoin UTXOs:\n\n";

// Add confirmed UTXOs
output += `Confirmed UTXOs (Total: ${confirmedTotal} sats):\n`;
if (confirmedCoins.length === 0) {
output += "└─ No confirmed UTXOs\n";
} else {
confirmedCoins.forEach((coin) => {
output += `└─ ${coin.txid}:${coin.vout} (${coin.value} sats)\n`;
});
}

// Add unconfirmed UTXOs
output += `\nUnconfirmed UTXOs (Total: ${unconfirmedTotal} sats):\n`;
if (unconfirmedCoins.length === 0) {
output += "└─ No unconfirmed UTXOs\n";
} else {
unconfirmedCoins.forEach((coin) => {
output += `└─ ${coin.txid}:${coin.vout} (${coin.value} sats)\n`;
});
}

return { text: output };
} catch (error) {
console.error("Error in coins action:", error);
return {
text: "Unable to fetch your Bitcoin coins at the moment. Please try again later.",
};
}
},
};
Loading
Loading