Skip to content

Commit c6cce25

Browse files
committed
feat: implement bitcoin onchain allocator
1 parent bbc7c81 commit c6cce25

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/utils/onchain-allocator.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { JsonRpcProvider } from "@near-js/providers";
2+
import bitcoin from "bitcoinjs-lib";
23
import bs58 from "bs58";
34
import TronWeb from "tronweb";
45
import {
@@ -181,6 +182,7 @@ export const getSigner = async (chainId: string) => {
181182

182183
let domainId: number | undefined;
183184
switch (vmType) {
185+
case "bitcoin-vm":
184186
case "ethereum-vm":
185187
case "hyperliquid-vm":
186188
case "tron-vm": {
@@ -217,6 +219,29 @@ export const getSigner = async (chainId: string) => {
217219

218220
const [, publicKey] = result!.toString().split(":");
219221
switch (vmType) {
222+
case "bitcoin-vm": {
223+
const raw = Buffer.from(bs58.decode(publicKey));
224+
225+
const x = raw.subarray(0, 32);
226+
const y = raw.subarray(32, 64);
227+
const yIsEven = (y[31] & 1) === 0;
228+
const prefix = yIsEven ? 0x02 : 0x03;
229+
const pubKeyCompressed = Buffer.concat([
230+
Buffer.from([prefix]),
231+
Buffer.from(x),
232+
]);
233+
234+
_getSignerCache.set(
235+
chainId,
236+
bitcoin.payments.p2pkh({
237+
network: bitcoin.networks.bitcoin,
238+
pubkey: pubKeyCompressed,
239+
}).address!,
240+
);
241+
242+
break;
243+
}
244+
220245
case "ethereum-vm":
221246
case "hyperliquid-vm":
222247
case "tron-vm": {

0 commit comments

Comments
 (0)