From f44e31a578303c93ff6f1037b4a40acd15c60204 Mon Sep 17 00:00:00 2001 From: Iris Devillars Date: Mon, 4 Nov 2024 14:28:12 +0000 Subject: [PATCH] feat: BitcoinProxiedRpcProvider --- example/index.ts | 27 +++++++++++++++++++-------- src/BitcoinProxiedRpcProvider.ts | 31 +++++++++++++++++++++++++++++++ src/BitcoinRpcProvider.ts | 5 ++++- src/UtuProvider.ts | 2 +- src/index.ts | 1 + 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 src/BitcoinProxiedRpcProvider.ts diff --git a/example/index.ts b/example/index.ts index 46f4686..4a74dd3 100644 --- a/example/index.ts +++ b/example/index.ts @@ -1,16 +1,27 @@ -import { BitcoinRpcProvider, UtuProvider } from "bitcoin-on-starknet"; +import { BitcoinProxiedRpcProvider, UtuProvider } from "bitcoin-on-starknet"; +import { Account, RpcProvider } from "starknet"; async function main() { - // Initialize the Bitcoin RPC provider - const bitcoinProvider = new BitcoinRpcProvider({ - url: process.env.BITCOIN_RPC_URL, - username: process.env.BITCOIN_RPC_USERNAME, - password: process.env.BITCOIN_RPC_PASSWORD, - }); + // Initialize the Bitcoin RPC provider with a testing rpc graciously provided by LFG Labs + const bitcoinProvider = new BitcoinProxiedRpcProvider( + "https://btcrpc.lfg.rs/rpc" + ); const utuProvider = new UtuProvider(bitcoinProvider); + // Initialize Starknet provider + const starknetProvider = new RpcProvider({ + nodeUrl: "https://sepolia.rpc.starknet.id", + }); + + // Initialize a Starknet Account + const account = new Account( + starknetProvider, + process.env.STARKNET_ADDRESS as string, + process.env.STARKNET_PRIVATE_KEY as string + ); + try { - // Data that we are going to use + // This is our bitcoin deposit transaction const txId = "fa89c32152bf324cd1d47d48187f977c7e0f380f6f78132c187ce27923f62fcc"; const rawTransaction = await bitcoinProvider.getRawTransaction(txId, true); diff --git a/src/BitcoinProxiedRpcProvider.ts b/src/BitcoinProxiedRpcProvider.ts new file mode 100644 index 0000000..791dc8c --- /dev/null +++ b/src/BitcoinProxiedRpcProvider.ts @@ -0,0 +1,31 @@ +import { BitcoinRpcProvider } from "@/BitcoinRpcProvider"; + +export class BitcoinProxiedRpcProvider extends BitcoinRpcProvider { + private proxyUrl: string; + + constructor(proxyUrl: string) { + // Pass a dummy config to parent class + super({ url: "dummy" }); + this.proxyUrl = proxyUrl; + } + + protected override async callRpc( + method: string, + params: any[] = [] + ): Promise { + const body = JSON.stringify({ + jsonrpc: "1.0", + id: new Date().getTime(), + method: method, + params: params, + }); + + return fetch(this.proxyUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: body, + }); + } +} diff --git a/src/BitcoinRpcProvider.ts b/src/BitcoinRpcProvider.ts index 784908c..e47323c 100644 --- a/src/BitcoinRpcProvider.ts +++ b/src/BitcoinRpcProvider.ts @@ -19,7 +19,10 @@ export class BitcoinRpcProvider implements BitcoinProvider { }; } - private async callRpc(method: string, params: any[] = []): Promise { + protected async callRpc( + method: string, + params: any[] = [] + ): Promise { const body = JSON.stringify({ jsonrpc: "1.0", id: new Date().getTime(), diff --git a/src/UtuProvider.ts b/src/UtuProvider.ts index b30b0d5..6d50e3f 100644 --- a/src/UtuProvider.ts +++ b/src/UtuProvider.ts @@ -1,7 +1,7 @@ import { BitcoinProvider } from "./BitcoinProvider"; import { BlockHeightProof, RegisterBlocksTx } from "@/UtuTypes"; import { BlockHeader } from "./BitcoinTypes"; -import { BigNumberish, byteArray, ByteArray } from "starknet"; +import { BigNumberish, ByteArray } from "starknet"; const CONTRACT_ADDRESS = "0x064e21f88caa162294fdda7f73d67ad09b81419e97df3409a5eb13ba39b88c31"; diff --git a/src/index.ts b/src/index.ts index eed61af..dc6e890 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export * from "@/BitcoinProvider"; +export * from "@/BitcoinProxiedRpcProvider"; export * from "@/BitcoinRpcProvider"; export * from "@/UtuProvider"; export * from "@/BitcoinTypes";