Skip to content

Commit

Permalink
feat: CID v1 default encoding is base32 #78
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The default was base58btc, it is now base32.
go-ipfs v0.4.21 has implemented this.
  • Loading branch information
richardschneider committed Jun 15, 2019
1 parent bc39fe8 commit 0128557
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/Cid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ void EnsureMutable()
/// <item><description><see cref="Hash"/> algorithm name equals "sha2-256"</description></item>
/// </list>
/// </para>
/// <para>
/// </para>
/// The default <see cref="Encoding"/> is "base32" when the
/// <see cref="Version"/> is not zero.
/// </remarks>
public int Version
{
Expand All @@ -83,6 +87,10 @@ public int Version
set
{
EnsureMutable();
if (version == 0 && value > 0 && Encoding == "base58btc")
{
encoding = "base32";
}
version = value;
}
}
Expand All @@ -91,7 +99,7 @@ public int Version
/// The <see cref="MultiBase"/> encoding of the CID.
/// </summary>
/// <value>
/// base58btc, base64, etc. Defaults to <see cref="MultiBase.DefaultAlgorithmName"/>.
/// base58btc, base32, base64, etc. Defaults to <see cref="MultiBase.DefaultAlgorithmName"/>.
/// </value>
/// <remarks>
/// Sets <see cref="Version"/> to 1, when setting a value that
Expand All @@ -107,11 +115,11 @@ public string Encoding
set
{
EnsureMutable();
encoding = value;
if (Version == 0 && value != "base58btc")
{
Version = 1;
}
encoding = value;
}
}

Expand Down
12 changes: 8 additions & 4 deletions test/CidTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void MultiHash_is_Cid_V1()
Cid cid = mh;
Assert.AreEqual(1, cid.Version);
Assert.AreEqual("dag-pb", cid.ContentType);
Assert.AreEqual("base58btc", cid.Encoding);
Assert.AreEqual("base32", cid.Encoding);
Assert.AreSame(mh, cid.Hash);
}

Expand Down Expand Up @@ -80,7 +80,9 @@ public void Encode_V1()
ContentType = "raw",
Hash = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
};
Assert.AreEqual("zb2rhj7crUKTQYRGCRATFaQ6YFLTde2YzdqbbhAASkL9uRDXn", cid.Encode());
Assert.AreEqual(1, cid.Version);
Assert.AreEqual("base32", cid.Encoding);
Assert.AreEqual("bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e", cid.Encode());
}

[TestMethod]
Expand All @@ -93,7 +95,8 @@ public void Encode_Upgrade_to_V1_ContentType()
Hash = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4"
};
Assert.AreEqual(1, cid.Version);
Assert.AreEqual("zb2rhj7crUKTQYRGCRATFaQ6YFLTde2YzdqbbhAASkL9uRDXn", cid.Encode());
Assert.AreEqual("base32", cid.Encoding);
Assert.AreEqual("bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e", cid.Encode());
}

[TestMethod]
Expand All @@ -119,7 +122,8 @@ public void Encode_Upgrade_to_V1_Hash()
Hash = mh
};
Assert.AreEqual(1, cid.Version);
Assert.AreEqual("zBunRFxZVcKeu8wAbUg92z2JK6UukiL7EnPR1D6TZaQCsPpRe7KzcmioKFyi2oEZd9ffwpbKTib1pucMQrDyRnAdaqwbB", cid.Encode());
Assert.AreEqual("base32", cid.Encoding);
Assert.AreEqual("bafybgqfnbq34ghljwmk7hka7cpem3zybbffnsfzfxinq3qyztsuxcntbxaua23xx42hrgptcchrolkndcucelv3pc4eoarjbwdxagtylboxsm", cid.Encode());
}

[TestMethod]
Expand Down
4 changes: 1 addition & 3 deletions test/DagNodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ public void Hashing_Algorithm()
var node = new DagNode(abc, null, "sha2-512");
CollectionAssert.AreEqual(abc, node.DataBytes);
Assert.AreEqual(0, node.Links.Count());
// Had to change the test, because sha2-512 always generates a CID V1.
// Assert.AreEqual("8Vv347foTHxpLZDdguzcTp7mjBmySjWK1VF36Je7io4ZKHo28fefMFr28LSv757yTcaRzn1dRqPB6WWFpYvbd5YXca", (string)node.Id);
Assert.AreEqual("zBunREkQwjzkZwU71RKBcv2n3XwkTehtzVLkeUPTKjtBDBs2pnGRHLcxt3bGqzX38BkuNom2bQquyPj5Fh7dBZV3UdXPU", (string)node.Id);
Assert.AreEqual("bafybgqdqrys7323fuivxoixir7nnsfqmsneuuseg6mkbmcjgj4xaq7suehcmbghv5sbtxu57ccnhqjggxx7iz5p77gkcrhv2i3pj3yhv7fi56", (string)node.Id);
Assert.AreEqual(5, node.Size);

RoundtripTest(node);
Expand Down

0 comments on commit 0128557

Please sign in to comment.