Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sdk): types cleanup & improvements #79

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 3 additions & 2 deletions packages/sdk/src/addresses/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type GetInscriptionsOptions = RequireAtLeastOne<{
}> & {
sort?: "asc" | "desc"
limit?: number
include?: ["value"]
next?: string | null
decodeMetadata?: boolean
}
Expand Down
6 changes: 4 additions & 2 deletions packages/sdk/src/browser-wallets/metamask/addresses.ts
Original file line number Diff line number Diff line change
@@ -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()) {
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/browser-wallets/metamask/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/browser-wallets/unisat/addresses.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/browser-wallets/xverse/addresses.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/browser-wallets/xverse/signatures.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
4 changes: 1 addition & 3 deletions packages/sdk/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions packages/sdk/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions packages/sdk/src/fee/FeeEstimator.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/fee/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Psbt } from "bitcoinjs-lib"

import { Network } from "../config/types"
import { Network } from "~/config/types"

export interface FeeEstimatorOptions {
feeRate: number
Expand Down
12 changes: 8 additions & 4 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -276,20 +271,3 @@ export class Inscriber extends PSBTBuilder {
return this.suitableUnspent
}
}

export type InscriberArgOptions = Pick<GetWalletOptions, "safeMode"> & {
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 }>
2 changes: 2 additions & 0 deletions packages/sdk/src/inscriber/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./Inscriber"
export * from "./types"
21 changes: 21 additions & 0 deletions packages/sdk/src/inscriber/types.ts
Original file line number Diff line number Diff line change
@@ -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 }>
13 changes: 9 additions & 4 deletions packages/sdk/src/inscription/collection.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -120,7 +123,7 @@ function validateInscriptions(inscriptions: CollectionInscription[] = []) {
return true
}

export type PublishCollectionOptions = Pick<GetWalletOptions, "safeMode"> & {
export type PublishCollectionOptions = {
address: string
feeRate: number
postage: number
Expand Down Expand Up @@ -148,6 +151,7 @@ export type PublishCollectionOptions = Pick<GetWalletOptions, "safeMode"> & {
outputs?: Outputs
encodeMetadata?: boolean
enableRBF?: boolean
safeMode?: OnOffUnion
}

export type CollectionInscription = {
Expand All @@ -156,7 +160,7 @@ export type CollectionInscription = {
sri?: string
}

export type MintFromCollectionOptions = Pick<GetWalletOptions, "safeMode"> & {
export type MintFromCollectionOptions = {
address: string
feeRate: number
postage: number
Expand All @@ -176,6 +180,7 @@ export type MintFromCollectionOptions = Pick<GetWalletOptions, "safeMode"> & {
encodeMetadata?: boolean
enableRBF?: boolean
datasource?: BaseDatasource
safeMode?: OnOffUnion
}

type Outputs = Array<{ address: string; value: number }>
1 change: 1 addition & 0 deletions packages/sdk/src/inscription/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./collection"
export * from "./types"
export * from "./types"
export * from "./witness"
1 change: 1 addition & 0 deletions packages/sdk/src/inscription/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface Inscription {
mediaSize: number
mediaContent: string
meta?: Record<string, any>
value?: number
}

export interface InputsToSign {
Expand Down
13 changes: 5 additions & 8 deletions packages/sdk/src/instant-trade/InstantTradeBuilder.ts
Original file line number Diff line number Diff line change
@@ -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<PSBTBuilderOptions, "publicKey" | "network" | "address" | "autoAdjustment" | "feeRate" | "datasource"> {
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
Expand Down
17 changes: 7 additions & 10 deletions packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 6 additions & 8 deletions packages/sdk/src/instant-trade/InstantTradeSellerTxBuilder.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 4 additions & 3 deletions packages/sdk/src/instant-trade/index.ts
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 15 additions & 0 deletions packages/sdk/src/instant-trade/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PSBTBuilderOptions } from "~/psbt-builder/types"

export interface InstantTradeBuilderArgOptions
extends Pick<PSBTBuilderOptions, "publicKey" | "network" | "address" | "autoAdjustment" | "feeRate" | "datasource"> {
inscriptionOutpoint?: string
}

export interface InstantTradeBuyerTxBuilderArgOptions extends InstantTradeBuilderArgOptions {
sellerPSBT: string
receiveAddress?: string
}

export interface InstantTradeSellerTxBuilderArgOptions extends InstantTradeBuilderArgOptions {
receiveAddress?: string
}
4 changes: 2 additions & 2 deletions packages/sdk/src/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading