js-algorand-sdk icon indicating copy to clipboard operation
js-algorand-sdk copied to clipboard

Why the `args` of transaction constructor are strictly required to be `UInt8Array`?

Open HarmlessEvil opened this issue 2 years ago • 2 comments

I noticed the following code in the transaction constructor:

txn.appArgs.forEach((arg) => {
    if (arg.constructor !== Uint8Array)
        throw Error('each element of AppArgs must be a Uint8Array.');
});

It forces the args to be of type Uint8Array, but ignores the inheritance tree. I stumbled upon this when tried to pass Buffer as transaction args, but received this error. My question is: is it intentional to force args to be strictly UInt8Array? Because I'd suggest to use instanceof here if it won't break anything.

HarmlessEvil avatar Aug 24 '22 12:08 HarmlessEvil

@jasonpaulos do you know why this is?

winder avatar Sep 29 '22 14:09 winder

I believe this is because Uint8Array and Buffer have some differences, and we like to maintain consistency internally.

That being said, I think it'd be reasonable for the constructor to convert any Buffer arguments to Uint8Arrays instead of erroring.

jasonpaulos avatar Sep 29 '22 21:09 jasonpaulos