Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function signTransaction(internalAccountID: string, transactionXDR: string, password: string) {
try {
const account = keyStore.getPublicKeyData(internalAccountID)
const networkPassphrase = account.testnet ? Networks.TESTNET : Networks.PUBLIC
const transaction = new Transaction(transactionXDR, networkPassphrase)
const privateKey = keyStore.getPrivateKeyData(internalAccountID, password).privateKey
transaction.sign(Keypair.fromSecret(privateKey))
return transaction
.toEnvelope()
.toXDR()
.toString("base64")
} catch (error) {
throw Object.assign(new Error("Wrong password."), { name: "WrongPasswordError" })
}
}
async signTransaction(tx: Transaction, account: Account, password: string): Promise {
let privateKey: string
try {
privateKey = keyStore.getPrivateKeyData(account.id, password).privateKey
} catch (error) {
throw WrongPasswordError()
}
const keypair = Keypair.fromSecret(privateKey)
const networkPassphrase = account.testnet ? Networks.TESTNET : Networks.PUBLIC
const signedTx = new Transaction((tx.toEnvelope().toXDR("base64") as unknown) as string, networkPassphrase)
signedTx.sign(keypair)
return signedTx
}
}
export async function createTransaction(operations: Array>, options: TxBlueprint) {
const { horizon, walletAccount } = options
const timeout = selectTransactionTimeout(options.accountData)
const [account, smartTxFee, timebounds] = await Promise.all([
horizon.loadAccount(walletAccount.publicKey),
selectTransactionFeeWithFallback(horizon, 1500),
horizon.fetchTimebounds(timeout)
])
const networkPassphrase = walletAccount.testnet ? Networks.TESTNET : Networks.PUBLIC
const txFee = Math.max(smartTxFee, options.minTransactionFee || 0)
const builder = new TransactionBuilder(account, {
fee: txFee,
memo: options.memo || undefined,
timebounds,
networkPassphrase
})
for (const operation of operations) {
builder.addOperation(operation)
}
const tx = builder.build()
return tx
}
export function deserializeTx(txResponse: Horizon.TransactionResponse, testnet: boolean) {
const networkPassphrase = testnet ? Networks.TESTNET : Networks.PUBLIC
return Object.assign(new Transaction(txResponse.envelope_xdr, networkPassphrase), {
created_at: txResponse.created_at
})
}
function deserializeTx(txResponse: ServerApi.TransactionRecord, testnet: boolean) {
const networkPassphrase = testnet ? Networks.TESTNET : Networks.PUBLIC
return Object.assign(new Transaction(txResponse.envelope_xdr, networkPassphrase), {
created_at: txResponse.created_at
})
}
async signTransaction(transaction: Transaction, account: Account, password: string) {
const transactionEnvelope = transaction.toEnvelope().toXDR("base64")
const event = await sendCommand(commands.keyStore.signTransactionCommand, {
keyID: account.id,
networkPassphrase: account.testnet ? networkPassphrases.testnet : networkPassphrases.mainnet,
password,
transactionEnvelope
})
if (event.data.error === "Wrong password") {
throw WrongPasswordError()
} else {
const signedTransactionEnvelope = event.data.result
const networkPassphrase = account.testnet ? Networks.TESTNET : Networks.PUBLIC
return new Transaction(signedTransactionEnvelope, networkPassphrase)
}
},
async removeKey(keyID: string) {
export function deserializeSignatureRequest(rawSignatureRequest: Omit): SignatureRequest {
const { operation, parameters } = parseRequestURI(rawSignatureRequest.request_uri)
const networkPassphrase = parameters.network_passphrase ? parameters.network_passphrase : Networks.PUBLIC
return {
...rawSignatureRequest,
meta: {
operation,
callbackURL: parameters.callback ? parameters.callback.replace(/^url:/, "") : undefined,
transaction: new Transaction(parameters.xdr, networkPassphrase)
}
}
}