Skip to content

Commit

Permalink
Fixing regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekctek committed Feb 2, 2023
1 parent 9cb916e commit de3b89f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Agent/Models/Certificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Certificate(HashTree tree, byte[] signature, CertificateDelegation? deleg
/// <returns>True if the certificate is valid, otherwise false</returns>
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
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Candid/Mapping/Mappers/OptMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 13 additions & 13 deletions src/Candid/Models/HashTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public EncodedValue AsLeaf()
public byte[] AsPruned()
{
this.ValidateType(HashTreeType.Pruned);
return (EncodedValue)this.value!;
return (byte[])this.value!;
}

/// <summary>
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/Candid/Models/Values/CandidValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public CandidService AsService()
/// <returns>An optional value</returns>
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;
}
Expand Down

0 comments on commit de3b89f

Please sign in to comment.