@coin-voyage/crypto 2.2.3 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { type Config, CreateConnectorFn } from "@bigmi/client";
2
- import { HttpTransportConfig } from "@bigmi/core";
2
+ import { type HttpTransportConfig } from "@bigmi/core";
3
3
  /**
4
4
  * UTXO Configuration for Wallets and Connectors
5
5
  * Includes options for Bigmi-specific configurations
@@ -1,6 +1,6 @@
1
1
  import { createConfig, leather, magicEden, okx, onekey, phantom, unisat, xverse, } from "@bigmi/client";
2
- import { bitcoin, createClient, http } from "@bigmi/core";
3
- import { ALCHEMY_KEY } from "../evm/utils";
2
+ import { bitcoin, createClient } from "@bigmi/core";
3
+ import { createUTXOTransport } from "./create-utxo-transport";
4
4
  export function createDefaultBigmiConfig(props = {
5
5
  additionalConfigOptions: { multiInjectedProviderDiscovery: false },
6
6
  }) {
@@ -19,11 +19,10 @@ export function createDefaultBigmiConfig(props = {
19
19
  connectors,
20
20
  client({ chain }) {
21
21
  const chainRpcConfig = props?.rpcConfig;
22
- let config = [http(), http(`https://bitcoin-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}`)];
23
- if (chainRpcConfig) {
24
- config = [http(chainRpcConfig.url, chainRpcConfig.config), http()];
25
- }
26
- return createClient({ chain, transport: config[0] });
22
+ return createClient({
23
+ chain,
24
+ transport: createUTXOTransport(chainRpcConfig),
25
+ });
27
26
  },
28
27
  ...props?.additionalConfigOptions,
29
28
  });
@@ -40,7 +40,9 @@ export async function createPsbtTx(client, fromAddress, toAddress, amountSats) {
40
40
  txid: utxo.txId,
41
41
  index: utxo.vout,
42
42
  sequence: 0xfffffffd, // RBF
43
- ...(addressType === AddressType.p2wpkh || addressType === AddressType.p2wsh
43
+ ...(addressType === AddressType.p2wpkh ||
44
+ addressType === AddressType.p2wsh ||
45
+ addressType === AddressType.p2tr
44
46
  ? {
45
47
  witnessUtxo: {
46
48
  script: scripts.script,
@@ -0,0 +1,6 @@
1
+ import { type HttpTransportConfig } from "@bigmi/core";
2
+ export interface UTXORpcConfig {
3
+ url?: string | undefined;
4
+ config?: HttpTransportConfig;
5
+ }
6
+ export declare function createUTXOTransport(rpcConfig?: UTXORpcConfig): import("@bigmi/core").FallbackTransport<readonly [import("@bigmi/core").HttpTransport<undefined, false>, import("@bigmi/core").HttpTransport<import("@bigmi/core").RpcSchema, false>, import("@bigmi/core").HttpTransport<undefined, false>, import("@bigmi/core").HttpTransport<undefined, false>, import("@bigmi/core").HttpTransport<undefined, false>, import("@bigmi/core").HttpTransport<undefined, false>]>;
@@ -0,0 +1,10 @@
1
+ import { ankr, blockchair, blockcypher, fallback, http, mempool, } from "@bigmi/core";
2
+ import { ALCHEMY_KEY } from "../evm/utils";
3
+ export function createUTXOTransport(rpcConfig) {
4
+ const primaryRpc = rpcConfig
5
+ ? http(rpcConfig.url, rpcConfig.config)
6
+ : http(`https://bitcoin-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}`);
7
+ return fallback([primaryRpc, http(), blockchair(), blockcypher(), mempool(), ankr({
8
+ apiKey: "2a2801137db177be9c80522e690ec8b59ffc5260d030b2671b5e3936b917daac"
9
+ })]);
10
+ }
@@ -1,4 +1,17 @@
1
+ import { Account } from "@bigmi/core";
1
2
  import { UTXOConnectorId } from "../types/utxo-connector-id";
3
+ type PhantomBitcoinProvider = {
4
+ isPhantom?: boolean;
5
+ requestAccounts(): Promise<Account[]>;
6
+ signPSBT(psbt: Uint8Array, options: {
7
+ inputsToSign: {
8
+ address: string;
9
+ signingIndexes: number[];
10
+ sigHash?: number;
11
+ }[];
12
+ finalize?: boolean;
13
+ }): Promise<Uint8Array>;
14
+ };
2
15
  declare global {
3
16
  interface Window {
4
17
  LeatherProvider?: any;
@@ -8,7 +21,7 @@ declare global {
8
21
  bitcoin?: any;
9
22
  };
10
23
  phantom?: {
11
- bitcoin?: any;
24
+ bitcoin?: PhantomBitcoinProvider;
12
25
  };
13
26
  xfi?: {
14
27
  bitcoin?: any;
@@ -1,5 +1,6 @@
1
1
  import { sendUTXOTransaction } from "@bigmi/core";
2
2
  import { ChainId } from "@coin-voyage/shared/types";
3
+ import { hex } from "@scure/base";
3
4
  import { Psbt } from "bitcoinjs-lib";
4
5
  import { createUnsecuredToken } from "jsontokens";
5
6
  import { withTimeout } from "viem";
@@ -139,30 +140,22 @@ async function sendBtcPhantom(recipient) {
139
140
  }
140
141
  const publicClient = await getUTXOPublicClient(ChainId.BTC);
141
142
  const tx = await createPsbtTx(publicClient, paymentAccount, recipient.address, Number(recipient.amount));
142
- const payAddress = paymentAccount.address;
143
- const inputsToSign = new Map();
144
- for (let i = 0; i < tx.inputsLength; i++) {
145
- const input = tx.getInput(i);
146
- if (inputsToSign.has(payAddress)) {
147
- inputsToSign.get(payAddress).signingIndexes.push(input.index);
148
- }
149
- else {
150
- inputsToSign.set(payAddress, {
151
- address: payAddress,
152
- sigHash: 0,
153
- signingIndexes: [input.index],
154
- });
155
- }
156
- }
143
+ const psbtBytes = tx.toPSBT();
144
+ const inputsToSign = [
145
+ {
146
+ address: paymentAccount.address,
147
+ signingIndexes: Array.from({ length: tx.inputsLength }, (_, index) => index),
148
+ },
149
+ ];
157
150
  try {
158
151
  // We give users 10 minutes to sign the transaction or it should be considered expired
159
- const signedPsbtHex = await withTimeout(() => provider.signPSBT(tx.unsignedTx, {
160
- inputsToSign: Array.from(inputsToSign.values()),
152
+ const signedPsbtBytes = await withTimeout(() => provider.signPSBT(psbtBytes, {
153
+ inputsToSign,
161
154
  }), {
162
155
  timeout: 600000,
163
156
  errorInstance: new Error("Transaction has expired."),
164
157
  });
165
- const signedPsbt = Psbt.fromHex(signedPsbtHex).finalizeAllInputs();
158
+ const signedPsbt = Psbt.fromHex(hex.encode(signedPsbtBytes)).finalizeAllInputs();
166
159
  const txHex = signedPsbt.extractTransaction().toHex();
167
160
  // const signedTx = btc.Transaction.fromRaw(signedPSBTBytes)
168
161
  // signedTx.finalize()
@@ -1,7 +1,10 @@
1
- import { type UTXOSchema, Chain, Client, FallbackTransport, HttpTransport, PublicActions, WalletActions } from "@bigmi/core";
1
+ import { type Client, type PublicActions, type UTXOSchema, type WalletActions, bitcoin } from "@bigmi/core";
2
+ import { createUTXOTransport } from "./create-utxo-transport";
3
+ type UTXOPublicClient = Client<ReturnType<typeof createUTXOTransport>, typeof bitcoin, undefined, UTXOSchema, PublicActions & WalletActions>;
2
4
  /**
3
5
  * Get an instance of a provider for a specific chain
4
6
  * @param chainId - Id of the chain the provider is for
5
7
  * @returns The public client for the given chain
6
8
  */
