Skip to content

Commit

Permalink
Merge pull request #28 from 0xsequence/Feature/WaaSIntegration
Browse files Browse the repository at this point in the history
Feature/waas integration
  • Loading branch information
BellringerQuinn authored Dec 18, 2023
2 parents 52d5399 + 7a16823 commit 65d5592
Show file tree
Hide file tree
Showing 184 changed files with 2,657 additions and 287 deletions.
43 changes: 43 additions & 0 deletions Assets/SequenceSDK/Core/Hash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sequence.Extensions;

namespace Sequence.Core
{
public class Hash
{
public static readonly int HashLength = 32;
public byte[] Bytes { get; private set; }

public Hash()
{
Bytes = new byte[HashLength];
}

public Hash(byte[] b)
{
int length = Mathf.Min(HashLength, b.Length);
Bytes = new byte[HashLength];
for (int i = 0; i < length; i++)
{
Bytes[i] = b[i];
}
}

public static implicit operator byte[](Hash hash)
{
return hash.Bytes;
}

public static implicit operator string(Hash hash)
{
return hash.ToString();
}

public override string ToString()
{
return Bytes.ByteArrayToHexStringWithPrefix();
}
}
}
11 changes: 11 additions & 0 deletions Assets/SequenceSDK/Core/Hash.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Assets/SequenceSDK/Core/Network.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Numerics;

namespace Sequence.Core
{
public class NetworkConfig
{
public string Name { get; set; }
public BigInteger ChainID { get; set; }
public string ENSAddress { get; set; }

public string RpcURL { get; set; }
public RPCProvider Provider;

public string RelayerURL { get; set; } // optional, one of the these should be set
public Relayer Relayer { get; set; }

public string IndexerURL { get; set; }

public bool IsDefaultChain { get; set; }
public bool IsAuthChain { get; set; }

public string SequenceAPIURL { get; set; }
}
}
11 changes: 11 additions & 0 deletions Assets/SequenceSDK/Core/Network.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Assets/SequenceSDK/Core/Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Sequence.Provider;
using UnityEngine;

