Skip to content

Commit

Permalink
UTXO signing secret deduplication (#1135)
Browse files Browse the repository at this point in the history
* UTXO signing secret deduplication

* UTXO signing secret deduplication adjustments

* UTXO signing secret deduplication adjustments 2
  • Loading branch information
Smrecz authored Oct 30, 2024
1 parent 1b44570 commit 3515554
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tatumio",
"version": "2.2.86",
"version": "2.2.87",
"license": "MIT",
"repository": "https://github.com/tatumio/tatum-js",
"scripts": {
Expand Down
15 changes: 7 additions & 8 deletions packages/blockchain/doge/src/lib/doge.sdk.tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const dogeTransactions = (
transaction.to(item.address, amount)
}

const privateKeysToSign = []
const privateKeysToSign = new Set<string>()
if ('fromUTXO' in body) {
for (const item of body.fromUTXO) {
const satoshis = amountUtils.toSatoshis(item.value)
Expand All @@ -64,8 +64,8 @@ export const dogeTransactions = (
satoshis,
}),
])
if ('signatureId' in item) privateKeysToSign.push(item.signatureId)
else if ('privateKey' in item) privateKeysToSign.push(item.privateKey)
if ('signatureId' in item) privateKeysToSign.add(item.signatureId)
else if ('privateKey' in item) privateKeysToSign.add(item.privateKey)
}
} else if ('fromAddress' in body) {
for (const item of body.fromAddress) {
Expand All @@ -88,10 +88,9 @@ export const dogeTransactions = (
satoshis,
}),
])

if ('signatureId' in item) privateKeysToSign.push(item.signatureId)
else if ('privateKey' in item) privateKeysToSign.push(item.privateKey)
}
if ('signatureId' in item) privateKeysToSign.add(item.signatureId)
else if ('privateKey' in item) privateKeysToSign.add(item.privateKey)
}
}

Expand All @@ -110,9 +109,9 @@ export const dogeTransactions = (
return JSON.stringify(transaction)
}

for (const pk of privateKeysToSign) {
privateKeysToSign.forEach((pk) => {
transaction.sign(PrivateKey.fromWIF(pk))
}
})

return transaction.serialize()
} catch (e: any) {
Expand Down
21 changes: 10 additions & 11 deletions packages/shared/blockchain/btc-based/src/lib/nested/btc-based.tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const btcBasedTransactions = (
transaction: BtcBasedTransaction,
body: BtcFromAddressTypes | LtcFromAddressTypes,
options: BtcBasedTxOptions,
): Promise<Array<string>> => {
): Promise<Set<string>> => {
if (body.fromAddress.length === 0 && !options.skipAllChecks) {
throw new BtcBasedSdkError(SdkErrorCode.BTC_BASED_NO_INPUTS)
}
Expand All @@ -140,7 +140,7 @@ export const btcBasedTransactions = (
totalOutputs += item.satoshis
}
try {
const privateKeysToSign = []
const privateKeysToSign = new Set<string>()

for (const item of body.fromAddress) {
if (totalInputs >= totalOutputs) {
Expand All @@ -162,10 +162,9 @@ export const btcBasedTransactions = (
satoshis: amountUtils.toSatoshis(utxo.value),
}),
])

if ('signatureId' in item) privateKeysToSign.push(item.signatureId)
else if ('privateKey' in item) privateKeysToSign.push(item.privateKey)
}
if ('signatureId' in item) privateKeysToSign.add(item.signatureId)
else if ('privateKey' in item) privateKeysToSign.add(item.privateKey)
}

return privateKeysToSign
Expand Down Expand Up @@ -205,13 +204,13 @@ export const btcBasedTransactions = (
transaction: Transaction,
body: BtcFromUtxoTypes | LtcFromUtxoTypes,
options: BtcBasedTxOptions,
): Promise<Array<string>> => {
): Promise<Set<string>> => {
if (body.fromUTXO.length === 0 && !options.skipAllChecks) {
throw new BtcBasedSdkError(SdkErrorCode.BTC_BASED_NO_INPUTS)
}

try {
const privateKeysToSign = []
const privateKeysToSign = new Set<string>()
const utxos: BtcUTXO[] = []

const filteredUtxos = await getUtxoBatch(getChain(options), body)
Expand All @@ -233,8 +232,8 @@ export const btcBasedTransactions = (
}),
])

if ('signatureId' in utxoItem) privateKeysToSign.push(utxoItem.signatureId)
else if ('privateKey' in utxoItem) privateKeysToSign.push(utxoItem.privateKey)
if ('signatureId' in utxoItem) privateKeysToSign.add(utxoItem.signatureId)
else if ('privateKey' in utxoItem) privateKeysToSign.add(utxoItem.privateKey)
}

if (!options.skipAllChecks) {
Expand Down Expand Up @@ -325,7 +324,7 @@ export const btcBasedTransactions = (
): Promise<string> {
try {
const tx: BtcBasedTransaction = new createTransaction()
let privateKeysToSign: string[] = []
let privateKeysToSign = new Set<string>()

if (body.changeAddress) {
tx.change(body.changeAddress)
Expand Down Expand Up @@ -359,7 +358,7 @@ export const btcBasedTransactions = (
}
}

new Set(privateKeysToSign).forEach((key) => {
privateKeysToSign.forEach((key) => {
tx.sign(new createPrivateKey(key))
})

Expand Down

0 comments on commit 3515554

Please sign in to comment.