Skip to content

Commit

Permalink
feat(sdk): account for fractal network
Browse files Browse the repository at this point in the history
  • Loading branch information
veralygit committed Oct 24, 2024
1 parent 72bc27f commit b89804f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 14 deletions.
14 changes: 11 additions & 3 deletions packages/sdk/src/addresses/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ecc from "@bitcoinerlab/secp256k1"
import BIP32Factory, { BIP32Interface } from "bip32"

import { Network } from "../config/types"
import { Chain, Network } from "../config/types"
import { createTransaction, getDerivationPath, getNetwork, toXOnly } from "../utils"
import { AddressFormats, addressFormats, addressNameToType, AddressTypes, addressTypeToName } from "./formats"

Expand Down Expand Up @@ -40,8 +40,11 @@ export function getAddressesFromPublicKey(
network: Network = "testnet",
format: AddressTypes | "all" = "all",
accountIndex = 0,
addressIndex = 0
addressIndex = 0,
chain: Chain = "bitcoin"
) {
network = chain === "fractal-bitcoin" ? "mainnet" : network

if (!Buffer.isBuffer(pubKey)) {
pubKey = Buffer.from(pubKey, "hex")
}
Expand Down Expand Up @@ -160,7 +163,12 @@ export function getAccountDataFromHdNode({
return accountData
}

export function getAllAccountsFromHdNode({ hdNode, network = "testnet", account = 0, addressIndex = 0 }: GetAllAccountsFromHDNodeOptions) {
export function getAllAccountsFromHdNode({
hdNode,
network = "testnet",
account = 0,
addressIndex = 0
}: GetAllAccountsFromHDNodeOptions) {
const accounts: Account[] = []
const addressTypesList = Object.values(addressTypeToName) as AddressFormats[]

Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/config/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type Network = "mainnet" | "testnet" | "regtest" | "signet"
export type Chain = "bitcoin" | "fractal-bitcoin";
32 changes: 28 additions & 4 deletions packages/sdk/src/inscription/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TaptreeVersion,
verifyMessage
} from ".."
import { Network } from "../config/types"
import { Chain, Network } from "../config/types"
import { MAXIMUM_ROYALTY_PERCENTAGE } from "../constants"
import { OrditSDKError } from "../utils/errors"
import { buildMeta } from "./meta"
Expand All @@ -22,12 +22,20 @@ export async function publishCollection({
royalty,
publishers,
inscriptions,
chain = "bitcoin",
network,
...options
}: PublishCollectionOptions) {
if (chain !== "bitcoin" && chain !== "fractal-bitcoin") {
throw new OrditSDKError("Invalid chain supplied.")
}

if (!validateInscriptions(inscriptions)) {
throw new OrditSDKError("Invalid inscriptions supplied.")
}

network = chain === "fractal-bitcoin" ? "mainnet" : network

if (royalty) {
// 0 = 0%, 0.1 = 10%
if (isNaN(royalty.pct) || royalty.pct < 0 || royalty.pct > MAXIMUM_ROYALTY_PERCENTAGE) {
Expand Down Expand Up @@ -56,14 +64,20 @@ export async function publishCollection({
insc: inscriptions
}

return new Inscriber({ ...options, meta: collectionMeta })
return new Inscriber({ ...options, meta: collectionMeta, network })
}

export async function mintFromCollection(options: MintFromCollectionOptions) {
export async function mintFromCollection({ chain = "bitcoin", ...options }: MintFromCollectionOptions) {
if (!options.collectionInscriptionId || !options.inscriptionIid || !options.destinationAddress) {
throw new OrditSDKError("Invalid options supplied.")
}

if (chain !== "bitcoin" && chain !== "fractal-bitcoin") {
throw new OrditSDKError("Invalid chain supplied.")
}

options.network = chain === "fractal-bitcoin" ? "mainnet" : options.network

const datasource = options.datasource || new JsonRpcDatasource({ network: options.network })
const collection = await datasource.getInscription({ id: options.collectionInscriptionId })
if (!collection) {
Expand Down Expand Up @@ -122,8 +136,15 @@ export async function bulkMintFromCollection({
network,
outputs,
changeAddress,
taptreeVersion
taptreeVersion,
chain = "bitcoin"
}: BulkMintFromCollectionOptions) {
if (chain !== "bitcoin" && chain !== "fractal-bitcoin") {
throw new OrditSDKError("Invalid chain supplied.")
}

network = chain === "fractal-bitcoin" ? "mainnet" : network

let currentPointer = 0

const { metaList, inscriptionList } = inscriptions.reduce<{
Expand Down Expand Up @@ -222,6 +243,7 @@ export type PublishCollectionOptions = Pick<GetWalletOptions, "safeMode"> & {
outputs?: Outputs
encodeMetadata?: boolean
enableRBF?: boolean
chain?: Chain
}

export type CollectionInscription = {
Expand Down Expand Up @@ -253,6 +275,7 @@ export type MintFromCollectionOptions = Pick<GetWalletOptions, "safeMode"> & {
// temporary flag for backward compatibility
includeMintAddress?: boolean
taptreeVersion?: TaptreeVersion
chain?: Chain
}

export type BulkMintFromCollectionOptions = {
Expand All @@ -268,6 +291,7 @@ export type BulkMintFromCollectionOptions = {
taptreeVersion?: TaptreeVersion
collectionGenesis: string
publisherAddress: string
chain?: Chain
}

export type InscriptionsToMint = {
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/instant-trade/InstantTradeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Inscription } from ".."
import { MINIMUM_AMOUNT_IN_SATS } from "../constants"
import { PSBTBuilder, PSBTBuilderOptions } from "../transactions/PSBTBuilder"
import { OrditSDKError } from "../utils/errors"
import { Chain } from "../config/types"

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

interface RoyaltyAttributes {
Expand Down
17 changes: 13 additions & 4 deletions packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder {
sellerPSBT,
feeRate,
datasource,
outputs
outputs,
chain = "bitcoin"
}: InstantTradeBuyerTxBuilderArgOptions) {
if (chain !== "bitcoin" && chain !== "fractal-bitcoin") {
throw new OrditSDKError("Invalid chain supplied")
}

network = chain === "fractal-bitcoin" ? "mainnet" : network

super({
address,
datasource,
network,
publicKey,
feeRate,
feeRate
})

this.receiveAddress = receiveAddress
Expand Down Expand Up @@ -79,10 +86,12 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder {
}

private bindRefundableOutput() {
this.outputs = [{
this.outputs = [
{
address: this.address,
value: this.utxos.reduce((acc, curr, index) => (acc += [0, 1].includes(index) ? curr.sats : 0), 0)
}]
}
]
}

private bindInscriptionOutput() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ export default class InstantTradeSellerTxBuilder extends InstantTradeBuilder {
publicKey,
inscriptionOutpoint,
receiveAddress,
injectRoyalty
injectRoyalty,
chain = "bitcoin"
}: InstantTradeSellerTxBuilderArgOptions) {
if (chain !== "bitcoin" && chain !== "fractal-bitcoin") {
throw new OrditSDKError("Invalid chain supplied")
}

network = chain === "fractal-bitcoin" ? "mainnet" : network

super({
address,
datasource,
Expand Down
8 changes: 7 additions & 1 deletion packages/sdk/src/utxos/UTXOManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { OrditSDKError } from "../utils/errors"
import { UTXOManagerOptions } from "./types"

export default class UTXOManager extends PSBTBuilder {
constructor({ address, publicKey, network, feeRate, datasource }: UTXOManagerOptions) {
constructor({ address, publicKey, network, feeRate, datasource, chain = "bitcoin" }: UTXOManagerOptions) {
if (chain !== "bitcoin" && chain !== "fractal-bitcoin") {
throw new OrditSDKError("Invalid chain supplied")
}

network = chain === "fractal-bitcoin" ? "mainnet" : network

super({
address,
publicKey,
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/src/utxos/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { PSBTBuilderOptions } from "../transactions/PSBTBuilder"
import { Chain } from "../config/types"

export type UTXOManagerOptions = Pick<
PSBTBuilderOptions,
"address" | "network" | "publicKey" | "feeRate" | "datasource"
>
> & {
chain?: Chain
}

0 comments on commit b89804f

Please sign in to comment.