Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// JSONify the claim
let stringifiedClaim = JSON.stringify(claim)
let encodedClaim = encode(stringifiedClaim)
let stringifiedHeader = JSON.stringify({
alg: this.algorithmCode,
typ: "JWT",
kid: this.keyId,
})
let encodedHeader = encode(stringifiedHeader)
let signature = await this.signer.sign(
new Buffer(`${encodedHeader}.${encodedClaim}`)
)
return `${encodedHeader}.${encodedClaim}.${fromBase64(
signature.toString("base64")
)}`
}
payer: quoteRequest.payer,
amount: quoteResponse.transferAmount,
transactionType: quoteRequest.transactionType,
note: quoteResponse.note
};
const ilpData = Buffer.from(base64url(JSON.stringify(transactionObject)));
const packetInput = {
amount: this._getIlpCurrencyAmount(quoteResponse.transferAmount), // unsigned 64bit integer as a string
account: this._getIlpAddress(quoteRequest.payee), // ilp address
data: ilpData // base64url encoded attached data
};
const packet = ilpPacket.serializeIlpPayment(packetInput);
let base64encodedIlpPacket = base64url.fromBase64(packet.toString('base64')).replace('"', '');
let generatedFulfilment = this.caluclateFulfil(base64encodedIlpPacket).replace('"', '');
let generatedCondition = this.calculateConditionFromFulfil(generatedFulfilment).replace('"', '');
const ret = {
fulfilment: generatedFulfilment,
ilpPacket: base64encodedIlpPacket,
condition: generatedCondition
};
this.logger.log(`Generated ILP: transaction object: ${util.inspect(transactionObject)}\nPacket input: ${util.inspect(packetInput)}\nOutput: ${util.inspect(ret)}`);
return ret;
}
const caluclateFulfil = (base64EncodedPacket, rawSecret) => {
const encodedSecret = Buffer.from(rawSecret).toString('base64')
const hmacsignature = Crypto.createHmac('sha256', Buffer.from(encodedSecret, 'ascii'))
.update(Buffer.from(base64EncodedPacket, 'ascii'))
const generatedFulfilment = hmacsignature.digest('base64')
console.log(`calculateFulfil:: generatedFulfilment=${generatedFulfilment} | length:${generatedFulfilment.length}`)
return base64url.fromBase64(generatedFulfilment)
}
caluclateFulfil(base64EncodedPacket) {
var encodedSecret = Buffer.from(this.secret).toString('base64');
var hmacsignature = Crypto.createHmac('sha256', new Buffer(encodedSecret, 'ascii'))
.update(new Buffer(base64EncodedPacket, 'ascii'));
var generatedFulfilment = hmacsignature.digest('base64');
return base64url.fromBase64(generatedFulfilment);
}
calculateConditionFromFulfil (fulfilment) {
var preimage = base64url.toBuffer(fulfilment);
if (preimage.length !== 32) {
throw new Error('Interledger preimages must be exactly 32 bytes.');
}
var calculatedConditionDigest = this._sha256(preimage);
return base64url.fromBase64(calculatedConditionDigest);
}
var dst = Buffer.allocUnsafe(rPadding + rLength + sPadding + sLength);
for (offset = 0; offset < rPadding; ++offset) {
dst[offset] = 0;
}
signature.copy(dst, offset, rOffset + Math.max(-rPadding, 0), rOffset + rLength);
offset = paramBytes;
for (var o = offset; offset < o + sPadding; ++offset) {
dst[offset] = 0;
}
signature.copy(dst, offset, sOffset + Math.max(-sPadding, 0), sOffset + sLength);
dst = dst.toString('base64');
dst = base64Url(dst);
return dst;
}
crypto.randomBytes(32, (error, buffer) => {
if (error) reject(error);
else resolve(base64url.fromBase64(buffer.toString('base64')));
});
});
sign (input, key) {
//assertSufficientKeyLength(key)
let {bitlength} = this
let hmac = crypto.createHmac(`sha${bitlength}`, key)
hmac.update(input)
return base64url.fromBase64(hmac.digest('base64'))
}
const fulfilmentToCondition = (fulfilment) => {
const hashSha256 = Crypto.createHash('sha256')
const preimage = base64url.toBuffer(fulfilment)
if (preimage.length !== 32) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.INTERNAL_SERVER_ERROR, 'Interledger preimages must be exactly 32 bytes')
}
const calculatedConditionDigest = hashSha256.update(preimage).digest('base64')
Logger.debug(`calculatedConditionDigest=${calculatedConditionDigest}`)
const calculatedConditionUrlEncoded = base64url.fromBase64(calculatedConditionDigest)
Logger.debug(`calculatedConditionUrlEncoded=${calculatedConditionUrlEncoded}`)
return calculatedConditionUrlEncoded
}