Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const onCreateAccount = async (formValues: AccountCreationValues) => {
try {
const account = await createAccount({
name: formValues.name,
keypair: Keypair.fromSecret(formValues.privateKey),
password: formValues.setPassword ? formValues.password : null,
testnet: props.testnet
})
if (formValues.createNewKey && !props.testnet) {
setCreatedAccount(account)
} else {
router.history.push(routes.account(account.id))
}
} catch (error) {
trackError(error)
}
}
const createAccount = async (formValues: AccountCreationValues) => {
try {
const account = await createAccountInStore({
name: formValues.name,
keypair: Keypair.fromSecret(formValues.privateKey),
password: formValues.setPassword ? formValues.password : null,
testnet: props.testnet
})
props.onClose()
props.history.push(routes.account(account.id))
} catch (error) {
addError(error)
}
}
return (
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
}
}
expose(ipcMain, commands.signTransactionCommand, events.signTransactionEvent, function signTransaction(
txEnvelopeXdr,
keyID,
networkPassphrase,
password
) {
const transaction = new Transaction(txEnvelopeXdr, networkPassphrase)
let privateKey
try {
privateKey = keystore.getPrivateKeyData(keyID, password).privateKey
} catch (error) {
throw Object.assign(new Error("Wrong password."), { name: "WrongPasswordError" })
}
transaction.sign(Keypair.fromSecret(privateKey))
return transaction.toEnvelope().toXDR("base64")
})
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" })
}
}
function isAccountAlreadyImported(privateKey: string, accounts: Account[]) {
const publicKey = Keypair.fromSecret(privateKey).publicKey()
return accounts.some(account => account.publicKey === publicKey)
}