Merge pull request #56 from shocknet/spontpay-encoding

correct encoding of spontaneous payment
This commit is contained in:
Daniel Lugo 2020-04-07 20:10:12 -04:00 committed by GitHub
commit fa7d3c7c27

View file

@ -420,7 +420,7 @@ exports.isOrderResponse = o => {
* @typedef {import('./schema-types').EncSpontPayment} EncSpontPayment * @typedef {import('./schema-types').EncSpontPayment} EncSpontPayment
*/ */
const ENC_SPONT_PAYMENT_PREFIX = '$$__SHOCKWALLET__SPONT__PAYMENT__' const ENC_SPONT_PAYMENT_PREFIX = '$$__SHOCKWALLET__SPONT__PAYMENT'
/** /**
* @param {string} s * @param {string} s
@ -446,7 +446,7 @@ exports.isEncodedSpontPayment = isEncodedSpontPayment
exports.decodeSpontPayment = sp => { exports.decodeSpontPayment = sp => {
try { try {
const [preimage, amtStr, memo] = sp const [preimage, amtStr, memo] = sp
.slice(ENC_SPONT_PAYMENT_PREFIX.length) .slice((ENC_SPONT_PAYMENT_PREFIX + '__').length)
.split('__') .split('__')
if (typeof preimage !== 'string') { if (typeof preimage !== 'string') {
@ -490,13 +490,24 @@ exports.decodeSpontPayment = sp => {
} }
/** /**
*
* @param {number} amt * @param {number} amt
* @param {string} memo * @param {string} memo
* @param {string} preimage * @param {string} preimage
* @returns {EncSpontPayment} * @returns {EncSpontPayment}
*/ */
exports.encodeSpontaneousPayment = (amt, memo, preimage) => { exports.encodeSpontaneousPayment = (amt, memo, preimage) => {
if (typeof amt !== 'number') {
throw new TypeError('amt must be a number')
}
if (typeof memo !== 'string') {
throw new TypeError('memo must be an string')
}
if (typeof preimage !== 'string') {
throw new TypeError('preimage must be an string')
}
if (amt <= 0) { if (amt <= 0) {
throw new RangeError('Amt must be greater than zero') throw new RangeError('Amt must be greater than zero')
} }