Skip to content

Commit 0128557

Browse files
feat: CID v1 default encoding is base32 #78
BREAKING CHANGE: The default was base58btc, it is now base32. go-ipfs v0.4.21 has implemented this.
1 parent bc39fe8 commit 0128557

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/Cid.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ void EnsureMutable()
7373
/// <item><description><see cref="Hash"/> algorithm name equals "sha2-256"</description></item>
7474
/// </list>
7575
/// </para>
76+
/// <para>
77+
/// </para>
78+
/// The default <see cref="Encoding"/> is "base32" when the
79+
/// <see cref="Version"/> is not zero.
7680
/// </remarks>
7781
public int Version
7882
{
@@ -83,6 +87,10 @@ public int Version
8387
set
8488
{
8589
EnsureMutable();
90+
if (version == 0 && value > 0 && Encoding == "base58btc")
91+
{
92+
encoding = "base32";
93+
}
8694
version = value;
8795
}
8896
}
@@ -91,7 +99,7 @@ public int Version
9199
/// The <see cref="MultiBase"/> encoding of the CID.
92100
/// </summary>
93101
/// <value>
94-
/// base58btc, base64, etc. Defaults to <see cref="MultiBase.DefaultAlgorithmName"/>.
102+
/// base58btc, base32, base64, etc. Defaults to <see cref="MultiBase.DefaultAlgorithmName"/>.
95103
/// </value>
96104
/// <remarks>
97105
/// Sets <see cref="Version"/> to 1, when setting a value that
@@ -107,11 +115,11 @@ public string Encoding
107115
set
108116
{
109117
EnsureMutable();
110-
encoding = value;
111118
if (Version == 0 && value != "base58btc")
112119
{
113120
Version = 1;
114121
}
122+
encoding = value;
115123
}
116124
}
117125

test/CidTest.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void MultiHash_is_Cid_V1()
4141
Cid cid = mh;
4242
Assert.AreEqual(1, cid.Version);
4343
Assert.AreEqual("dag-pb", cid.ContentType);
44-
Assert.AreEqual("base58btc", cid.Encoding);
44+
Assert.AreEqual("base32", cid.Encoding);
4545
Assert.AreSame(mh, cid.Hash);
4646
}
4747

@@ -80,7 +80,9 @@ public void Encode_V1()
8080
ContentType = "raw",
8181
Hash = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
8282
};
83-
Assert.AreEqual("zb2rhj7crUKTQYRGCRATFaQ6YFLTde2YzdqbbhAASkL9uRDXn", cid.Encode());
83+
Assert.AreEqual(1, cid.Version);
84+
Assert.AreEqual("base32", cid.Encoding);
85+
Assert.AreEqual("bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e", cid.Encode());
8486
}
8587

8688
[TestMethod]
@@ -93,7 +95,8 @@ public void Encode_Upgrade_to_V1_ContentType()
9395
Hash = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
9496
};
9597
Assert.AreEqual(1, cid.Version);
96-
Assert.AreEqual("zb2rhj7crUKTQYRGCRATFaQ6YFLTde2YzdqbbhAASkL9uRDXn", cid.Encode());
98+
Assert.AreEqual("base32", cid.Encoding);
99+
Assert.AreEqual("bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e", cid.Encode());
97100
}
98101

99102
[TestMethod]
@@ -119,7 +122,8 @@ public void Encode_Upgrade_to_V1_Hash()
119122
Hash = mh
120123
};
121124
Assert.AreEqual(1, cid.Version);
122-
Assert.AreEqual("zBunRFxZVcKeu8wAbUg92z2JK6UukiL7EnPR1D6TZaQCsPpRe7KzcmioKFyi2oEZd9ffwpbKTib1pucMQrDyRnAdaqwbB", cid.Encode());
125+
Assert.AreEqual("base32", cid.Encoding);
126+
Assert.AreEqual("bafybgqfnbq34ghljwmk7hka7cpem3zybbffnsfzfxinq3qyztsuxcntbxaua23xx42hrgptcchrolkndcucelv3pc4eoarjbwdxagtylboxsm", cid.Encode());
123127
}
124128

125129
[TestMethod]

test/DagNodeTest.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ public void Hashing_Algorithm()
118118
var node = new DagNode(abc, null, "sha2-512");
119119
CollectionAssert.AreEqual(abc, node.DataBytes);
120120
Assert.AreEqual(0, node.Links.Count());
121-
// Had to change the test, because sha2-512 always generates a CID V1.
122-
// Assert.AreEqual("8Vv347foTHxpLZDdguzcTp7mjBmySjWK1VF36Je7io4ZKHo28fefMFr28LSv757yTcaRzn1dRqPB6WWFpYvbd5YXca", (string)node.Id);
123-
Assert.AreEqual("zBunREkQwjzkZwU71RKBcv2n3XwkTehtzVLkeUPTKjtBDBs2pnGRHLcxt3bGqzX38BkuNom2bQquyPj5Fh7dBZV3UdXPU", (string)node.Id);
121+
Assert.AreEqual("bafybgqdqrys7323fuivxoixir7nnsfqmsneuuseg6mkbmcjgj4xaq7suehcmbghv5sbtxu57ccnhqjggxx7iz5p77gkcrhv2i3pj3yhv7fi56", (string)node.Id);
124122
Assert.AreEqual(5, node.Size);
125123

126124
RoundtripTest(node);

0 commit comments

Comments
 (0)