Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function unpackAddress(address, hdPassphrase) {
// we decode the address from the base58 string
// and then we strip the 24 CBOR data tags (the "[0].value" part)
const addressAsBuffer = cbor.decode(base58.decode(address))[0].value
const addressData = cbor.decode(addressAsBuffer)
const attributes = addressData[1]
const payload = cbor.decode(attributes.get(1))
let derivationPath
try {
derivationPath = decryptDerivationPath(payload, hdPassphrase)
} catch (e) {
throw new Error('Unable to get derivation path from address')
}
if (derivationPath && derivationPath.length > 2) {
throw Error('Invalid derivation path length, should be at most 2')
}
return {
derivationPath,
}
}
function isValidAddress(address) {
try {
// we decode the address from the base58 string
const addressAsArray = cbor.decode(base58.decode(address))
// we strip the 24 CBOR data taga by taking the "value" attribute from the "Tagged" object
const addressDataEncoded = addressAsArray[0].value
const crc32Checksum = addressAsArray[1]
if (crc32Checksum !== crc32(addressDataEncoded)) {
return false
}
} catch (e) {
return false
}
return true
}
function unpackAddress(address, hdPassphrase) {
// we decode the address from the base58 string
// and then we strip the 24 CBOR data tags (the "[0].value" part)
const addressAsBuffer = cbor.decode(base58.decode(address))[0].value
const addressData = cbor.decode(addressAsBuffer)
const attributes = addressData[1]
const payload = cbor.decode(attributes.get(1))
let derivationPath
try {
derivationPath = decryptDerivationPath(payload, hdPassphrase)
} catch (e) {
throw new Error('Unable to get derivation path from address')
}
if (derivationPath && derivationPath.length > 2) {
throw Error('Invalid derivation path length, should be at most 2')
}
return {
derivationPath,
function unpackAddress(address, hdPassphrase) {
// we decode the address from the base58 string
// and then we strip the 24 CBOR data tags (the "[0].value" part)
const addressAsBuffer = cbor.decode(base58.decode(address))[0].value
const addressData = cbor.decode(addressAsBuffer)
const attributes = addressData[1]
const payload = cbor.decode(attributes.get(1))
let derivationPath
try {
derivationPath = decryptDerivationPath(payload, hdPassphrase)
} catch (e) {
throw new Error('Unable to get derivation path from address')
}
if (derivationPath && derivationPath.length > 2) {
throw Error('Invalid derivation path length, should be at most 2')
}
return {
const parseTxInput = (val) => {
const type = val[0]
const inputAttributes = decode(val[1].value)
const txHash = parseTxHash(inputAttributes[0])
const outputIndex = inputAttributes[1]
const utxo = {
type,
txHash,
outputIndex,
}
return TxInputFromUtxo(utxo)
}
const parseTxWitness = (val) => {
const witnessAttributes = decode(val[1].value)
const extendedPubicKey = witnessAttributes[0]
const signature = witnessAttributes[1]
return TxWitness(extendedPubicKey, signature)
}
/*
if (walletExportObj.fileType !== 'WALLETS_EXPORT') {
throw Error('Invalid file type')
}
const {passwordHash: b64PasswordHash, walletSecretKey: b64WalletSecret} = walletExportObj.wallet
const passwordHash = Buffer.from(b64PasswordHash, 'base64')
const derivationScheme = Object.values(derivationSchemes).find(
(x) => x.keyfileVersion === walletExportObj.fileVersion
)
if (derivationScheme === undefined) {
throw Error(`Invalid file version: ${walletExportObj.fileVersion}`)
}
const walletSecretDef = {
rootSecret: decode(Buffer.from(b64WalletSecret, 'base64')),
derivationScheme,
}
return {
passwordHash,
walletSecretDef,
}
}
function decryptDerivationPath(addressPayload, hdPassphrase) {
const decipheredDerivationPath = chacha20poly1305Decrypt(
addressPayload,
hdPassphrase,
Buffer.from('serokellfore')
)
try {
return cbor.decode(Buffer.from(decipheredDerivationPath))
} catch (err) {
throw new Error('incorrect address or passphrase')
}
}