Skip to content

Commit

Permalink
fix: parsing empty maps
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Dec 30, 2023
1 parent 22e03d0 commit e89ce2a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"blake2b": "^2.1.4",
"cbor": "^8.1.0"
"cbor": "^9.0.1"
},
"bugs": {
"url": "https://github.com/vacuumlabs/cardano-hw-interop-lib/issues"
Expand Down
19 changes: 19 additions & 0 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,31 @@ export function createParser<T, A extends unknown[]>(
}
/* eslint-enable no-redeclare */

// inspired by lodash _isEmpty
// https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L11479
const _isEmptyObject = (data: unknown): boolean => {
const mapTag = '[object Map]'
const setTag = '[object Set]'
const tag = Object.prototype.toString.call(data)
if (tag === mapTag || tag === setTag) {
return false
} else {
return data instanceof Object && Object.keys(data).length === 0
}
}

export const parseMap = <K, V>(
data: unknown,
parseKey: Parser<K>,
parseValue: Parser<V>,
errMsg: ParseErrorReason,
): Map<K, V> => {
// an empty map (CBOR a0) is parsed as an empty object by the cbor library
if (_isEmptyObject(data)) {
return new Map()
}

// non-empty map
validate(isMap(data), errMsg)
return new Map(
Array.from(data.entries()).map(([key, value]) => [
Expand Down
51 changes: 51 additions & 0 deletions test/integration/__fixtures__/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,57 @@ export const ValidTransactionBodyTestCases: ValidTransactionBodyTestCase[] = [
donation: undefined,
},
},
{
testName: 'Tx body with empty withdrawals',
cbor: 'a60081825820bc8bf52ea894fb8e442fe3eea628be87d0c9a37baef185b70eb00a5c8a849d3b0001818258390180f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa122a946b9ad3d2ddf029d3a828f0468aece76895f15c9efbd69b42771a0023583c021a00029b75031a01a3bd8f05a0061a01a3bd8f',
txBody: {
inputs: {
items: [
{
transactionId: toFixLenBuffer(
'bc8bf52ea894fb8e442fe3eea628be87d0c9a37baef185b70eb00a5c8a849d3b',
32,
),
index: toUint(0),
},
],
hasTag: false,
} as CddlSet<TransactionInput>,
outputs: [
{
format: TxOutputFormat.ARRAY_LEGACY,
address: toFixLenBuffer(
'0180f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa122a946b9ad3d2ddf029d3a828f0468aece76895f15c9efbd69b4277',
57
),
amount: {
type: AmountType.WITHOUT_MULTIASSET,
coin: toUint(2316348),
},
datumHash: undefined,
},
],
fee: toUint(170869),
ttl: toUint(27508111),
certificates: undefined,
withdrawals: [],
update: toUint(27508111),
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: undefined,
collateralInputs: undefined,
requiredSigners: undefined,
networkId: undefined,
collateralReturnOutput: undefined,
totalCollateral: undefined,
referenceInputs: undefined,
votingProcedures: undefined,
proposalProcedures: undefined,
treasury: undefined,
donation: undefined,
},
},
{
testName: 'Tx body with multiple outputs and multiassets',
cbor: 'a40081825820b64ae44e1195b04663ab863b62337e626c65b0c9855a9fbb9ef4458f81a6f5ee1bffffffffffffffff018282583930167f6dbf610ae030f043adb1f3af78754ed9595ad4ac1f7ed9ff6466760fb6955d1217b1f1f208df6d45ab23c9e17b0c984a2d3a22bbbfb8821a0001e91fa1581cd7a7c6999786354b6dbee181a2f562a628a75fce126f4da40ce5d9b2a146546f6b656e3101825839000743d16cfe3c4fcc0c11c2403bbc10dbc7ecdd4477e053481a368e7a06e2ae44dff6770dc0f4ada3cf4cf2605008e27aecdb332ad349fda7821a3dbb8b21a1581cd7a7c6999786354b6dbee181a2f562a628a75fce126f4da40ce5d9b2a246546f6b656e311a00155d9746546f6b656e321a00beeff1021a0003050309a1581cd7a7c6999786354b6dbee181a2f562a628a75fce126f4da40ce5d9b2a246546f6b656e313a0098967f46546f6b656e321b7fffffffffffffff',
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,10 @@ camelcase@^6.0.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a"
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==

cbor@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/cbor/-/cbor-8.1.0.tgz#cfc56437e770b73417a2ecbfc9caf6b771af60d5"
integrity sha512-DwGjNW9omn6EwP70aXsn7FQJx5kO12tX0bZkaTjzdVFM6/7nhA4t0EENocKGx6D2Bch9PE2KzCUf5SceBdeijg==
cbor@^9.0.1:
version "9.0.1"
resolved "https://registry.yarnpkg.com/cbor/-/cbor-9.0.1.tgz#b16e393d4948d44758cd54ac6151379d443b37ae"
integrity sha512-/TQOWyamDxvVIv+DY9cOLNuABkoyz8K/F3QE56539pGVYohx0+MEA1f4lChFTX79dBTBS7R1PF6ovH7G+VtBfQ==
dependencies:
nofilter "^3.1.0"

Expand Down

0 comments on commit e89ce2a

Please sign in to comment.