Skip to content

Commit ec85938

Browse files
authored
feat(sdk): Add max transaction size check (#122)
1 parent ece311f commit ec85938

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

packages/sdk/src/transactions/InscriberV2.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class InscriberV2 extends PSBTBuilder {
3131
private recovery = false
3232
private recoverAmount = 0
3333
private previewMode = false
34+
private isStandard = true
3435
readonly metaInscriptions: EnvelopeOpts[]
3536
readonly inscriptions: EnvelopeOpts[]
3637

@@ -56,7 +57,8 @@ export class InscriberV2 extends PSBTBuilder {
5657
taptreeVersion,
5758
datasource,
5859
metaInscriptions,
59-
inscriptions
60+
inscriptions,
61+
isStandard
6062
}: InscriberV2ArgOptions) {
6163
super({
6264
address,
@@ -74,6 +76,7 @@ export class InscriberV2 extends PSBTBuilder {
7476
this.taptreeVersion = taptreeVersion
7577
this.metaInscriptions = metaInscriptions ?? []
7678
this.inscriptions = inscriptions ?? []
79+
this.isStandard = isStandard ?? true
7780
}
7881

7982
get data() {
@@ -276,6 +279,13 @@ export class InscriberV2 extends PSBTBuilder {
276279

277280
await this.calculateNetworkFeeUsingPreviewMode()
278281

282+
if (this.isStandard) {
283+
// max weight of a tx is 400,000 WU https://github.com/bitcoin/bitcoin/blob/d908877c4774c2456eed09167a5f382758e4a8a6/src/policy/policy.h#L26-L27
284+
if (this.weight > 400_000) {
285+
throw new OrditSDKError("Transaction exceeds maximum weight")
286+
}
287+
}
288+
279289
this.commitAddress = this.payment.address!
280290
return {
281291
address: this.payment.address!,
@@ -355,6 +365,7 @@ export type InscriberV2ArgOptions = {
355365
taptreeVersion?: TaptreeVersion
356366
outputs?: Outputs
357367
datasource?: BaseDatasource
368+
isStandard?: boolean
358369
}
359370

360371
type Outputs = Array<{ address: string; value: number }>

packages/sdk/src/wallet/Ordit.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import ECPairFactory, { ECPairInterface } from "ecpair"
1010
import {
1111
Account,
1212
AddressFormats,
13-
addressNameToType, DerivationIndex,
13+
addressNameToType,
14+
DerivationIndex,
1415
getAccountDataFromHdNode,
1516
getAddressesFromPublicKey,
1617
getAllAccountsFromHdNode,
1718
getNetwork,
1819
mintFromCollection,
19-
publishCollection, SigningMessageOptions,
20+
publishCollection,
21+
SigningMessageOptions,
2022
tweakSigner
2123
} from ".."
2224
import { Network } from "../config/types"
@@ -37,7 +39,16 @@ export class Ordit {
3739
selectedAddressType: AddressFormats | undefined
3840
selectedAddress: string | undefined
3941

40-
constructor({ wif, seed, privateKey, bip39, network = "testnet", type = "legacy", account = 0, addressIndex = 0 }: WalletOptions) {
42+
constructor({
43+
wif,
44+
seed,
45+
privateKey,
46+
bip39,
47+
network = "testnet",
48+
type = "legacy",
49+
account = 0,
50+
addressIndex = 0
51+
}: WalletOptions) {
4152
this.#network = network
4253
const networkObj = getNetwork(network)
4354
const format = addressNameToType[type]
@@ -102,8 +113,12 @@ export class Ordit {
102113
if (!this.#initialized || !this.allAddresses.length) {
103114
throw new OrditSDKError("Wallet not fully initialized.")
104115
}
105-
return this.allAddresses.find((address) => address.format === type && address.derivationPath.account === derivationIndex.accountIndex
106-
&& address.derivationPath.addressIndex === derivationIndex.addressIndex);
116+
return this.allAddresses.find(
117+
(address) =>
118+
address.format === type &&
119+
address.derivationPath.account === derivationIndex.accountIndex &&
120+
address.derivationPath.addressIndex === derivationIndex.addressIndex
121+
)
107122
}
108123

109124
getAllAddresses() {
@@ -114,17 +129,20 @@ export class Ordit {
114129
return this.allAddresses
115130
}
116131

117-
setDefaultAddress(type: AddressFormats, { accountIndex = 0, addressIndex = 0 }: DerivationIndex) {
132+
setDefaultAddress(
133+
type: AddressFormats,
134+
{ accountIndex, addressIndex }: DerivationIndex = { accountIndex: 0, addressIndex: 0 }
135+
) {
118136
if (this.selectedAddressType === type) return
119-
let addressToSelect: Account;
137+
let addressToSelect: Account
120138

121139
const account = this.getAddressByType(type, { accountIndex, addressIndex }) as Account
122140
if (!account) {
123-
addressToSelect = this.generateAddress(type, accountIndex, addressIndex);
141+
addressToSelect = this.generateAddress(type, accountIndex, addressIndex)
124142
// Push to current list of addresses
125-
this.allAddresses.push(addressToSelect);
143+
this.allAddresses.push(addressToSelect)
126144
} else {
127-
addressToSelect = account;
145+
addressToSelect = account
128146
}
129147

130148
if (!addressToSelect)
@@ -237,9 +255,12 @@ export class Ordit {
237255

238256
signMessage(message: string, type?: AddressFormats, opts?: SigningMessageOptions) {
239257
const addressType = type || this.selectedAddressType
240-
const accountIndexToSign: number = opts?.accountIndex === undefined ? 0 : opts?.accountIndex;
241-
const addressIndexToSign: number = opts?.addressIndex === undefined ? 0 : opts?.addressIndex;
242-
const node = this.getAddressByType(addressType!, { accountIndex: accountIndexToSign, addressIndex: addressIndexToSign }) as Account
258+
const accountIndexToSign: number = opts?.accountIndex === undefined ? 0 : opts?.accountIndex
259+
const addressIndexToSign: number = opts?.addressIndex === undefined ? 0 : opts?.addressIndex
260+
const node = this.getAddressByType(addressType!, {
261+
accountIndex: accountIndexToSign,
262+
addressIndex: addressIndexToSign
263+
}) as Account
243264
const signature = AddressUtils.isP2PKH(node.address!)
244265
? sign(message, node.child.privateKey!)
245266
: Signer.sign(node.child.toWIF(), node.address!, message, getNetwork(this.#network))
@@ -270,8 +291,8 @@ export type WalletOptions = {
270291
bip39?: string
271292
network?: Network
272293
type?: AddressFormats
273-
account?: number;
274-
addressIndex?: number;
294+
account?: number
295+
addressIndex?: number
275296
}
276297

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

0 commit comments

Comments
 (0)