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

ALL-3271 Fix signing with mnemonic BCH transactions #97

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum-kms",
"version": "6.4.2",
"version": "6.4.3",
"description": "Tatum KMS - Key Management System for Tatum-powered apps.",
"main": "dist/index.js",
"engines": {
Expand Down
87 changes: 40 additions & 47 deletions src/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
return [...new Set(keys)]
}

function validatePrivateKeyWasFound(wallet: any, blockchainSignature: TransactionKMS, privateKey: string | undefined) {

Check warning on line 75 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
if (privateKey) return

const index = blockchainSignature.index
Expand All @@ -99,13 +99,13 @@
axios: AxiosInstance,
path?: string,
externalUrl?: string,
externalUrlMethod?: ExternalUrlMethod
externalUrlMethod?: ExternalUrlMethod,
) => {
if (externalUrl) {
console.log(`${new Date().toISOString()} - External url '${externalUrl}' is present, checking against it.`)
try {
if (externalUrlMethod === 'POST') {
await axios.post(`${externalUrl}/${blockchainSignature.id}`, blockchainSignature);
await axios.post(`${externalUrl}/${blockchainSignature.id}`, blockchainSignature)
} else {
await axios.get(`${externalUrl}/${blockchainSignature.id}`)
}
Expand Down Expand Up @@ -148,7 +148,7 @@
return
}
case Currency.SOL: {
const solSDK = TatumSolanaSDK({ apiKey, url: TATUM_URL as any })

Check warning on line 151 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
txData = await solSDK.kms.sign(
blockchainSignature as PendingTransaction,
wallets.map(w => w.privateKey),
Expand All @@ -164,12 +164,9 @@
if (blockchainSignature.withdrawalId) {
txData = await signBitcoinCashOffchainKMSTransaction(blockchainSignature, wallets[0].mnemonic, testnet)
} else {
const privateKeys = await getPrivateKeys(wallets)
await bcashBroadcast(
await signBitcoinCashKMSTransaction(
blockchainSignature,
wallets.map(w => w.privateKey),
testnet,
),
await signBitcoinCashKMSTransaction(blockchainSignature, privateKeys, testnet),
blockchainSignature.id,
)
return
Expand Down Expand Up @@ -199,16 +196,16 @@
return
}
case Currency.XRP: {
const xrpSdk = TatumXrpSDK({ apiKey, url: TATUM_URL as any })

Check warning on line 199 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
const xrpSecret = wallets[0].secret ? wallets[0].secret : wallets[0].privateKey
txData = await xrpSdk.kms.sign(blockchainSignature as any, xrpSecret)

Check warning on line 201 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
await xrpSdk.blockchain.broadcast({ txData, signatureId: blockchainSignature.id })
return
}
case Currency.XLM: {
const xlmSdk = TatumXlmSDK({ apiKey, url: TATUM_URL as any })

Check warning on line 206 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
const xlmSecret = wallets[0].secret ? wallets[0].secret : wallets[0].privateKey
txData = await xlmSdk.kms.sign(blockchainSignature as any, xlmSecret, testnet)

Check warning on line 208 in src/signatures.ts

View workflow job for this annotation

GitHub Actions / 🏗️ Install and build

Unexpected any. Specify a different type
await xlmSdk.blockchain.broadcast({ txData, signatureId: blockchainSignature.id })
return
}
Expand Down Expand Up @@ -607,65 +604,61 @@

if (runOnce) {
await processPendingTransactions(supportedChains, pwd, testnet, path, axios, externalUrl, externalUrlMethod)
return;
return
}

setInterval(async () => {
if (running) {
return
}
running = true

await processPendingTransactions(supportedChains, pwd, testnet, path, axios, externalUrl, externalUrlMethod)

running = false
}, period * 1000)
}

async function processPendingTransactions(
supportedChains: Currency[],
pwd: string,
testnet: boolean,
path: string | undefined,
axios: AxiosInstance,
externalUrl: string | undefined,
externalUrlMethod: ExternalUrlMethod | undefined
supportedChains: Currency[],
pwd: string,
testnet: boolean,
path: string | undefined,
axios: AxiosInstance,
externalUrl: string | undefined,
externalUrlMethod: ExternalUrlMethod | undefined,
) {
const transactions = []
try {
for (const supportedChain of supportedChains) {
const wallets = getManagedWallets(pwd, supportedChain, testnet, path)
transactions.push(...(await getPendingTransactions(axios, supportedChain, wallets)))
}
} catch (e) {
console.error(e)
}
const data = []
for (const transaction of transactions) {
try {
for (const supportedChain of supportedChains) {
const wallets = getManagedWallets(pwd, supportedChain, testnet, path)
transactions.push(...(await getPendingTransactions(axios, supportedChain, wallets)))
}
await processTransaction(transaction, testnet, pwd, axios, path, externalUrl, externalUrlMethod)
console.log(`${new Date().toISOString()} - Tx was processed: ${transaction.id}`)
} catch (e) {
console.error(e)
}
const data = []
for (const transaction of transactions) {
try {
await processTransaction(transaction, testnet, pwd, axios, path, externalUrl, externalUrlMethod)
console.log(`${new Date().toISOString()} - Tx was processed: ${transaction.id}`)
} catch (e) {
const msg = (<any>e).response ? JSON.stringify((<any>e).response.data, null, 2) : `${e}`
data.push({ signatureId: transaction.id, error: msg })
console.error(`${new Date().toISOString()} - Could not process transaction id ${transaction.id}, error: ${msg}`)
}
const msg = (<any>e).response ? JSON.stringify((<any>e).response.data, null, 2) : `${e}`
data.push({ signatureId: transaction.id, error: msg })
console.error(`${new Date().toISOString()} - Could not process transaction id ${transaction.id}, error: ${msg}`)
}
if (data.length > 0) {
try {
const url = `${TATUM_URL}/v3/tatum/kms/batch`
await axios.post(
url,
{ errors: data },
{ headers: { 'x-api-key': Config.getValue(ConfigOption.TATUM_API_KEY) } },
)
console.log(`${new Date().toISOString()} - Send batch call to url '${url}'.`)
} catch (e) {
console.error(
`${new Date().toISOString()} - Error received from API /v3/tatum/kms/batch - ${(<any>e).config.data}`,
)
}
}
if (data.length > 0) {
try {
const url = `${TATUM_URL}/v3/tatum/kms/batch`
await axios.post(url, { errors: data }, { headers: { 'x-api-key': Config.getValue(ConfigOption.TATUM_API_KEY) } })
console.log(`${new Date().toISOString()} - Send batch call to url '${url}'.`)
} catch (e) {
console.error(
`${new Date().toISOString()} - Error received from API /v3/tatum/kms/batch - ${(<any>e).config.data}`,
)
}
}
}

function isValidNumber(value: number | undefined): boolean {
Expand Down
Loading