diff --git a/packages/sdk/src/addresses/index.ts b/packages/sdk/src/addresses/index.ts index af8ed53f..327579ae 100644 --- a/packages/sdk/src/addresses/index.ts +++ b/packages/sdk/src/addresses/index.ts @@ -1,8 +1,9 @@ import * as ecc from "@bitcoinerlab/secp256k1" import BIP32Factory, { BIP32Interface } from "bip32" -import { Network } from "../config/types" -import { createTransaction, getDerivationPath, getNetwork, toXOnly } from "../utils" +import { Network } from "~/config/types" +import { createTransaction, getDerivationPath, getNetwork, toXOnly } from "~/utils" + import { AddressFormats, addressFormats, addressNameToType, AddressTypes, addressTypeToName } from "./formats" export function getAddressFormat(address: string, network: Network) { diff --git a/packages/sdk/src/api/types.ts b/packages/sdk/src/api/types.ts index b0d15681..14c81a14 100644 --- a/packages/sdk/src/api/types.ts +++ b/packages/sdk/src/api/types.ts @@ -45,6 +45,7 @@ export type GetInscriptionsOptions = RequireAtLeastOne<{ }> & { sort?: "asc" | "desc" limit?: number + include?: ["value"] next?: string | null decodeMetadata?: boolean } diff --git a/packages/sdk/src/browser-wallets/metamask/addresses.ts b/packages/sdk/src/browser-wallets/metamask/addresses.ts index 02914862..e1713408 100644 --- a/packages/sdk/src/browser-wallets/metamask/addresses.ts +++ b/packages/sdk/src/browser-wallets/metamask/addresses.ts @@ -1,7 +1,9 @@ import { ethers } from "ethers" -import { getAddressesFromPublicKey, getDerivedNode } from "../.." -import { Network } from "../../config/types" +import { getAddressesFromPublicKey } from "~/addresses" +import { Network } from "~/config/types" +import { getDerivedNode } from "~/keys" + import { isMetaMaskInstalled } from "./utils" export async function getAddresses({ path, network = "testnet" }: GetMetaMaskAddressesOptions) { if (!isMetaMaskInstalled()) { diff --git a/packages/sdk/src/browser-wallets/metamask/signatures.ts b/packages/sdk/src/browser-wallets/metamask/signatures.ts index c6087675..07c68d2d 100644 --- a/packages/sdk/src/browser-wallets/metamask/signatures.ts +++ b/packages/sdk/src/browser-wallets/metamask/signatures.ts @@ -2,8 +2,9 @@ import { Psbt } from "bitcoinjs-lib" import { sign } from "bitcoinjs-message" import { ethers } from "ethers" -import { createTransaction } from "../.." -import { Network } from "../../config/types" +import { Network } from "~/config/types" +import { createTransaction } from "~/utils" + import { getDerivedNodeFromMetaMaskSignature } from "./addresses" import { isMetaMaskInstalled } from "./utils" diff --git a/packages/sdk/src/browser-wallets/unisat/addresses.ts b/packages/sdk/src/browser-wallets/unisat/addresses.ts index 6693be22..960a5418 100644 --- a/packages/sdk/src/browser-wallets/unisat/addresses.ts +++ b/packages/sdk/src/browser-wallets/unisat/addresses.ts @@ -1,5 +1,6 @@ -import { getAddressFormat } from "../.." -import { Network } from "../../config/types" +import { getAddressFormat } from "~/addresses" +import { Network } from "~/config/types" + import { isUnisatInstalled, UnisatNetwork } from "./utils" export async function getAddresses(network: Network) { diff --git a/packages/sdk/src/browser-wallets/xverse/addresses.ts b/packages/sdk/src/browser-wallets/xverse/addresses.ts index f8a3f898..767ab681 100644 --- a/packages/sdk/src/browser-wallets/xverse/addresses.ts +++ b/packages/sdk/src/browser-wallets/xverse/addresses.ts @@ -1,7 +1,8 @@ import { AddressPurposes, getAddress } from "sats-connect" -import { getAddressFormat } from "../../addresses" -import { Network } from "../../config/types" +import { getAddressFormat } from "~/addresses" +import { Network } from "~/config/types" + import { fromXOnlyToFullPubkey, isXverseInstalled, XverseNetwork } from "./utils" export async function getAddresses(options: XverseGetAddressOptions) { options.network = options.network ?? "testnet" diff --git a/packages/sdk/src/browser-wallets/xverse/signatures.ts b/packages/sdk/src/browser-wallets/xverse/signatures.ts index d4bf60af..39fa7d27 100644 --- a/packages/sdk/src/browser-wallets/xverse/signatures.ts +++ b/packages/sdk/src/browser-wallets/xverse/signatures.ts @@ -1,8 +1,9 @@ import { Psbt } from "bitcoinjs-lib" import { signMessage as _signMessage, signTransaction } from "sats-connect" -import { Network } from "../../config/types" -import { InputsToSign } from "../../inscription/types" +import { Network } from "~/config/types" +import { InputsToSign } from "~/inscription/types" + import { BrowserWalletSignPSBTResponse } from "../types" import { isXverseInstalled, XverseNetwork } from "./utils" diff --git a/packages/sdk/src/config/index.ts b/packages/sdk/src/config/index.ts index b4c2de40..e3f71d06 100644 --- a/packages/sdk/src/config/index.ts +++ b/packages/sdk/src/config/index.ts @@ -19,6 +19,4 @@ export const apiConfig = { } } -// Input from seller PSBT when unwrapped & merged, -// is placed on the 2nd index in instant-buy-sell flow -export const INSTANT_BUY_SELLER_INPUT_INDEX = 2 +export type * from "./types" diff --git a/packages/sdk/src/constants/index.ts b/packages/sdk/src/constants/index.ts index a9618db1..18959d20 100644 --- a/packages/sdk/src/constants/index.ts +++ b/packages/sdk/src/constants/index.ts @@ -4,3 +4,7 @@ export const MINIMUM_AMOUNT_IN_SATS = 600 // Fee calculated by the fee estimator cannot be greater than 0.05 BTC in any case export const MAXIMUM_FEE = 5000000 + +// Input from seller PSBT when unwrapped & merged, +// is placed on the 2nd index in instant-buy-sell flow +export const INSTANT_BUY_SELLER_INPUT_INDEX = 2 diff --git a/packages/sdk/src/fee/FeeEstimator.ts b/packages/sdk/src/fee/FeeEstimator.ts index b15b697c..4fe03f79 100644 --- a/packages/sdk/src/fee/FeeEstimator.ts +++ b/packages/sdk/src/fee/FeeEstimator.ts @@ -1,11 +1,13 @@ import { Psbt } from "bitcoinjs-lib" -import { AddressFormats, getNetwork, getScriptType } from ".." -import { Network } from "../config/types" -import { MAXIMUM_FEE } from "../constants" +import { AddressFormats } from "~/addresses" +import { Network } from "~/config/types" +import { MAXIMUM_FEE } from "~/constants" +import { getNetwork, getScriptType } from "~/utils" + import { FeeEstimatorOptions } from "./types" -export default class FeeEstimator { +export class FeeEstimator { protected fee = 0 protected feeRate: number protected network: Network diff --git a/packages/sdk/src/fee/types.ts b/packages/sdk/src/fee/types.ts index 66559302..36b8e22c 100644 --- a/packages/sdk/src/fee/types.ts +++ b/packages/sdk/src/fee/types.ts @@ -1,6 +1,6 @@ import { Psbt } from "bitcoinjs-lib" -import { Network } from "../config/types" +import { Network } from "~/config/types" export interface FeeEstimatorOptions { feeRate: number diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 6f59e1f1..5dddd262 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -10,6 +10,9 @@ import * as transactions from "./transactions" import * as utils from "./utils" import * as wallet from "./wallet" +/** + * @deprecated `ordit` has been deprecated. Import respective modules directly. + */ export const ordit = { config, addresses, @@ -29,14 +32,15 @@ export * as metamask from "./browser-wallets/metamask" export * as unisat from "./browser-wallets/unisat" export * as xverse from "./browser-wallets/xverse" export * from "./config" +export * from "./fee" export * from "./inscription" -export { InstantTradeBuilder, InstantTradeBuyerTxBuilder, InstantTradeSellerTxBuilder } from "./instant-trade" +export * from "./instant-trade" export * from "./keys" -export { BaseDatasource, JsonRpcDatasource } from "./modules" +export * from "./modules" +export * from "./psbt-builder" export * from "./signatures" export * from "./transactions" -export { PSBTBuilder } from "./transactions/PSBTBuilder" export * from "./utils" -export { UTXOManager } from "./utxos" +export * from "./utxos" export * from "./wallet" export { Ordit } from "./wallet/Ordit" diff --git a/packages/sdk/src/transactions/Inscriber.ts b/packages/sdk/src/inscriber/Inscriber.ts similarity index 90% rename from packages/sdk/src/transactions/Inscriber.ts rename to packages/sdk/src/inscriber/Inscriber.ts index 961f09ae..68574a47 100644 --- a/packages/sdk/src/transactions/Inscriber.ts +++ b/packages/sdk/src/inscriber/Inscriber.ts @@ -2,19 +2,14 @@ import * as ecc from "@bitcoinerlab/secp256k1" import * as bitcoin from "bitcoinjs-lib" import { Tapleaf } from "bitcoinjs-lib/src/types" -import { - buildWitnessScript, - createTransaction, - encodeObject, - getDummyP2TRInput, - getNetwork, - GetWalletOptions, - OnOffUnion -} from ".." -import { Network } from "../config/types" -import { NestedObject } from "../utils/types" -import { PSBTBuilder } from "./PSBTBuilder" -import { SkipStrictSatsCheckOptions, UTXOLimited } from "./types" +import { buildWitnessScript } from "~/inscription" +import { PSBTBuilder } from "~/psbt-builder" +import { SkipStrictSatsCheckOptions, UTXOLimited } from "~/transactions/types" +import { createTransaction, encodeObject, getDummyP2TRInput, getNetwork } from "~/utils" +import { NestedObject } from "~/utils/types" +import { OnOffUnion } from "~/wallet" + +import { InscriberArgOptions } from "./types" bitcoin.initEccLib(ecc) @@ -276,20 +271,3 @@ export class Inscriber extends PSBTBuilder { return this.suitableUnspent } } - -export type InscriberArgOptions = Pick & { - network: Network - address: string - destinationAddress: string - publicKey: string - feeRate: number - postage: number - mediaType: string - mediaContent: string - changeAddress: string - meta?: NestedObject - outputs?: Outputs - encodeMetadata?: boolean -} - -type Outputs = Array<{ address: string; value: number }> diff --git a/packages/sdk/src/inscriber/index.ts b/packages/sdk/src/inscriber/index.ts new file mode 100644 index 00000000..c6ebd839 --- /dev/null +++ b/packages/sdk/src/inscriber/index.ts @@ -0,0 +1,2 @@ +export * from "./Inscriber" +export * from "./types" diff --git a/packages/sdk/src/inscriber/types.ts b/packages/sdk/src/inscriber/types.ts new file mode 100644 index 00000000..a52673d4 --- /dev/null +++ b/packages/sdk/src/inscriber/types.ts @@ -0,0 +1,21 @@ +import { Network } from "~/config/types" +import { NestedObject } from "~/utils/types" +import { OnOffUnion } from "~/wallet" + +export type InscriberArgOptions = { + network: Network + address: string + destinationAddress: string + publicKey: string + feeRate: number + postage: number + mediaType: string + mediaContent: string + changeAddress: string + meta?: NestedObject + outputs?: Outputs + encodeMetadata?: boolean + safeMode?: OnOffUnion +} + +type Outputs = Array<{ address: string; value: number }> diff --git a/packages/sdk/src/inscription/collection.ts b/packages/sdk/src/inscription/collection.ts index ddf44d44..9923b76f 100644 --- a/packages/sdk/src/inscription/collection.ts +++ b/packages/sdk/src/inscription/collection.ts @@ -1,5 +1,8 @@ -import { BaseDatasource, GetWalletOptions, Inscriber, JsonRpcDatasource, verifyMessage } from ".." -import { Network } from "../config/types" +import { Network } from "~/config/types" +import { Inscriber } from "~/inscriber" +import { BaseDatasource, JsonRpcDatasource } from "~/modules" +import { verifyMessage } from "~/signatures/message" +import { OnOffUnion } from "~/wallet" export async function publishCollection({ title, @@ -120,7 +123,7 @@ function validateInscriptions(inscriptions: CollectionInscription[] = []) { return true } -export type PublishCollectionOptions = Pick & { +export type PublishCollectionOptions = { address: string feeRate: number postage: number @@ -148,6 +151,7 @@ export type PublishCollectionOptions = Pick & { outputs?: Outputs encodeMetadata?: boolean enableRBF?: boolean + safeMode?: OnOffUnion } export type CollectionInscription = { @@ -156,7 +160,7 @@ export type CollectionInscription = { sri?: string } -export type MintFromCollectionOptions = Pick & { +export type MintFromCollectionOptions = { address: string feeRate: number postage: number @@ -176,6 +180,7 @@ export type MintFromCollectionOptions = Pick & { encodeMetadata?: boolean enableRBF?: boolean datasource?: BaseDatasource + safeMode?: OnOffUnion } type Outputs = Array<{ address: string; value: number }> diff --git a/packages/sdk/src/inscription/index.ts b/packages/sdk/src/inscription/index.ts index fa6cd9dc..78565bda 100644 --- a/packages/sdk/src/inscription/index.ts +++ b/packages/sdk/src/inscription/index.ts @@ -1,3 +1,4 @@ export * from "./collection" export * from "./types" +export * from "./types" export * from "./witness" diff --git a/packages/sdk/src/inscription/types.ts b/packages/sdk/src/inscription/types.ts index 644ba580..427889c3 100644 --- a/packages/sdk/src/inscription/types.ts +++ b/packages/sdk/src/inscription/types.ts @@ -39,6 +39,7 @@ export interface Inscription { mediaSize: number mediaContent: string meta?: Record + value?: number } export interface InputsToSign { diff --git a/packages/sdk/src/instant-trade/InstantTradeBuilder.ts b/packages/sdk/src/instant-trade/InstantTradeBuilder.ts index 0b8d5e2c..7f6d7eed 100644 --- a/packages/sdk/src/instant-trade/InstantTradeBuilder.ts +++ b/packages/sdk/src/instant-trade/InstantTradeBuilder.ts @@ -1,13 +1,10 @@ -import { Inscription } from ".." -import { MINIMUM_AMOUNT_IN_SATS } from "../constants" -import { PSBTBuilder, PSBTBuilderOptions } from "../transactions/PSBTBuilder" +import { MINIMUM_AMOUNT_IN_SATS } from "~/constants" +import { Inscription } from "~/inscription" +import { PSBTBuilder } from "~/psbt-builder" -export interface InstantTradeBuilderArgOptions - extends Pick { - inscriptionOutpoint?: string -} +import { InstantTradeBuilderArgOptions } from "./types" -export default class InstantTradeBuilder extends PSBTBuilder { +export class InstantTradeBuilder extends PSBTBuilder { protected inscriptionOutpoint?: string protected inscription?: Inscription protected price = 0 diff --git a/packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts b/packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts index 5942d96e..8205029c 100644 --- a/packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts +++ b/packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts @@ -1,17 +1,14 @@ import { Psbt } from "bitcoinjs-lib" import reverseBuffer from "buffer-reverse" -import { decodePSBT, generateTxUniqueIdentifier, getScriptType, INSTANT_BUY_SELLER_INPUT_INDEX } from ".." -import { MINIMUM_AMOUNT_IN_SATS } from "../constants" -import { InjectableInput, InjectableOutput } from "../transactions/PSBTBuilder" -import InstantTradeBuilder, { InstantTradeBuilderArgOptions } from "./InstantTradeBuilder" - -interface InstantTradeBuyerTxBuilderArgOptions extends InstantTradeBuilderArgOptions { - sellerPSBT: string - receiveAddress?: string -} +import { INSTANT_BUY_SELLER_INPUT_INDEX, MINIMUM_AMOUNT_IN_SATS } from "~/constants" +import { InjectableInput, InjectableOutput } from "~/psbt-builder/types" +import { decodePSBT, generateTxUniqueIdentifier, getScriptType } from "~/utils" + +import { InstantTradeBuilder } from "./InstantTradeBuilder" +import { InstantTradeBuyerTxBuilderArgOptions } from "./types" -export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder { +export class InstantTradeBuyerTxBuilder extends InstantTradeBuilder { private receiveAddress?: string private sellerPSBT!: Psbt private sellerAddress?: string diff --git a/packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts b/packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts index bf32536b..04a26608 100644 --- a/packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts +++ b/packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts @@ -1,15 +1,13 @@ import * as bitcoin from "bitcoinjs-lib" -import { processInput } from ".." -import { MINIMUM_AMOUNT_IN_SATS } from "../constants" -import { UTXO } from "../transactions/types" -import InstantTradeBuilder, { InstantTradeBuilderArgOptions } from "./InstantTradeBuilder" +import { MINIMUM_AMOUNT_IN_SATS } from "~/constants" +import { processInput } from "~/transactions" +import { UTXO } from "~/transactions/types" -interface InstantTradeSellerTxBuilderArgOptions extends InstantTradeBuilderArgOptions { - receiveAddress?: string -} +import { InstantTradeBuilder } from "./InstantTradeBuilder" +import { InstantTradeSellerTxBuilderArgOptions } from "./types" -export default class InstantTradeSellerTxBuilder extends InstantTradeBuilder { +export class InstantTradeSellerTxBuilder extends InstantTradeBuilder { private receiveAddress?: string private utxo?: UTXO diff --git a/packages/sdk/src/instant-trade/index.ts b/packages/sdk/src/instant-trade/index.ts index cf0a856c..f2395be3 100644 --- a/packages/sdk/src/instant-trade/index.ts +++ b/packages/sdk/src/instant-trade/index.ts @@ -1,3 +1,4 @@ -export { default as InstantTradeBuilder } from "./InstantTradeBuilder" -export { default as InstantTradeBuyerTxBuilder } from "./InstantTradeBuyerTxBuilder" -export { default as InstantTradeSellerTxBuilder } from "./InstantTradeSellerTxBuilder" +export * from "./InstantTradeBuilder" +export * from "./InstantTradeBuyerTxBuilder" +export * from "./InstantTradeSellerTxBuilder" +export * from "./types" diff --git a/packages/sdk/src/instant-trade/types.ts b/packages/sdk/src/instant-trade/types.ts new file mode 100644 index 00000000..620df8c5 --- /dev/null +++ b/packages/sdk/src/instant-trade/types.ts @@ -0,0 +1,15 @@ +import { PSBTBuilderOptions } from "~/psbt-builder/types" + +export interface InstantTradeBuilderArgOptions + extends Pick { + inscriptionOutpoint?: string +} + +export interface InstantTradeBuyerTxBuilderArgOptions extends InstantTradeBuilderArgOptions { + sellerPSBT: string + receiveAddress?: string +} + +export interface InstantTradeSellerTxBuilderArgOptions extends InstantTradeBuilderArgOptions { + receiveAddress?: string +} diff --git a/packages/sdk/src/keys/index.ts b/packages/sdk/src/keys/index.ts index 6c2b5ede..3dcc9614 100644 --- a/packages/sdk/src/keys/index.ts +++ b/packages/sdk/src/keys/index.ts @@ -2,8 +2,8 @@ import * as ecc from "@bitcoinerlab/secp256k1" import BIP32Factory from "bip32" import { entropyToMnemonic, mnemonicToSeed, validateMnemonic, wordlists } from "bip39" -import { Network } from "../config/types" -import { getNetwork } from "../utils" +import { Network } from "~/config/types" +import { getNetwork } from "~/utils" export async function getWalletKeys(seedValue: string, network: Network = "testnet", path: string) { const { parent, mnemonic } = await getDerivedNode(seedValue, network, path) diff --git a/packages/sdk/src/modules/BaseDatasource.ts b/packages/sdk/src/modules/BaseDatasource.ts index 64fac76b..6daa5d3c 100644 --- a/packages/sdk/src/modules/BaseDatasource.ts +++ b/packages/sdk/src/modules/BaseDatasource.ts @@ -1,6 +1,5 @@ import { Transaction as BTCTransaction } from "bitcoinjs-lib" -import { Inscription } from ".." import { GetBalanceOptions, GetInscriptionOptions, @@ -11,15 +10,16 @@ import { GetUnspentsOptions, GetUnspentsResponse, RelayOptions -} from "../api/types" -import { Network } from "../config/types" -import { Transaction, UTXO, UTXOLimited } from "../transactions/types" +} from "~/api/types" +import { Network } from "~/config/types" +import { Inscription } from "~/inscription" +import { Transaction, UTXO, UTXOLimited } from "~/transactions/types" interface BaseDatasourceOptions { network: Network } -export default abstract class BaseDatasource { +export abstract class BaseDatasource { protected readonly network: Network constructor({ network }: BaseDatasourceOptions) { @@ -41,6 +41,7 @@ export default abstract class BaseDatasource { sort, limit, next, + include, decodeMetadata }: GetInscriptionsOptions): Promise diff --git a/packages/sdk/src/modules/DatasourceUtility.ts b/packages/sdk/src/modules/DatasourceUtility.ts index 0172e352..6902e4fb 100644 --- a/packages/sdk/src/modules/DatasourceUtility.ts +++ b/packages/sdk/src/modules/DatasourceUtility.ts @@ -1,12 +1,13 @@ -import { decodeObject, Inscription } from ".." -import { GetUnspentsResponse } from "../api/types" -import { UTXO } from "../transactions/types" +import { GetUnspentsResponse } from "~/api/types" +import { Inscription } from "~/inscription" +import { UTXO } from "~/transactions/types" +import { decodeObject } from "~/utils" interface SegregateUTXOsBySpendStatusArgOptions { utxos: UTXO[] } -export default class DatasourceUtility { +export class DatasourceUtility { static transformInscriptions(inscriptions?: Inscription[]) { if (!inscriptions) return [] diff --git a/packages/sdk/src/modules/JsonRpcDatasource.ts b/packages/sdk/src/modules/JsonRpcDatasource.ts index 01f4cd35..4805d3e2 100644 --- a/packages/sdk/src/modules/JsonRpcDatasource.ts +++ b/packages/sdk/src/modules/JsonRpcDatasource.ts @@ -1,7 +1,6 @@ import { Transaction as BTCTransaction } from "bitcoinjs-lib" -import { Inscription } from ".." -import { rpc } from "../api/jsonrpc" +import { rpc } from "~/api/jsonrpc" import { GetBalanceOptions, GetInscriptionOptions, @@ -12,17 +11,20 @@ import { GetUnspentsOptions, GetUnspentsResponse, RelayOptions -} from "../api/types" -import { Network } from "../config/types" -import { Transaction, UTXO, UTXOLimited } from "../transactions/types" -import { BaseDatasource, DatasourceUtility } from "." +} from "~/api/types" +import { Network } from "~/config/types" +import { Inscription } from "~/inscription" +import { Transaction, UTXO, UTXOLimited } from "~/transactions/types" + +import { BaseDatasource } from "./BaseDatasource" +import { DatasourceUtility } from "./DatasourceUtility" import { JsonRpcPagination } from "./types" interface JsonRpcDatasourceOptions { network: Network } -export default class JsonRpcDatasource extends BaseDatasource { +export class JsonRpcDatasource extends BaseDatasource { constructor({ network }: JsonRpcDatasourceOptions) { super({ network }) } @@ -69,7 +71,8 @@ export default class JsonRpcDatasource extends BaseDatasource { decodeMetadata, sort = "asc", limit = 25, - next = null + next = null, + include }: GetInscriptionsOptions) { let inscriptions: Inscription[] = [] do { @@ -80,6 +83,7 @@ export default class JsonRpcDatasource extends BaseDatasource { "Ordinals.GetInscriptions", { filter: { creator, owner, mimeType, mimeSubType, outpoint }, + include, sort: { number: sort }, pagination: { limit, next } }, diff --git a/packages/sdk/src/modules/index.ts b/packages/sdk/src/modules/index.ts index 3c0379f9..293e79ac 100644 --- a/packages/sdk/src/modules/index.ts +++ b/packages/sdk/src/modules/index.ts @@ -1,3 +1,4 @@ -export { default as BaseDatasource } from "./BaseDatasource" -export { default as DatasourceUtility } from "./DatasourceUtility" -export { default as JsonRpcDatasource } from "./JsonRpcDatasource" +export * from "./BaseDatasource" +export * from "./DatasourceUtility" +export * from "./JsonRpcDatasource" +export * from "./types" diff --git a/packages/sdk/src/transactions/PSBTBuilder.ts b/packages/sdk/src/psbt-builder/PSBTBuilder.ts similarity index 91% rename from packages/sdk/src/transactions/PSBTBuilder.ts rename to packages/sdk/src/psbt-builder/PSBTBuilder.ts index 7a37a78e..1aa73222 100644 --- a/packages/sdk/src/transactions/PSBTBuilder.ts +++ b/packages/sdk/src/psbt-builder/PSBTBuilder.ts @@ -2,47 +2,15 @@ import { networks, Psbt, Transaction } from "bitcoinjs-lib" import reverseBuffer from "buffer-reverse" -import { - BaseDatasource, - convertSatoshisToBTC, - generateTxUniqueIdentifier, - getNetwork, - InputsToSign, - INSTANT_BUY_SELLER_INPUT_INDEX, - JsonRpcDatasource, - toXOnly -} from ".." -import { Network } from "../config/types" -import { MINIMUM_AMOUNT_IN_SATS } from "../constants" -import FeeEstimator from "../fee/FeeEstimator" -import { InputType, processInput } from "." -import { Output, UTXOLimited } from "./types" - -export interface PSBTBuilderOptions { - address: string - changeAddress?: string - feeRate: number - network: Network - outputs: Output[] - publicKey: string - autoAdjustment?: boolean - instantTradeMode?: boolean - datasource?: BaseDatasource -} - -export type InjectableInput = { - injectionIndex: number - txInput: any - sats: number - standardInput: InputType -} - -export interface InjectableOutput { - injectionIndex: number - txOutput: any - sats: number - standardOutput: any -} +import { INSTANT_BUY_SELLER_INPUT_INDEX, MINIMUM_AMOUNT_IN_SATS } from "~/constants" +import { FeeEstimator } from "~/fee" +import { InputsToSign } from "~/inscription" +import { BaseDatasource, JsonRpcDatasource } from "~/modules" +import { InputType, processInput } from "~/transactions" +import { Output, UTXOLimited } from "~/transactions/types" +import { convertSatoshisToBTC, generateTxUniqueIdentifier, getNetwork, toXOnly } from "~/utils" + +import { InjectableInput, InjectableOutput, PSBTBuilderOptions } from "./types" export class PSBTBuilder extends FeeEstimator { protected address: string diff --git a/packages/sdk/src/psbt-builder/index.ts b/packages/sdk/src/psbt-builder/index.ts new file mode 100644 index 00000000..79446eab --- /dev/null +++ b/packages/sdk/src/psbt-builder/index.ts @@ -0,0 +1 @@ +export * from "./PSBTBuilder" diff --git a/packages/sdk/src/psbt-builder/types.ts b/packages/sdk/src/psbt-builder/types.ts new file mode 100644 index 00000000..26185951 --- /dev/null +++ b/packages/sdk/src/psbt-builder/types.ts @@ -0,0 +1,29 @@ +import { Network } from "~/config/types" +import { BaseDatasource } from "~/modules/BaseDatasource" +import { InputType, Output } from "~/transactions" + +export interface PSBTBuilderOptions { + address: string + changeAddress?: string + feeRate: number + network: Network + outputs: Output[] + publicKey: string + autoAdjustment?: boolean + instantTradeMode?: boolean + datasource?: BaseDatasource +} + +export type InjectableInput = { + injectionIndex: number + txInput: any + sats: number + standardInput: InputType +} + +export interface InjectableOutput { + injectionIndex: number + txOutput: any + sats: number + standardOutput: any +} diff --git a/packages/sdk/src/signatures/message.ts b/packages/sdk/src/signatures/message.ts index b4db0d29..8a4826d0 100644 --- a/packages/sdk/src/signatures/message.ts +++ b/packages/sdk/src/signatures/message.ts @@ -1,8 +1,8 @@ import { sign, verify } from "bitcoinjs-message" -import { Network } from "../config/types" -import { getDerivedNode } from "../keys" -import { createTransaction, getNetwork } from "../utils" +import { Network } from "~/config/types" +import { getDerivedNode } from "~/keys" +import { createTransaction, getNetwork } from "~/utils" export async function signMessage(options: SignMessageOptions) { const network = getNetwork(options.network) diff --git a/packages/sdk/src/signatures/psbt.ts b/packages/sdk/src/signatures/psbt.ts index 0a61558f..bf3238db 100644 --- a/packages/sdk/src/signatures/psbt.ts +++ b/packages/sdk/src/signatures/psbt.ts @@ -1,8 +1,8 @@ import * as ecc from "@bitcoinerlab/secp256k1" import * as bitcoin from "bitcoinjs-lib" -import { Network } from "../config/types" -import { getDerivedNode } from "../keys" +import { Network } from "~/config/types" +import { getDerivedNode } from "~/keys" export async function signPsbt(options: SignPsbtOptions) { bitcoin.initEccLib(ecc) diff --git a/packages/sdk/src/transactions/index.ts b/packages/sdk/src/transactions/index.ts index fcf8fff2..c6145566 100644 --- a/packages/sdk/src/transactions/index.ts +++ b/packages/sdk/src/transactions/index.ts @@ -1,2 +1,2 @@ -export * from "./Inscriber" export * from "./psbt" +export * from "./types" diff --git a/packages/sdk/src/transactions/psbt.ts b/packages/sdk/src/transactions/psbt.ts index 9788d14e..073f8539 100644 --- a/packages/sdk/src/transactions/psbt.ts +++ b/packages/sdk/src/transactions/psbt.ts @@ -1,12 +1,19 @@ import * as ecc from "@bitcoinerlab/secp256k1" import { BIP32Factory } from "bip32" -import { Network } from "../config/types" -import { BaseDatasource, JsonRpcDatasource } from "../modules" -import { createTransaction, getNetwork, toXOnly } from "../utils" -import { OnOffUnion } from "../wallet" -import { PSBTBuilder } from "./PSBTBuilder" -import { Output, UTXO, UTXOLimited } from "./types" +import { JsonRpcDatasource } from "~/modules" +import { PSBTBuilder } from "~/psbt-builder" +import { createTransaction, getNetwork, toXOnly } from "~/utils" + +import { + CreatePsbtOptions, + InputType, + LegacyInputType, + NestedSegwitInputType, + ProcessInputOptions, + SegwitInputType, + TaprootInputType +} from "./types" const bip32 = BIP32Factory(ecc) @@ -154,71 +161,3 @@ async function generateLegacyInput({ ...(sighashType ? { sighashType } : undefined) } } - -// TODO: replace below interfaces and custom types w/ PsbtInputExtended from bitcoinjs-lib -interface BaseInputType { - hash: string - index: number - sighashType?: number -} - -type LegacyInputType = BaseInputType & { - type: "legacy" - nonWitnessUtxo?: Buffer - witnessUtxo?: { - script: Buffer - value: number - } -} - -type SegwitInputType = BaseInputType & { - type: "segwit" - witnessUtxo?: { - script: Buffer - value: number - } - witness?: Buffer[] -} - -type TaprootInputType = BaseInputType & - Omit & { - type: "taproot" - tapInternalKey: Buffer - tapLeafScript?: TapLeafScript[] - } - -type NestedSegwitInputType = BaseInputType & - Omit & { - type: "nested-segwit" - redeemScript: Buffer - } - -export type InputType = LegacyInputType | SegwitInputType | NestedSegwitInputType | TaprootInputType - -export type CreatePsbtOptions = { - satsPerByte: number - address: string - outputs: Output[] - enableRBF?: boolean - pubKey: string - network: Network - safeMode?: OnOffUnion -} - -interface ProcessInputOptions { - utxo: UTXO | UTXOLimited - pubKey: string - network: Network - sighashType?: number - witness?: Buffer[] - datasource?: BaseDatasource -} - -interface TapScript { - leafVersion: number - script: Buffer -} -export declare type ControlBlock = Buffer -export interface TapLeafScript extends TapScript { - controlBlock: ControlBlock -} diff --git a/packages/sdk/src/transactions/types.ts b/packages/sdk/src/transactions/types.ts index ca18b07e..00d186b4 100644 --- a/packages/sdk/src/transactions/types.ts +++ b/packages/sdk/src/transactions/types.ts @@ -1,4 +1,7 @@ -import { Inscription, Ordinal } from "../inscription/types" +import { Network } from "~/config/types" +import { Inscription, Ordinal } from "~/inscription/types" +import { BaseDatasource } from "~/modules" +import { OnOffUnion } from "~/wallet" export type Vout = { value: number @@ -75,3 +78,71 @@ export interface SkipStrictSatsCheckOptions { skipStrictSatsCheck?: boolean customAmount?: number } + +export type InputType = LegacyInputType | SegwitInputType | NestedSegwitInputType | TaprootInputType + +// TODO: replace below interfaces and custom types w/ PsbtInputExtended from bitcoinjs-lib +export interface BaseInputType { + hash: string + index: number + sighashType?: number +} + +export type LegacyInputType = BaseInputType & { + type: "legacy" + nonWitnessUtxo?: Buffer + witnessUtxo?: { + script: Buffer + value: number + } +} + +export type SegwitInputType = BaseInputType & { + type: "segwit" + witnessUtxo?: { + script: Buffer + value: number + } + witness?: Buffer[] +} + +export type TaprootInputType = BaseInputType & + Omit & { + type: "taproot" + tapInternalKey: Buffer + tapLeafScript?: TapLeafScript[] + } + +export type NestedSegwitInputType = BaseInputType & + Omit & { + type: "nested-segwit" + redeemScript: Buffer + } + +export type CreatePsbtOptions = { + satsPerByte: number + address: string + outputs: Output[] + enableRBF?: boolean + pubKey: string + network: Network + safeMode?: OnOffUnion +} + +export interface ProcessInputOptions { + utxo: UTXO | UTXOLimited + pubKey: string + network: Network + sighashType?: number + witness?: Buffer[] + datasource?: BaseDatasource +} + +export interface TapScript { + leafVersion: number + script: Buffer +} +export declare type ControlBlock = Buffer +export interface TapLeafScript extends TapScript { + controlBlock: ControlBlock +} diff --git a/packages/sdk/src/utils/constants.ts b/packages/sdk/src/utils/constants.ts index 8504ee39..f04f6c33 100644 --- a/packages/sdk/src/utils/constants.ts +++ b/packages/sdk/src/utils/constants.ts @@ -1,4 +1,4 @@ -import { AddressFormats } from ".." +import { AddressFormats } from "~/addresses" export const DERIVATION_PATHS_WITHOUT_INDEX: Record = { legacy: `m/44'/0'/0'/0/`, diff --git a/packages/sdk/src/utils/index.ts b/packages/sdk/src/utils/index.ts index 154d0c59..34b161ed 100644 --- a/packages/sdk/src/utils/index.ts +++ b/packages/sdk/src/utils/index.ts @@ -3,9 +3,10 @@ import { BIP32Interface } from "bip32" import * as bitcoin from "bitcoinjs-lib" import ECPairFactory from "ecpair" -import { AddressFormats, AddressTypes, addressTypeToName } from "../addresses/formats" -import { Network } from "../config/types" -import { UTXO } from "../transactions/types" +import { AddressFormats, AddressTypes, addressTypeToName } from "~/addresses" +import { Network } from "~/config/types" +import { UTXO } from "~/transactions/types" + import { BufferOrHex, EncodeDecodeObjectOptions, @@ -255,3 +256,5 @@ export function getDummyP2TRInput(): UTXO { confirmation: 10 } } + +export * from "./types" diff --git a/packages/sdk/src/utils/types.ts b/packages/sdk/src/utils/types.ts index 53a4cc7b..15ff6856 100644 --- a/packages/sdk/src/utils/types.ts +++ b/packages/sdk/src/utils/types.ts @@ -1,6 +1,6 @@ import { payments } from "bitcoinjs-lib" -import { AddressFormats, AddressTypes } from ".." +import { AddressFormats, AddressTypes } from "~/addresses" export interface NestedObject { [key: string]: NestedObject | any diff --git a/packages/sdk/src/utxos/UTXOManager.ts b/packages/sdk/src/utxos/UTXOManager.ts index 9257c559..f50a2ae8 100644 --- a/packages/sdk/src/utxos/UTXOManager.ts +++ b/packages/sdk/src/utxos/UTXOManager.ts @@ -1,8 +1,10 @@ -import { processInput, PSBTBuilder } from ".." -import { MINIMUM_AMOUNT_IN_SATS } from "../constants" +import { MINIMUM_AMOUNT_IN_SATS } from "~/constants" +import { PSBTBuilder } from "~/psbt-builder" +import { processInput } from "~/transactions" + import { UTXOManagerOptions } from "./types" -export default class UTXOManager extends PSBTBuilder { +export class UTXOManager extends PSBTBuilder { constructor({ address, publicKey, network, feeRate, datasource }: UTXOManagerOptions) { super({ address, diff --git a/packages/sdk/src/utxos/index.ts b/packages/sdk/src/utxos/index.ts index 2769f153..50518e4a 100644 --- a/packages/sdk/src/utxos/index.ts +++ b/packages/sdk/src/utxos/index.ts @@ -1 +1,2 @@ -export { default as UTXOManager } from "./UTXOManager" +export * from "./types" +export * from "./UTXOManager" diff --git a/packages/sdk/src/utxos/types.ts b/packages/sdk/src/utxos/types.ts index a30eeb32..7e7c57fd 100644 --- a/packages/sdk/src/utxos/types.ts +++ b/packages/sdk/src/utxos/types.ts @@ -1,4 +1,4 @@ -import { PSBTBuilderOptions } from "../transactions/PSBTBuilder" +import { PSBTBuilderOptions } from "~/psbt-builder/types" export type UTXOManagerOptions = Pick< PSBTBuilderOptions, diff --git a/packages/sdk/src/wallet/Ordit.ts b/packages/sdk/src/wallet/Ordit.ts index 91066ad8..af2a2f23 100644 --- a/packages/sdk/src/wallet/Ordit.ts +++ b/packages/sdk/src/wallet/Ordit.ts @@ -12,13 +12,11 @@ import { addressNameToType, getAccountDataFromHdNode, getAddressesFromPublicKey, - getAllAccountsFromHdNode, - getNetwork, - mintFromCollection, - publishCollection, - tweakSigner -} from ".." -import { Network } from "../config/types" + getAllAccountsFromHdNode +} from "~/addresses" +import { Network } from "~/config/types" +import { mintFromCollection, publishCollection } from "~/inscription" +import { getNetwork, tweakSigner } from "~/utils" bitcoin.initEccLib(ecc) const ECPair = ECPairFactory(ecc) diff --git a/packages/sdk/src/wallet/index.ts b/packages/sdk/src/wallet/index.ts index 8006dcf6..2723fa7b 100644 --- a/packages/sdk/src/wallet/index.ts +++ b/packages/sdk/src/wallet/index.ts @@ -1,5 +1,5 @@ -import { AddressTypes } from "../addresses/formats" -import { Network } from "../config/types" +import { AddressTypes } from "~/addresses/formats" +import { Network } from "~/config/types" export type OnOffUnion = "on" | "off" diff --git a/packages/sdk/tsconfig.json b/packages/sdk/tsconfig.json index a5b38310..4eea74eb 100644 --- a/packages/sdk/tsconfig.json +++ b/packages/sdk/tsconfig.json @@ -1,7 +1,11 @@ { "extends": "@sadoprotocol/typescript", "compilerOptions": { - "rootDir": "./", + "rootDir": ".", + "baseUrl": ".", + "paths": { + "~/*": ["./src/*"] + }, "outDir": "./dist", "sourceMap": true },