Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert.equal(typeof memo, 'string', 'memo')
if(!/^#/.test(memo)) return memo
memo = memo.substring(1)
assert(private_key, 'private_key is required')
memo = base58.decode(memo)
memo = encMemo.fromBuffer(new Buffer(memo, 'binary'))
const {from, to, nonce, check, encrypted} = memo
const pubkey = private_key.toPublicKey().toString()
const otherpub = pubkey === from.toString() ? to.toString() : from.toString()
memo = Aes.decrypt(private_key, otherpub, nonce, encrypted, check)
// remove varint length prefix
const mbuf = ByteBuffer.fromBinary(memo.toString('binary'), ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
try {
mbuf.mark()
return mbuf.readVString()
} catch(e) {
mbuf.reset()
// Sender did not length-prefix the memo
memo = new Buffer(mbuf.toString('binary'), 'binary').toString('utf-8')
return memo
}
}
assert(private_key, 'private_key is required')
checkEncryption()
private_key = toPrivateObj(private_key)
memo = base58.decode(memo)
memo = encMemo.fromBuffer(new Buffer(memo, 'binary'))
const {from, to, nonce, check, encrypted} = memo
const pubkey = private_key.toPublicKey().toString()
const otherpub = pubkey === from.toString() ? to.toString() : from.toString()
memo = Aes.decrypt(private_key, otherpub, nonce, encrypted, check)
// remove varint length prefix
const mbuf = ByteBuffer.fromBinary(memo.toString('binary'), ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
try {
mbuf.mark()
return '#' + mbuf.readVString()
} catch(e) {
mbuf.reset()
// Sender did not length-prefix the memo
memo = new Buffer(mbuf.toString('binary'), 'binary').toString('utf-8')
return '#' + memo
}
}
// priv_to_pub: private_key.toPublicKey().toString(),
// pub: public_key.toString(),
// nonce: nonce.toString(),
// message: message.length,
// checksum,
// S: S.toString('hex'),
// encryption_key: encryption_key.toString('hex'),
// })
const iv = encryption_key.slice(32, 48)
const key = encryption_key.slice(0, 32)
// check is first 64 bit of sha256 hash treated as uint64_t truncated to 32 bits.
let check = hash.sha256(encryption_key)
check = check.slice(0, 4)
const cbuf = ByteBuffer.fromBinary(check.toString('binary'), ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
check = cbuf.readUint32()
if (checksum) {
if (check !== checksum)
throw new Error('Invalid key')
message = cryptoJsDecrypt(message, key, iv)
} else {
message = cryptoJsEncrypt(message, key, iv)
}
return {nonce, message, checksum: check}
}
// priv_to_pub: private_key.toPublic().toString(),
// pub: public_key.toString(),
// nonce: nonce.toString(),
// message: message.length,
// checksum,
// S: S.toString('hex'),
// encryption_key: encryption_key.toString('hex'),
// })
const iv = encryption_key.slice(32, 48)
const key = encryption_key.slice(0, 32)
// check is first 64 bit of sha256 hash treated as uint64_t truncated to 32 bits.
let check = hash.sha256(encryption_key)
check = check.slice(0, 4)
const cbuf = ByteBuffer.fromBinary(check.toString('binary'), ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
check = cbuf.readUint32()
if (checksum) {
if (check !== checksum)
throw new Error('Invalid key')
message = cryptoJsDecrypt(message, key, iv)
} else {
message = cryptoJsEncrypt(message, key, iv)
}
return {nonce, message, checksum: check}
}
fromBuffer(buffer) {
let b = ByteBuffer.fromBinary(buffer.toString(), ByteBuffer.LITTLE_ENDIAN);
return type.fromByteBuffer(b);
},
fromBuffer(buffer){
var b = ByteBuffer.fromBinary(buffer.toString("binary"), ByteBuffer.LITTLE_ENDIAN);
return this.fromByteBuffer(b);
}
fromBuffer(buffer){
var b = ByteBuffer.fromBinary(buffer.toString(), ByteBuffer.LITTLE_ENDIAN);
return type.fromByteBuffer(b);
},
fromBinary(string) {
let b = ByteBuffer.fromBinary(string, ByteBuffer.LITTLE_ENDIAN);
return type.fromByteBuffer(b);
},
fromBinary(string) {
var b = ByteBuffer.fromBinary(string, ByteBuffer.LITTLE_ENDIAN);
return type.fromByteBuffer(b);
},
let bufferToNumber = (buf, type = "Uint32") =>
ByteBuffer.fromBinary(buf.toString("binary"), ByteBuffer.LITTLE_ENDIAN)["read" + type]()