From de3b89f6505e8b717f10b02f144e2e79b87c831b Mon Sep 17 00:00:00 2001 From: Ethan Celletti Date: Thu, 2 Feb 2023 14:23:28 -0800 Subject: [PATCH] Fixing regressions --- src/Agent/Models/Certificate.cs | 4 ++-- src/Candid/Mapping/Mappers/OptMapper.cs | 2 +- src/Candid/Models/HashTree.cs | 26 ++++++++++++------------- src/Candid/Models/Values/CandidValue.cs | 5 +++++ 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Agent/Models/Certificate.cs b/src/Agent/Models/Certificate.cs index 6c5b3ed2..75ab653e 100644 --- a/src/Agent/Models/Certificate.cs +++ b/src/Agent/Models/Certificate.cs @@ -44,7 +44,7 @@ public Certificate(HashTree tree, byte[] signature, CertificateDelegation? deleg /// True if the certificate is valid, otherwise false public bool IsValid(byte[] rootPublicKey) { - HashTree.EncodedValue rootHash = this.Tree.BuildRootHash(); + byte[] rootHash = this.Tree.BuildRootHash(); if (this.Delegation != null) { // override the root key to the delegated one @@ -55,7 +55,7 @@ public bool IsValid(byte[] rootPublicKey) } } var blsKey = new DerEncodedPublicKey(rootPublicKey).AsBls(); - return BlsUtil.VerifySignature(blsKey, rootHash.Value, this.Signature); + return BlsUtil.VerifySignature(blsKey, rootHash, this.Signature); } } diff --git a/src/Candid/Mapping/Mappers/OptMapper.cs b/src/Candid/Mapping/Mappers/OptMapper.cs index bcaa79ed..597df4a9 100644 --- a/src/Candid/Mapping/Mappers/OptMapper.cs +++ b/src/Candid/Mapping/Mappers/OptMapper.cs @@ -34,7 +34,7 @@ public object Map(CandidValue value, CandidConverterOptions options) object? innerValue; bool hasValue; - if (opt.Value != CandidValue.Null()) + if (!opt.Value.IsNull()) { innerValue = (this.OverrideInnerMapper ?? options.ResolveMapper(this.InnerType)).Map(opt.Value, options); hasValue = true; diff --git a/src/Candid/Models/HashTree.cs b/src/Candid/Models/HashTree.cs index da3f1985..b12db827 100644 --- a/src/Candid/Models/HashTree.cs +++ b/src/Candid/Models/HashTree.cs @@ -93,7 +93,7 @@ public EncodedValue AsLeaf() public byte[] AsPruned() { this.ValidateType(HashTreeType.Pruned); - return (EncodedValue)this.value!; + return (byte[])this.value!; } /// @@ -235,7 +235,7 @@ public byte[] BuildRootHash() domain_sep(s) = byte(|s|) · s */ SHA256HashFunction hashFunction = new(SHA256.Create()); - EncodedValue rootHash = this.BuildHashInternal(hashFunction); + byte[] rootHash = this.BuildHashInternal(hashFunction); return EncodedValue.WithDomainSeperator("ic-state-root", rootHash); } @@ -359,21 +359,21 @@ public static implicit operator EncodedValue(byte[] bytes) } - internal static byte[] WithDomainSeperator(string value, params EncodedValue[] encodedValues) + internal static byte[] WithDomainSeperator(string value, params byte[][] encodedValues) { // domain_sep(s) = byte(|s|) · s byte[] textBytes = Encoding.UTF8.GetBytes(value); - byte[] bytes = new byte[textBytes.Length + 1 + encodedValues.Sum(v => v.Value.Length)]; + byte[] bytes = new byte[textBytes.Length + 1 + encodedValues.Sum(v => v.Length)]; bytes[0] = (byte)textBytes.Length; int index = 1; textBytes.CopyTo(bytes, index); index++; - foreach (EncodedValue encodedValue in encodedValues) + foreach (byte[] encodedValue in encodedValues) { - encodedValue.Value.CopyTo(bytes, index); - index += encodedValue.Value.Length; + encodedValue.CopyTo(bytes, index); + index += encodedValue.Length; } - return new EncodedValue(bytes); + return bytes; } } @@ -387,18 +387,18 @@ private byte[] BuildHashInternal(SHA256HashFunction hashFunction) break; case HashTreeType.Fork: (HashTree left, HashTree right) = this.AsFork(); - EncodedValue leftHash = left.BuildHashInternal(hashFunction); - EncodedValue rightHash = right.BuildHashInternal(hashFunction); + byte[] leftHash = left.BuildHashInternal(hashFunction); + byte[] rightHash = right.BuildHashInternal(hashFunction); encodedValue = EncodedValue.WithDomainSeperator("ic-hashtree-fork", leftHash, rightHash); break; case HashTreeType.Labeled: (EncodedValue label, HashTree tree) = this.AsLabeled(); - EncodedValue treeHash = tree.BuildHashInternal(hashFunction); - encodedValue = EncodedValue.WithDomainSeperator("ic-hashtree-labeled", label, treeHash); + byte[] treeHash = tree.BuildHashInternal(hashFunction); + encodedValue = EncodedValue.WithDomainSeperator("ic-hashtree-labeled", label.Value, treeHash); break; case HashTreeType.Leaf: EncodedValue leaf = this.AsLeaf(); - encodedValue = EncodedValue.WithDomainSeperator("ic-hashtree-leaf", leaf); + encodedValue = EncodedValue.WithDomainSeperator("ic-hashtree-leaf", leaf.Value); break; case HashTreeType.Pruned: encodedValue = this.AsPruned(); diff --git a/src/Candid/Models/Values/CandidValue.cs b/src/Candid/Models/Values/CandidValue.cs index edb7fa26..843d6e0f 100644 --- a/src/Candid/Models/Values/CandidValue.cs +++ b/src/Candid/Models/Values/CandidValue.cs @@ -232,6 +232,11 @@ public CandidService AsService() /// An optional value public CandidOptional AsOptional() { + if (this.IsNull()) + { + // TODO is this the best way to handle this? + return new CandidOptional(null); + } this.ValidateType(CandidValueType.Optional); return (CandidOptional)this; }