Skip to content

Commit

Permalink
feat(dotnet): add conversion from Algorand.Unity.Account to `Algora…
Browse files Browse the repository at this point in the history
…nd.Algod.Model.Account`
  • Loading branch information
jasonboukheir committed Nov 25, 2022
1 parent b20a47a commit 9691d0e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Binary file modified .docfx/resources/favicon.ico
Binary file not shown.
43 changes: 25 additions & 18 deletions Runtime/Algorand.Unity.Crypto/Ed25519.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace Algorand.Unity.Crypto
{
public unsafe static class Ed25519
public static class Ed25519
{
public struct KeyPair : INativeDisposable
public readonly struct KeyPair : INativeDisposable
{
public readonly SecretKeyHandle SecretKey;
public readonly PublicKey PublicKey;
Expand Down Expand Up @@ -72,7 +72,7 @@ public static implicit operator SecretKeyHandle(SecureMemoryHandle secureMemoryH
return new SecretKeyHandle() { handle = secureMemoryHandle };
}

public Signature Sign<TMessage>(TMessage message)
public unsafe Signature Sign<TMessage>(TMessage message)
where TMessage : IByteArray
{
var signature = new Signature();
Expand Down Expand Up @@ -138,18 +138,22 @@ public KeyPair ToKeyPair()
{
var pk = new PublicKey();
var sk = SecretKeyHandle.Create();
fixed (Seed* seedPtr = &this)
unsafe
{
int error = crypto_sign_ed25519_seed_keypair(
&pk,
sk.Ptr,
seedPtr
);
if (error > 0)
fixed (Seed* seedPtr = &this)
{
throw new System.Exception($"error code {error} when converting to KeyPair");
int error = crypto_sign_ed25519_seed_keypair(
&pk,
sk.Ptr,
seedPtr
);
if (error > 0)
{
throw new System.Exception($"error code {error} when converting to KeyPair");
}
}
}

return new KeyPair(sk, pk);
}
}
Expand Down Expand Up @@ -233,14 +237,17 @@ public byte this[int index]
public bool Verify<TMessage>(TMessage message, PublicKey pk)
where TMessage : IByteArray
{
fixed (Signature* s = &this)
unsafe
{
var error = crypto_sign_ed25519_verify_detached(
s,
message.GetUnsafePtr(),
(UIntPtr)message.Length,
&pk);
return error == 0;
fixed (Signature* s = &this)
{
var error = crypto_sign_ed25519_verify_detached(
s,
message.GetUnsafePtr(),
(UIntPtr)message.Length,
&pk);
return error == 0;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions Runtime/Algorand.Unity/Accounts/Account.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using Algorand.Unity.LowLevel;
using Cysharp.Threading.Tasks;
using Unity.Collections;

Expand Down Expand Up @@ -109,5 +110,10 @@ public UniTask<SignedTxn<T>[]> SignTxnsAsync<T, TProgress>(T[] txns, TxnIndices
progress.Report(1f);
return SignTxnsAsync(txns, txnsToSign);
}

public static explicit operator Algorand.Algod.Model.Account(Account from)
{
return new Algorand.Algod.Model.Account(from.privateKey.ToArray());
}
}
}

0 comments on commit 9691d0e

Please sign in to comment.