Skip to content

Commit 45ffe0f

Browse files
committed
RequestLinkEncoding: verify allowed byte length of Nimiq request link messages
1 parent 7a37e72 commit 45ffe0f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/request-link-encoding/RequestLinkEncoding.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ValidationUtils } from '../validation-utils/ValidationUtils';
22
import { FormattableNumber, toNonScientificNumberString } from '../formattable-number/FormattableNumber';
3+
import { Utf8Tools } from '../utf8-tools/Utf8Tools';
34

45
// this imports only the type without bundling the library
56
type BigInteger = import('big-integer').BigInteger;
@@ -222,7 +223,11 @@ export function createNimiqRequestLink(
222223

223224
if (!ValidationUtils.isValidAddress(recipient)) throw new Error(`Not a valid address: ${recipient}`);
224225
if (amount && !isUnsignedSafeInteger(amount)) throw new Error(`Not a valid amount: ${amount}`);
225-
if (message && typeof message !== 'string') throw new Error(`Not a valid message: ${message}`);
226+
if (message && (
227+
typeof message !== 'string'
228+
// Message length is limited to 64 bytes, see BasicAccount.verifyIncomingTransaction in Nimiq core.
229+
|| Utf8Tools.stringToUtf8ByteArray(message).byteLength > 64
230+
)) throw new Error(`Not a valid message: ${message}`);
226231
if (label && typeof label !== 'string') throw new Error(`Not a valid label: ${label}`);
227232

228233
recipient = ValidationUtils.normalizeAddress(recipient).replace(/ /g, ''); // normalize and strip spaces
@@ -300,6 +305,9 @@ function parseNimiqParams(params: NimiqParams): ParsedNimiqParams | null {
300305

301306
const { label, message } = params;
302307

308+
// Message length is limited to 64 bytes, see BasicAccount.verifyIncomingTransaction in Nimiq core.
309+
if (message && Utf8Tools.stringToUtf8ByteArray(message).byteLength > 64) return null;
310+
303311
return { recipient, amount, label, message };
304312
}
305313

0 commit comments

Comments
 (0)