Closed iamgabrielsoft closed 2 years ago
Can you give me more information, please? I think your report got its formatting confused a little.
yeah sure...
I was using the cbor package to decode a message but ran into this error ----> Error: Unexpected data: 0x44
return cbor.decodeFirst(payload) .then((obj) => { let type = options.defaultType ? options.defaultType : this.Signtag; if (obj instanceof Tagged) { if (obj.tag !== this.Signtag && obj.tag !== this.Sign1Tag) { throw new Error('Unexpected cbor tag, \'' + obj.tag + '\''); } type = obj.tag; obj = obj.value; }
});``
Your payload includes multiple cbor data items.
const result = await cbor.decodeFirst('014401020304')
where the first byte (01
) is a complete CBOR data item, and decodeFirst by default throws an error in that situation. Try this:
const extendedResult = await cbor.decodeFirst('0144', {extendedResults: true})
// { value: 1, bytes: <Buffer 01>, length: 1, unused: <Buffer 44> }
const result = extendedResult.value
Alternately, it is more efficient to treat the decoder as a stream:
const d = new cbor.Decoder()
d.on('data', value => console.log(value))
d.write(payload)
cbor.Decoder
is a class. cbor.decodeFirst
is a function, which is also available as cbor.Decoder.decodeFirst
.
Error: Unexpected data: 0x44. Sorry but till getting the error
Can you please post the code that you're currently running?
return cbor.decodeFirst(payload)
.then((obj) => {
let type = options.defaultType ? options.defaultType : this.Signtag;
if (obj instanceof Tagged) {
if (obj.tag !== this.Signtag && obj.tag !== this.Sign1Tag) {
throw new Error('Unexpected cbor tag, \'' + obj.tag + '\'');
}
type = obj.tag;
obj = obj.value;
}
if (!Array.isArray(obj)) {
throw new Error('Expecting Array');
}
if (obj.length !== 4) {
throw new Error('Expecting Array of lenght 4');
}
let [p, u, plaintext, signers] = obj;
if (type === SignTag && !Array.isArray(signers)) {
throw new Error('Expecting signature Array');
}
p = (!p.length) ? this.EMPTY_BUFFER : cbor.decodeFirstSync(p);
u = (!u.size) ? this.EMPTY_BUFFER : u;
let signer = (type === SignTag ? this.getSigner(signers, verifier) : signers);
if (!signer) {
throw new Error('Failed to find signer with kid' + verifier.key.kid);
}
if (type === SignTag) {
const externalAAD = verifier.externalAAD || this.EMPTY_BUFFER;
let [signerP, , sig] = signer;
signerP = (!signerP.length) ? this.EMPTY_BUFFER : signerP;
p = (!p.size) ? this.EMPTY_BUFFER : cbor.encode(p);
const signerPMap = cbor.decode(signerP);
const alg = signerPMap.get(Common.HeaderParameters.alg);
const SigStructure = [
'Signature',
p,
signerP,
externalAAD,
plaintext
];
return this.doVerify(SigStructure, verifier, alg, sig)
.then(() => {
return plaintext;
});
} else {
const externalAAD = verifier.externalAAD || this.EMPTY_BUFFER;
const alg = this.getCommonParameter(p, u, Common.HeaderParameters.alg);
p = (!p.size) ? this.EMPTY_BUFFER : cbor.encode(p);
const SigStructure = [
'Signature1',
p,
externalAAD,
plaintext
];
return this.doVerify(SigStructure, verifier, alg, signer)
.then(() => {
return plaintext;
});
}
});
The paramters am passing are in Buffer
change the first two lines to:
return cbor.decodeFirst(payload, {extendedResults: true})
.then(({value: obj}) => {
return cbor.decodeFirst(payload, {extendedResults: true}) .then(({value: obj}) => { console.log("value", obj) })
Till the same
what is the output of:
return cbor.decodeFirst(payload, {extendedResults: true})
.catch(e => console.log(payload.toString('hex'))
Error: Unexpected data: 0x44
at Decoder._parse (C:\Users\Softbook\Documents\Main Programs\Open Source\cose-js\node_modules\cbor\lib\decoder.js:357:15)
at _parse.next (
Do you have another place where you're calling decodeFirst
than the one you changed?
yeah i do but passing a payload parameter
in another file
Is your full project available on GitHub or somewhere else where I can see it?
Or mail me a zip file to the email address in the package.json file for cbor.
First off, you're using an ancient version of the cbor package. Please update to the latest.
hahaha😜
to what version??
i installed version + cbor@3.0.3
Till the same error
Try installing cbor@8.0.0
I use https://github.com/raineorshine/npm-check-updates to ensure all of my dependencies are up-to-date.
yeah siure...
now getting err UnexpectedDataError: Unexpected data: 0x44
How are you getting this error to occur? I updated my local copy of your repo to cbor@8, pulled in the git submodules, and all of the tests pass.
wooow....
Probably am runing the example file sepately...
Also, it looks like you changed one instance of decodeFirst
(in encrypt.js), but there are several more. In particular, ensure that you've changed the one on line 188 of sign.js.
This is likely an issue in how you are calling @erdtman's code. Perhaps you could get his help.
Why getting this error while `
decodeFirst`
Please somebody respond to this....