namespace Sequence.Core.Provider
namespace Sequence.Core
{
public struct ProviderOption
{
Expand All @@ -22,7 +22,7 @@ public class RPCProvider
{
// Logger logger;
string nodeURL;
HttpRpcClient httpClient;
IEthClient client;


public RPCProvider(string _nodeURL)
Expand All @@ -33,18 +33,18 @@ public RPCProvider(string _nodeURL)
public RPCProvider(string _nodeURL, ProviderOption options)
{
nodeURL = _nodeURL;
httpClient = new HttpRpcClient(_nodeURL);
client = new SequenceEthClient(_nodeURL);
}

public void SetHTTPClient(HttpRpcClient _httpClient)
public void SetHTTPClient(IEthClient client)
{
httpClient = _httpClient;
this.client = client;
}

public async Task Send(string payload)
public async Task<BigInteger> ChainID()
{
//?
await httpClient.SendRequest(payload);
string chainId = await client.ChainID();
return BigInteger.Parse(chainId);
}

}
Expand Down
20 changes: 20 additions & 0 deletions Assets/SequenceSDK/Core/Relayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Sequence.Transactions;
using Sequence.Core.Wallet;
using System.Numerics;

namespace Sequence.Core
{
public interface Relayer
{
RPCProvider GetProvider();

EthTransaction[] EstimateGasLimits(IWalletConfig walletConfig, WalletContext walletContext, params EthTransaction[] transactions);

BigInteger GetNonce(IWalletConfig walletConfig, WalletContext walletContext, BigInteger space, BigInteger blockNumber);

//(string, EthTransaction, TransactionReceipt) Relay(SignedTransactions signedTransactions);

TransactionReceipt Wait(string transactionID, float maxWaitTime = -1);
}
}
11 changes: 11 additions & 0 deletions Assets/SequenceSDK/Core/Relayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 32 additions & 15 deletions Assets/SequenceSDK/Core/Signature/Digest.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.Linq;
using System.Numerics;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using Sequence.ABI;
using Sequence.Extensions;
using Sequence.Utils;

namespace Sequence.Core.Signature
{
public class Digest
{
public byte[] Hash { get; set; }
public Hash Hash { get; set; }
// Preimage is the preimage of the digest
public byte[] Preimage { get; set; }

Expand All @@ -17,13 +19,30 @@ public static Digest NewDigest(params string[] messages)
byte[] preimage = Encoding.UTF8.GetBytes(string.Join("", messages));
return new Digest
{
Hash = SequenceCoder.KeccakHash(preimage),
Hash = new Hash(SequenceCoder.KeccakHash(preimage)),
Preimage = preimage
};
}

public (ImageHash, Exception) ApprovedImageHash()
{
byte[] approvalSalt = ImageHash.ApprovalSalt.ToByteArray();
int approvalSaltLength = approvalSalt.Length;
if (!Preimage.HasPrefix(approvalSalt) ||
Preimage.Length != approvalSaltLength + Hash.HashLength)
{
return (null, new Exception($"Preimage {Preimage.ByteArrayToHexStringWithPrefix()} of {Hash} is not an image hash approval"));
}

byte[] hashBytes = Preimage.AsSpan(approvalSaltLength).ToArray();
return (new ImageHash()
{
Hash = new Hash(hashBytes),
}, null);
}

// Subdigest derives the hash to be signed by the Sequence wallet's signers to validate the digest.
public Subdigest Subdigest(string walletAddress, params BigInteger[] chainID)
public Subdigest Subdigest(Address wallet, params BigInteger[] chainID)
{
if (chainID.Length == 0 || chainID[0] == null)
{
Expand All @@ -38,22 +57,20 @@ public Subdigest Subdigest(string walletAddress, params BigInteger[] chainID)
byte[] chainIDBytes = chainID[0].ToByteArray();
Array.Reverse(chainIDBytes);

byte[] data = new byte[] { 0x19, 0x01 }
.Concat(chainIDBytes)
.Concat(Encoding.UTF8.GetBytes(walletAddress))
.Concat(this.Hash)
.ToArray();
byte[] data = ByteArrayExtensions.ConcatenateByteArrays(
new byte[] { 0x19, 0x01 },
chainIDBytes,
wallet.Value.ToByteArray(),
Hash);


return new Subdigest
{
Hash = SequenceCoder.ByteArrayToHexString(SequenceCoder.KeccakHash(data)),
Hash = new Hash(SequenceCoder.KeccakHash(data)),
Digest = this,
WalletAddress = walletAddress,
WalletAddress = new Address(wallet),
ChainID = chainID[0]
};
}


}
}
15 changes: 7 additions & 8 deletions Assets/SequenceSDK/Core/Signature/IImageHashable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ public interface IImageHashable
// Used for type safety and preimage recovery.
public class ImageHash
{
public string Hash { get; set; }
// Preimage is the ImageHashable with this ImageHash,
// in go-sequence :
// Preimage ImageHashable
//TODO: If Preimage is set to type IImageHashable, would it be a cyclic definition? Preimage is set to byte[] for now, will modify it accordingly
public byte[] Preimage { get; set; }
public Hash Hash { get; set; }

public static string imageHashApprovalSalt = SequenceCoder.KeccakHash("SetImageHash(bytes32 imageHash)");
// Preimage is the ImageHashable with this ImageHash, null if unknown.
public IImageHashable[] Preimage { get; set; }

public static readonly string ApprovalSalt = SequenceCoder.KeccakHash("SetImageHash(bytes32 imageHash)");

// Approval derives the digest that must be signed to approve the ImageHash for subsequent signatures.
public Digest Approval()
{

return Digest.NewDigest(imageHashApprovalSalt, this.Hash);// Assuming Digest is a valid type and has a constructor accepting the approvalSalt and hashBytes as parameters
return Digest.NewDigest(ApprovalSalt, Hash.ToString());
}
}
}
15 changes: 9 additions & 6 deletions Assets/SequenceSDK/Core/Signature/ISignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@
using System.Numerics;
using UnityEngine;
using Sequence.Core.Wallet;
using Sequence.Core.Provider;
using Sequence.Core;
using System;
using System.Threading.Tasks;
using Sequence.Core.V2;

namespace Sequence.Core.Signature
{
public interface ISignature
{
// Threshold is the minimum signing weight required for a signature to be valid.
int Threshold();
UInt16 Threshold();

// Checkpoint is the nonce of the wallet configuration that the signature applies to.
BigInteger Checkpoint();
UInt32 Checkpoint();

// Recover derives the wallet configuration that the signature applies to.
// Also returns the signature's weight.
// If chainID is not provided, provider must be provided.
// If provider is not provided, EIP-1271 signatures are assumed to be valid.
// If signerSignatures is provided, it will be populated with the valid signer signatures of this signature.
(IWalletConfig, BigInteger) Recover(WalletContext context,
Task<(IWalletConfig, BigInteger)> Recover(WalletContext context,
Digest digest,
Address wallet,
BigInteger chainId,
RPCProvider provider,
List<SignerSignatures> signerSignatures);
params SignerSignatures[] signerSignatures);

// Recover a signature but only using the subdigest
(IWalletConfig, BigInteger) RecoverSubdigest(WalletContext context,
Subdigest subdigest,
RPCProvider provider,
List<SignerSignatures> signerSignatures);
params SignerSignatures[] signerSignatures);

// Reduce returns an equivalent optimized signature.
ISignature Reduce(Subdigest subdigest);
Expand Down
20 changes: 20 additions & 0 deletions Assets/SequenceSDK/Core/Signature/SignatureType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace Sequence.Core.Signature
{
public enum SignatureType : UInt32
{
Legacy = 0,
Regular = 1,
NoChainID = 2,
Chained = 3
}

public static class SignatureTypeExtensions
{
public static byte[] ToByteArray(this SignatureType value)
{
return new byte[] { (byte)value };
}
}
}
11 changes: 11 additions & 0 deletions Assets/SequenceSDK/Core/Signature/SignatureType.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 65d5592

Please sign in to comment.