Skip to content

Commit

Permalink
feat: BitcoinProxiedRpcProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Nov 4, 2024
1 parent e158fa3 commit f44e31a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
27 changes: 19 additions & 8 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
31 changes: 31 additions & 0 deletions src/BitcoinProxiedRpcProvider.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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,
});
}
}
5 changes: 4 additions & 1 deletion src/BitcoinRpcProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export class BitcoinRpcProvider implements BitcoinProvider {
};
}

private async callRpc(method: string, params: any[] = []): Promise<Response> {
protected async callRpc(
method: string,
params: any[] = []
): Promise<Response> {
const body = JSON.stringify({
jsonrpc: "1.0",
id: new Date().getTime(),
Expand Down
2 changes: 1 addition & 1 deletion src/UtuProvider.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "@/BitcoinProvider";
export * from "@/BitcoinProxiedRpcProvider";
export * from "@/BitcoinRpcProvider";
export * from "@/UtuProvider";
export * from "@/BitcoinTypes";
Expand Down

0 comments on commit f44e31a

Please sign in to comment.