7
- export declare const getUTXOPublicClient: (chainId: number) => Promise<Client<FallbackTransport<readonly HttpTransport[]>, Chain, undefined, UTXOSchema, PublicActions & WalletActions>>;
9
+ export declare const getUTXOPublicClient: (chainId: number) => Promise<UTXOPublicClient>;
10
+ export {};
@@ -1,5 +1,15 @@
1
- import { ankr, bitcoin, blockchair, blockcypher, createClient, fallback, mempool, publicActions, rpcSchema, walletActions, } from "@bigmi/core";
2
- // cached providers
1
+ import { bitcoin, createClient, publicActions, rpcSchema, walletActions, } from "@bigmi/core";
2
+ import { createUTXOTransport } from "./create-utxo-transport";
3
+ function createUTXOPublicClient() {
4
+ return createClient({
5
+ chain: bitcoin,
6
+ rpcSchema: rpcSchema(),
7
+ transport: createUTXOTransport(),
8
+ pollingInterval: 10000,
9
+ })
10
+ .extend(publicActions)
11
+ .extend(walletActions);
12
+ }
3
13
  const publicClients = {};
4
14
  /**
5
15
  * Get an instance of a provider for a specific chain
@@ -8,15 +18,7 @@ const publicClients = {};
8
18
  */
9
19
  export const getUTXOPublicClient = async (chainId) => {
10
20
  if (!publicClients[chainId]) {
11
- const client = createClient({
12
- chain: bitcoin,
13
- rpcSchema: rpcSchema(),
14
- transport: fallback([blockchair(), blockcypher(), mempool(), ankr()]),
15
- pollingInterval: 10000,
16
- })
17
- .extend(publicActions)
18
- .extend(walletActions);
19
- publicClients[chainId] = client;
21
+ publicClients[chainId] = createUTXOPublicClient();
20
22
  }
21
23
  if (!publicClients[chainId]) {
22
24
  throw new Error(`Unable to configure provider for chain ${chainId}`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coin-voyage/crypto",
3
3
  "description": "Crypto utilities for Coin Voyage",
4
- "version": "2.2.3",
4
+ "version": "2.3.0",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "author": "Lars <lars@coinvoyage.io>",
@@ -50,7 +50,7 @@
50
50
  "@solana/wallet-adapter-walletconnect": "0.1.21",
51
51
  "@solana/wallet-adapter-base": "0.9.27",
52
52
  "@solana/wallet-adapter-coinbase": "0.1.23",
53
- "@coin-voyage/shared": "2.2.5"
53
+ "@coin-voyage/shared": "2.3.1"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@types/elliptic": "6.4.18"