Skip to content

Commit 29ab656

Browse files
committed
format
1 parent 4d41f4e commit 29ab656

File tree

9 files changed

+124
-120
lines changed

9 files changed

+124
-120
lines changed

src/CborDecode.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/*******************************************************************************
1+
/**
2+
*
23
* (c) 2022 Zondax AG
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,7 +13,8 @@
1213
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314
* See the License for the specific language governing permissions and
1415
* limitations under the License.
15-
********************************************************************************/
16+
*
17+
*/
1618
// THIS CODE WAS SECURITY REVIEWED BY KUDELSKI SECURITY, BUT NOT FORMALLY AUDITED
1719

1820
// SPDX-License-Identifier: Apache-2.0

src/CidCbor.sol

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ library CidCbor {
2424

2525
function cidMatches(Cid memory one, Cid memory other) internal pure returns (bool) {
2626
require(!(one.nullish && other.nullish), "two nullish cids should not be compared");
27-
return Compare.bytesMatch(abi.encodePacked(one.prefix, one.sha, one.nullish), abi.encodePacked(other.prefix, other.sha, other.nullish));
27+
return Compare.bytesMatch(
28+
abi.encodePacked(one.prefix, one.sha, one.nullish), abi.encodePacked(other.prefix, other.sha, other.nullish)
29+
);
2830
}
2931

3032
function expectCidTag(bytes memory cborData, uint byteIdx) internal pure returns (uint) {
@@ -36,20 +38,24 @@ library CidCbor {
3638
return byteIdx;
3739
}
3840

39-
function readCid(bytes memory cborData, uint byteIdx, bool expectTag) internal pure returns (Cid memory ret, uint) {
40-
if(expectTag) {
41+
function readCid(bytes memory cborData, uint byteIdx, bool expectTag)
42+
internal
43+
pure
44+
returns (Cid memory ret, uint)
45+
{
46+
if (expectTag) {
4147
(byteIdx) = expectCidTag(cborData, byteIdx);
4248
}
4349

44-
if(cborData.isNullNext(byteIdx)) {
50+
if (cborData.isNullNext(byteIdx)) {
4551
ret.nullish = true;
4652
return (ret, byteIdx + 1);
4753
}
4854

4955
bytes memory cidBytes;
5056
(cidBytes, byteIdx) = cborData.readBytes(byteIdx);
5157

52-
if(cidBytes.length == 0) {
58+
if (cidBytes.length == 0) {
5359
ret.nullish = true;
5460
return (ret, byteIdx);
5561
}
@@ -63,20 +69,18 @@ library CidCbor {
6369
require(uint8(cidBytes[3]) == MULTIHASH_SHA_256, "expected CID multihash sha-256");
6470
require(uint8(cidBytes[4]) == MULTIHASH_SIZE_32, "expected CID content size 32 bytes");
6571

66-
ret.prefix = bytes4(abi.encodePacked(
67-
cidBytes[1], cidBytes[2], cidBytes[3], cidBytes[4]
68-
));
72+
ret.prefix = bytes4(abi.encodePacked(cidBytes[1], cidBytes[2], cidBytes[3], cidBytes[4]));
6973

7074
// cid data length plus prefix length
7175
require(cidBytes.length == 1 + 4 + MULTIHASH_SIZE_32, "expected cid data to be 37 bytes");
7276

7377
// TODO: ;_;
7478
bytes memory shaBytes = new bytes(32);
75-
for(uint i = 0; i < 32; i++) {
79+
for (uint i = 0; i < 32; i++) {
7680
shaBytes[i] = cidBytes[5 + i];
7781
}
7882
ret.sha = bytes32(shaBytes);
7983

8084
return (ret, byteIdx);
8185
}
82-
}
86+
}

src/CommitCbor.sol

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.28;
3+
34
import "./CidCbor.sol";
45
import "./Compare.sol";
56

67
library CommitCbor {
78
using CBORDecoder for bytes;
9+
810
uint8 private constant COMMIT_VERSION = 3;
911

1012
struct Commit {
1113
string did;
1214
uint8 version;
1315
CidCbor.Cid data;
1416
string rev;
15-
CidCbor.Cid prev;
17+
CidCbor.Cid prev;
1618
}
1719

1820
function readCommit(bytes memory cborData, uint byteIdx) internal pure returns (Commit memory ret, uint) {
@@ -21,28 +23,24 @@ library CommitCbor {
2123

2224
require(mapLen == 5, "expected 5 fields in commit");
2325

24-
for(uint i = 0; i < mapLen; i++) {
26+
for (uint i = 0; i < mapLen; i++) {
2527
string memory mapKey;
2628
(mapKey, byteIdx) = cborData.readString(byteIdx);
27-
if(Compare.stringsMatch(mapKey, "did")) {
29+
if (Compare.stringsMatch(mapKey, "did")) {
2830
(ret.did, byteIdx) = cborData.readString(byteIdx);
2931
require(bytes(ret.did).length == 32, "did string must be 32 bytes");
30-
}
31-
else if(Compare.stringsMatch(mapKey, "version")) {
32+
} else if (Compare.stringsMatch(mapKey, "version")) {
3233
(ret.version, byteIdx) = cborData.readUInt8(byteIdx);
3334
require(ret.version == COMMIT_VERSION, "unexpected commit version");
34-
}
35-
else if(Compare.stringsMatch(mapKey, "data")) {
35+
} else if (Compare.stringsMatch(mapKey, "data")) {
3636
(ret.data, byteIdx) = CidCbor.readCid(cborData, byteIdx, false);
37-
}
38-
else if(Compare.stringsMatch(mapKey, "rev")) {
37+
} else if (Compare.stringsMatch(mapKey, "rev")) {
3938
(ret.rev, byteIdx) = cborData.readString(byteIdx);
40-
}
41-
else if(Compare.stringsMatch(mapKey, "prev")) {
39+
} else if (Compare.stringsMatch(mapKey, "prev")) {
4240
(ret.prev, byteIdx) = CidCbor.readCid(cborData, byteIdx, false);
4341
}
4442
}
4543

4644
return (ret, byteIdx);
4745
}
48-
}
46+
}

src/IntCbor.sol

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/*******************************************************************************
1+
/**
2+
*
23
* (c) 2022 Zondax AG
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,7 +13,8 @@
1213
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314
* See the License for the specific language governing permissions and
1415
* limitations under the License.
15-
********************************************************************************/
16+
*
17+
*/
1618
//
1719
// THIS CODE WAS SECURITY REVIEWED BY KUDELSKI SECURITY, BUT NOT FORMALLY AUDITED
1820

@@ -29,5 +31,4 @@ import "./Misc.sol";
2931
library Uint64CBOR {
3032
using CBOR for CBOR.CBORBuffer;
3133
using CBORDecoder for bytes;
32-
3334
}

src/Misc.sol

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/*******************************************************************************
1+
/**
2+
*
23
* (c) 2022 Zondax AG
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,13 +13,13 @@
1213
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314
* See the License for the specific language governing permissions and
1415
* limitations under the License.
15-
********************************************************************************/
16+
*
17+
*/
1618
// THIS CODE WAS SECURITY REVIEWED BY KUDELSKI SECURITY, BUT NOT FORMALLY AUDITED
1719

1820
// SPDX-License-Identifier: Apache-2.0
1921
pragma solidity ^0.8.17;
2022

21-
2223
/// @title Library containing miscellaneous functions used on the project
2324
/// @author Zondax AG
2425
library Misc {
@@ -75,4 +76,4 @@ library Misc {
7576
function getBoolSize() internal pure returns (uint256) {
7677
return getPrefixSize(1);
7778
}
78-
}
79+
}

src/TreeNodeCbor.sol

Lines changed: 76 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,97 @@
1-
21
// SPDX-License-Identifier: MIT
32
pragma solidity ^0.8.28;
43

54
import "./CidCbor.sol";
65

76
library TreeNodeCbor {
8-
using CBORDecoder for bytes;
9-
10-
struct TreeNode {
11-
CidCbor.Cid left;
12-
TreeNodeEntry[] entries;
13-
}
7+
using CBORDecoder for bytes;
148

15-
struct TreeNodeEntry {
16-
string key;
17-
CidCbor.Cid value;
18-
CidCbor.Cid tree;
19-
}
20-
21-
struct TreeNodeE {
22-
uint8 p; // prefixlen
23-
bytes k; // keysuffix
24-
CidCbor.Cid v; // value
25-
CidCbor.Cid t; // tree
26-
}
9+
struct TreeNode {
10+
CidCbor.Cid left;
11+
TreeNodeEntry[] entries;
12+
}
2713

28-
function readNodeE(bytes memory cborData, uint byteIdx) internal pure returns (TreeNodeE[] memory ret, uint) {
29-
uint arrayLen;
30-
(arrayLen, byteIdx) = cborData.readFixedArray(byteIdx);
14+
struct TreeNodeEntry {
15+
string key;
16+
CidCbor.Cid value;
17+
CidCbor.Cid tree;
18+
}
3119

32-
ret = new TreeNodeE[](arrayLen);
33-
for(uint i = 0; i < arrayLen; i++) {
34-
(ret[i], byteIdx) = readE(cborData, byteIdx);
20+
struct TreeNodeE {
21+
uint8 p; // prefixlen
22+
bytes k; // keysuffix
23+
CidCbor.Cid v; // value
24+
CidCbor.Cid t; // tree
3525
}
3626

37-
return (ret, byteIdx);
38-
}
27+
function readNodeE(bytes memory cborData, uint byteIdx) internal pure returns (TreeNodeE[] memory ret, uint) {
28+
uint arrayLen;
29+
(arrayLen, byteIdx) = cborData.readFixedArray(byteIdx);
3930

40-
function readE(bytes memory cborData, uint byteIdx) internal pure returns (TreeNodeE memory ret, uint) {
41-
uint mapLen;
42-
(mapLen, byteIdx) = cborData.readFixedMap(byteIdx);
31+
ret = new TreeNodeE[](arrayLen);
32+
for (uint i = 0; i < arrayLen; i++) {
33+
(ret[i], byteIdx) = readE(cborData, byteIdx);
34+
}
4335

44-
require(mapLen == 4, "expected 4 fields in node entry");
45-
for(uint i = 0; i < mapLen; i++) {
46-
string memory mapKey;
47-
(mapKey, byteIdx) = cborData.readString(byteIdx);
48-
if(Compare.stringsMatch(mapKey, "p")) {
49-
(ret.p, byteIdx) = cborData.readUInt8(byteIdx);
50-
}
51-
else if(Compare.stringsMatch(mapKey, "k")) {
52-
(ret.k, byteIdx) = cborData.readBytes(byteIdx);
53-
}
54-
else if(Compare.stringsMatch(mapKey, 't')) {
55-
(ret.t, byteIdx) = CidCbor.readCid(cborData, byteIdx, false);
56-
}
57-
else if(Compare.stringsMatch(mapKey, 'v')) {
58-
(ret.v, byteIdx) = CidCbor.readCid(cborData, byteIdx, false);
59-
}
36+
return (ret, byteIdx);
6037
}
6138

62-
return (ret, byteIdx);
63-
}
39+
function readE(bytes memory cborData, uint byteIdx) internal pure returns (TreeNodeE memory ret, uint) {
40+
uint mapLen;
41+
(mapLen, byteIdx) = cborData.readFixedMap(byteIdx);
42+
43+
require(mapLen == 4, "expected 4 fields in node entry");
44+
for (uint i = 0; i < mapLen; i++) {
45+
string memory mapKey;
46+
(mapKey, byteIdx) = cborData.readString(byteIdx);
47+
if (Compare.stringsMatch(mapKey, "p")) {
48+
(ret.p, byteIdx) = cborData.readUInt8(byteIdx);
49+
} else if (Compare.stringsMatch(mapKey, "k")) {
50+
(ret.k, byteIdx) = cborData.readBytes(byteIdx);
51+
} else if (Compare.stringsMatch(mapKey, "t")) {
52+
(ret.t, byteIdx) = CidCbor.readCid(cborData, byteIdx, false);
53+
} else if (Compare.stringsMatch(mapKey, "v")) {
54+
(ret.v, byteIdx) = CidCbor.readCid(cborData, byteIdx, false);
55+
}
56+
}
6457

65-
function buildEntryKeys(TreeNodeE[] memory e) internal pure returns (TreeNodeEntry[] memory entries) {
66-
entries = new TreeNodeEntry[](e.length);
67-
bytes memory previousKey = new bytes(0);
68-
for(uint i = 0; i < e.length; i++) {
69-
uint8 p = e[i].p;
70-
bytes memory k = e[i].k;
71-
bytes memory key = new bytes(p + k.length);
72-
for(uint j = 0; j < p; j++) {
73-
key[j] = previousKey[j];
74-
}
75-
for(uint j = p; j < p + k.length; j++) {
76-
key[j] = k[j - p];
77-
}
78-
entries[i].key = string(key);
79-
previousKey = key;
58+
return (ret, byteIdx);
8059
}
81-
return entries;
82-
}
8360

84-
function readTreeNode(bytes memory cborData, uint byteIdx) internal pure returns (TreeNode memory node, uint) {
85-
uint mapLen;
86-
(mapLen, byteIdx) = cborData.readFixedMap(byteIdx);
87-
require(mapLen == 2, "expected 2 fields in node");
88-
for(uint i = 0; i < mapLen; i++) {
89-
string memory mapKey;
90-
(mapKey, byteIdx) = cborData.readString(byteIdx);
91-
if(Compare.stringsMatch(mapKey, "l")) {
92-
(node.left, byteIdx) = CidCbor.readCid(cborData, byteIdx, false);
93-
}
94-
else if(Compare.stringsMatch(mapKey, "e")) {
95-
TreeNodeE[] memory e;
96-
(e, byteIdx) = readNodeE(cborData, byteIdx);
97-
node.entries = buildEntryKeys(e);
98-
}
61+
function buildEntryKeys(TreeNodeE[] memory e) internal pure returns (TreeNodeEntry[] memory entries) {
62+
entries = new TreeNodeEntry[](e.length);
63+
bytes memory previousKey = new bytes(0);
64+
for (uint i = 0; i < e.length; i++) {
65+
uint8 p = e[i].p;
66+
bytes memory k = e[i].k;
67+
bytes memory key = new bytes(p + k.length);
68+
for (uint j = 0; j < p; j++) {
69+
key[j] = previousKey[j];
70+
}
71+
for (uint j = p; j < p + k.length; j++) {
72+
key[j] = k[j - p];
73+
}
74+
entries[i].key = string(key);
75+
previousKey = key;
76+
}
77+
return entries;
9978
}
100-
return (node, byteIdx);
101-
}
10279

80+
function readTreeNode(bytes memory cborData, uint byteIdx) internal pure returns (TreeNode memory node, uint) {
81+
uint mapLen;
82+
(mapLen, byteIdx) = cborData.readFixedMap(byteIdx);
83+
require(mapLen == 2, "expected 2 fields in node");
84+
for (uint i = 0; i < mapLen; i++) {
85+
string memory mapKey;
86+
(mapKey, byteIdx) = cborData.readString(byteIdx);
87+
if (Compare.stringsMatch(mapKey, "l")) {
88+
(node.left, byteIdx) = CidCbor.readCid(cborData, byteIdx, false);
89+
} else if (Compare.stringsMatch(mapKey, "e")) {
90+
TreeNodeE[] memory e;
91+
(e, byteIdx) = readNodeE(cborData, byteIdx);
92+
node.entries = buildEntryKeys(e);
93+
}
94+
}
95+
return (node, byteIdx);
96+
}
10397
}

test/CborDecode.t.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/*******************************************************************************
1+
/**
2+
*
23
* (c) 2023 Zondax AG
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,7 +13,8 @@
1213
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314
* See the License for the specific language governing permissions and
1415
* limitations under the License.
15-
********************************************************************************/
16+
*
17+
*/
1618
//
1719
// THIS CODE WAS SECURITY REVIEWED BY KUDELSKI SECURITY, BUT NOT FORMALLY AUDITED
1820

@@ -152,4 +154,4 @@ contract CborDecodeTest {
152154
(num, index) = input.readUInt8(index);
153155
require(num == 111, "num is not 111");
154156
}
155-
}
157+
}

0 commit comments

Comments
 (0)