Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function process() {
// If the queue is not empty
if (queue.length > 0) {
// Get the next message ID
const id = queue[0]
// Attempt to read the message from disk
try {
const message = decode((new Uint8Array(JSON.parse(localStorage.getItem("_asap_" + id)))).buffer)
const timeout = message[2]
// If the message has expired
if (timeout < Date.now()) {
// Dequeue the message
dequeue(id)
}
// If the message has not expired
else {
// Attempt to send the message
try {
peerSocket.send(message)
} catch (error) {
debug && console.log(error)
}
}
}
if (!bytecode || bytecode.length <= 2) return bytecode;
// Gather length of CBOR metadata from the end of the file
const rawLength = bytecode.slice(bytecode.length - 4);
const length = parseInt(rawLength, 16);
// Bail on unreasonable values for length (meaning we read something else other than metadata length)
if (length * 2 > bytecode.length - 4) return bytecode;
// Gather what we assume is the CBOR encoded metadata, and try to parse it
const metadataStart = bytecode.length - length * 2 - 4;
const metadata = bytecode.slice(metadataStart, bytecode.length - 4);
// Parse it to see if it is indeed valid metadata
try {
cbor.decode(Buffer.from(metadata, 'hex'));
} catch (err) {
Loggy.noSpin.warn(
__filename,
'tryRemoveMetadata',
'parse-contract-metadata',
`Error parsing contract metadata: ${err.message}. Ignoring.`,
);
return bytecode;
}
// Return bytecode without it
return bytecode.slice(0, metadataStart);
}
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))
const derivationPath = decryptDerivationPath(payload, hdPassphrase)
if (derivationPath.length > 2) {
throw Error('Invalid derivation path length, should be at most 2')
}
return {
derivationPath,
}
}
exports.deriveSecretStringFromAddressOrFail = function(address, rootSecretString) {
// we decode the address from the base58 string
// and then we strip the 24 CBOR data taga (the "[0].value" part)
const addressAsBuffer = cbor.decode(base58.decode(address))[0].value
const addressData = cbor.decode(addressAsBuffer)
const addressAttributes = addressData[1]
let childIndex
if (addressAttributes.length === 0) {
// the root address (derrived straight from the root secret key)
childIndex = 0x80000000
} else {
// the remaining addresses have a nontrivial child index
// therefore the derivation path is nonempty
const addressPayload = cbor.decode(addressAttributes.get(1))
const hdPassphrase = deriveHDPassphrase(rootSecretString)
const derivationPath = decryptDerivationPathOrFail(addressPayload, hdPassphrase)
childIndex = derivationPath[1]
}
function decryptDerivationPath(addressPayload, hdPassphrase) {
const decipheredDerivationPath = chacha20poly1305Decrypt(
addressPayload,
hdPassphrase,
Buffer.from('serokellfore')
)
try {
return cbor.decode(Buffer.from(decipheredDerivationPath))
} catch (err) {
debugLog(err)
throw NamedError('AddressDecodingException', 'incorrect address or passphrase')
}
}
static deconstructAddress(address: string) {
const [addressRoot, addrAttr, addressType] = cbor.decode(
cbor.decode(bs58.decode(address))[0].value,
)
return { addressRoot, addrAttr, addressType }
}
}
static deconstructAddress(address: string) {
const [addressRoot, addrAttr, addressType] = cbor.decode(
cbor.decode(bs58.decode(address))[0].value,
)
return { addressRoot, addrAttr, addressType }
}
}
inputs: inputs.map(inp => {
const [type, tagged] = inp
const [inputTxId, idx] = cbor.decode(tagged.value)
return { type, txId: inputTxId.toString('hex'), idx }
}),
outputs: outputs.map(out => {
static parseBlock(blob: Buffer, handleRegularBlock: number): Block {
const [type, [header, body]] = cbor.decode(blob)
const hash = utils.headerToId(header, type)
const common = {
hash,
magic: header[0],
prevHash: header[1].toString('hex'),
}
let blockData
switch (type) {
case 0:
blockData = { ...common, ...Block.handleEpochBoundaryBlock(header) }
break
case 1:
blockData = {
...common,
...Block.handleRegularBlock(header, body, hash, handleRegularBlock),
}
function decryptDerivationPathOrFail(addressPayload, hdPassphrase) {
const cipher = new chacha20.ChaCha20Poly1305(hdPassphrase)
const decipheredDerivationPath = cipher.open(new Buffer('serokellfore'), addressPayload)
try {
return cbor.decode(new Buffer(decipheredDerivationPath))
} catch (err) {
throw new AddressDecodingException('incorrect address or passphrase')
}
}