Skip to content

Commit

Permalink
feat(sdk): Add max transaction size check (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevzzsk authored Jul 10, 2024
1 parent ece311f commit ec85938
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
13 changes: 12 additions & 1 deletion packages/sdk/src/transactions/InscriberV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class InscriberV2 extends PSBTBuilder {
private recovery = false
private recoverAmount = 0
private previewMode = false
private isStandard = true
readonly metaInscriptions: EnvelopeOpts[]
readonly inscriptions: EnvelopeOpts[]

Expand All @@ -56,7 +57,8 @@ export class InscriberV2 extends PSBTBuilder {
taptreeVersion,
datasource,
metaInscriptions,
inscriptions
inscriptions,
isStandard
}: InscriberV2ArgOptions) {
super({
address,
Expand All @@ -74,6 +76,7 @@ export class InscriberV2 extends PSBTBuilder {
this.taptreeVersion = taptreeVersion
this.metaInscriptions = metaInscriptions ?? []
this.inscriptions = inscriptions ?? []
this.isStandard = isStandard ?? true
}

get data() {
Expand Down Expand Up @@ -276,6 +279,13 @@ export class InscriberV2 extends PSBTBuilder {

await this.calculateNetworkFeeUsingPreviewMode()

if (this.isStandard) {
// max weight of a tx is 400,000 WU https://github.com/bitcoin/bitcoin/blob/d908877c4774c2456eed09167a5f382758e4a8a6/src/policy/policy.h#L26-L27
if (this.weight > 400_000) {
throw new OrditSDKError("Transaction exceeds maximum weight")
}
}

this.commitAddress = this.payment.address!
return {
address: this.payment.address!,
Expand Down Expand Up @@ -355,6 +365,7 @@ export type InscriberV2ArgOptions = {
taptreeVersion?: TaptreeVersion
outputs?: Outputs
datasource?: BaseDatasource
isStandard?: boolean
}

type Outputs = Array<{ address: string; value: number }>
51 changes: 36 additions & 15 deletions packages/sdk/src/wallet/Ordit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import ECPairFactory, { ECPairInterface } from "ecpair"
import {
Account,
AddressFormats,
addressNameToType, DerivationIndex,
addressNameToType,
DerivationIndex,
getAccountDataFromHdNode,
getAddressesFromPublicKey,
getAllAccountsFromHdNode,
getNetwork,
mintFromCollection,
publishCollection, SigningMessageOptions,
publishCollection,
SigningMessageOptions,
tweakSigner
} from ".."
import { Network } from "../config/types"
Expand All @@ -37,7 +39,16 @@ export class Ordit {
selectedAddressType: AddressFormats | undefined
selectedAddress: string | undefined

constructor({ wif, seed, privateKey, bip39, network = "testnet", type = "legacy", account = 0, addressIndex = 0 }: WalletOptions) {
constructor({
wif,
seed,
privateKey,
bip39,
network = "testnet",
type = "legacy",
account = 0,
addressIndex = 0
}: WalletOptions) {
this.#network = network
const networkObj = getNetwork(network)
const format = addressNameToType[type]
Expand Down Expand Up @@ -102,8 +113,12 @@ export class Ordit {
if (!this.#initialized || !this.allAddresses.length) {
throw new OrditSDKError("Wallet not fully initialized.")
}
return this.allAddresses.find((address) => address.format === type && address.derivationPath.account === derivationIndex.accountIndex
&& address.derivationPath.addressIndex === derivationIndex.addressIndex);
return this.allAddresses.find(
(address) =>
address.format === type &&
address.derivationPath.account === derivationIndex.accountIndex &&
address.derivationPath.addressIndex === derivationIndex.addressIndex
)
}

getAllAddresses() {
Expand All @@ -114,17 +129,20 @@ export class Ordit {
return this.allAddresses
}

setDefaultAddress(type: AddressFormats, { accountIndex = 0, addressIndex = 0 }: DerivationIndex) {
setDefaultAddress(
type: AddressFormats,
{ accountIndex, addressIndex }: DerivationIndex = { accountIndex: 0, addressIndex: 0 }
) {
if (this.selectedAddressType === type) return
let addressToSelect: Account;
let addressToSelect: Account

const account = this.getAddressByType(type, { accountIndex, addressIndex }) as Account
if (!account) {
addressToSelect = this.generateAddress(type, accountIndex, addressIndex);
addressToSelect = this.generateAddress(type, accountIndex, addressIndex)
// Push to current list of addresses
this.allAddresses.push(addressToSelect);
this.allAddresses.push(addressToSelect)
} else {
addressToSelect = account;
addressToSelect = account
}

if (!addressToSelect)
Expand Down Expand Up @@ -237,9 +255,12 @@ export class Ordit {

signMessage(message: string, type?: AddressFormats, opts?: SigningMessageOptions) {
const addressType = type || this.selectedAddressType
const accountIndexToSign: number = opts?.accountIndex === undefined ? 0 : opts?.accountIndex;
const addressIndexToSign: number = opts?.addressIndex === undefined ? 0 : opts?.addressIndex;
const node = this.getAddressByType(addressType!, { accountIndex: accountIndexToSign, addressIndex: addressIndexToSign }) as Account
const accountIndexToSign: number = opts?.accountIndex === undefined ? 0 : opts?.accountIndex
const addressIndexToSign: number = opts?.addressIndex === undefined ? 0 : opts?.addressIndex
const node = this.getAddressByType(addressType!, {
accountIndex: accountIndexToSign,
addressIndex: addressIndexToSign
}) as Account
const signature = AddressUtils.isP2PKH(node.address!)
? sign(message, node.child.privateKey!)
: Signer.sign(node.child.toWIF(), node.address!, message, getNetwork(this.#network))
Expand Down Expand Up @@ -270,8 +291,8 @@ export type WalletOptions = {
bip39?: string
network?: Network
type?: AddressFormats
account?: number;
addressIndex?: number;
account?: number
addressIndex?: number
}

export type Address = ReturnType<typeof getAddressesFromPublicKey>[0]
Expand Down

0 comments on commit ec85938

Please sign in to comment.