From 0f15282efa0bf339241643cf3fd0b16660d641f1 Mon Sep 17 00:00:00 2001 From: Jordan Andrews Date: Mon, 22 Jun 2020 14:09:52 +1000 Subject: [PATCH 01/14] Test demonstrating ECRECOVER code --- src/NBitcoin/Crypto/ECKey.cs | 8 ++++ src/Stratis.SmartContracts.CLR/EcRecover.cs | 38 ++++++++++++++++ .../ECRecoverTests.cs | 43 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/Stratis.SmartContracts.CLR/EcRecover.cs create mode 100644 src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs diff --git a/src/NBitcoin/Crypto/ECKey.cs b/src/NBitcoin/Crypto/ECKey.cs index 66e08884f0..aeb9d5e1cb 100644 --- a/src/NBitcoin/Crypto/ECKey.cs +++ b/src/NBitcoin/Crypto/ECKey.cs @@ -191,4 +191,12 @@ private static ECPoint DecompressKey(BigInteger xBN, bool yBit) } } + + public static class ECKeyUtils + { + public static PubKey RecoverFromSignature(int recId, ECDSASignature sig, uint256 message, bool compressed) + { + return ECKey.RecoverFromSignature(recId, sig, message, compressed).GetPubKey(compressed); + } + } } diff --git a/src/Stratis.SmartContracts.CLR/EcRecover.cs b/src/Stratis.SmartContracts.CLR/EcRecover.cs new file mode 100644 index 0000000000..1065369b26 --- /dev/null +++ b/src/Stratis.SmartContracts.CLR/EcRecover.cs @@ -0,0 +1,38 @@ +using NBitcoin; +using NBitcoin.Crypto; +using Stratis.SmartContracts.Core.Hashing; + +namespace Stratis.SmartContracts.CLR +{ + /// + /// Holds logic for the equivalent of the ECRECOVER opcode. + /// + /// This is static for now but when we know more about how we are going to use it we will adjust as necessary. + /// + public static class EcRecover + { + // TODO: Not sure what this is yet. + private const int RecId = 0; + + public static ECDSASignature SignMessage(Key privateKey, byte[] message) + { + uint256 hashedUint256 = GetUint256FromMessage(message); + return privateKey.Sign(hashedUint256); + } + + public static Address GetAddressFromSignatureAndMessage(byte[] signature, byte[] message, Network network) + { + // TODO: Error handling for incorrect signature format etc. + + uint256 hashedUint256 = GetUint256FromMessage(message); + ECDSASignature loadedSignature = new ECDSASignature(signature); + PubKey pubKey = ECKeyUtils.RecoverFromSignature(RecId, loadedSignature, hashedUint256, true); + return pubKey.GetAddress(network).ToString().ToAddress(network); + } + + private static uint256 GetUint256FromMessage(byte[] message) + { + return new uint256(HashHelper.Keccak256(message)); + } + } +} diff --git a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs new file mode 100644 index 0000000000..f06ec467f8 --- /dev/null +++ b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs @@ -0,0 +1,43 @@ +using NBitcoin; +using NBitcoin.Crypto; +using Stratis.SmartContracts.CLR; +using Stratis.SmartContracts.Networks; +using Xunit; +using Key = NBitcoin.Key; + +namespace Stratis.SmartContracts.IntegrationTests +{ + public class ECRecoverTests + { + private readonly Network network; + + public ECRecoverTests() + { + this.network = new SmartContractsPoARegTest(); + } + + // 2 things to test: + + // 1) That we have the ECDSA code and can make it available. + + [Fact] + public void CanSignAndRetrieveSender() + { + Key privateKey = new Key(); + Address address = privateKey.PubKey.GetAddress(this.network).ToString().ToAddress(this.network); + + byte[] message = new byte[] { 0x69, 0x76, 0xAA }; + + ECDSASignature offChainSignature = EcRecover.SignMessage(privateKey, message); + + Address recoveredAddress = EcRecover.GetAddressFromSignatureAndMessage(offChainSignature.ToDER(), message, this.network); + + Assert.Equal(address, recoveredAddress); + } + + // 2) That we can enable the method in new contracts without affecting the older contracts + + // TODO + + } +} From 94bcb4ddf6eefc4039f8cdb5fe03541bef3985a4 Mon Sep 17 00:00:00 2001 From: Jordan Andrews Date: Mon, 22 Jun 2020 14:14:45 +1000 Subject: [PATCH 02/14] Formatting --- src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs index f06ec467f8..b63208d344 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs @@ -25,13 +25,15 @@ public void CanSignAndRetrieveSender() { Key privateKey = new Key(); Address address = privateKey.PubKey.GetAddress(this.network).ToString().ToAddress(this.network); - byte[] message = new byte[] { 0x69, 0x76, 0xAA }; + // Sign a message ECDSASignature offChainSignature = EcRecover.SignMessage(privateKey, message); + // Get the address out of the signature Address recoveredAddress = EcRecover.GetAddressFromSignatureAndMessage(offChainSignature.ToDER(), message, this.network); + // Check that the address matches that generated from the private key. Assert.Equal(address, recoveredAddress); } From fdb13a923f88d096f811da3d254896c1939b6a6a Mon Sep 17 00:00:00 2001 From: Jordan Andrews Date: Mon, 22 Jun 2020 14:27:23 +1000 Subject: [PATCH 03/14] 1 instead of 0 --- src/Stratis.SmartContracts.CLR/EcRecover.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Stratis.SmartContracts.CLR/EcRecover.cs b/src/Stratis.SmartContracts.CLR/EcRecover.cs index 1065369b26..d465ddcc60 100644 --- a/src/Stratis.SmartContracts.CLR/EcRecover.cs +++ b/src/Stratis.SmartContracts.CLR/EcRecover.cs @@ -12,7 +12,7 @@ namespace Stratis.SmartContracts.CLR public static class EcRecover { // TODO: Not sure what this is yet. - private const int RecId = 0; + private const int RecId = 1; public static ECDSASignature SignMessage(Key privateKey, byte[] message) { From aa89b62176f049f32b6075cdd183679c70a19930 Mon Sep 17 00:00:00 2001 From: Jordan Andrews Date: Tue, 23 Jun 2020 15:41:10 +1000 Subject: [PATCH 04/14] Code mostly complete just need to fix tests --- ...tcoin.Features.SmartContracts.Tests.csproj | 2 +- .../SmartContractFeature.cs | 1 + ...tis.Bitcoin.Features.SmartContracts.csproj | 2 +- .../AuctionTests.cs | 6 ++++- .../ContractLogHolderTests.cs | 4 ++-- ...ratis.SmartContracts.CLR.Validation.csproj | 2 +- .../{EcRecover.cs => EcRecoverProvider.cs} | 23 ++++++++++++------- .../SmartContractState.cs | 5 +++- .../SmartContractStateFactory.cs | 9 ++++++-- 9 files changed, 37 insertions(+), 17 deletions(-) rename src/Stratis.SmartContracts.CLR/{EcRecover.cs => EcRecoverProvider.cs} (76%) diff --git a/src/Stratis.Bitcoin.Features.SmartContracts.Tests/Stratis.Bitcoin.Features.SmartContracts.Tests.csproj b/src/Stratis.Bitcoin.Features.SmartContracts.Tests/Stratis.Bitcoin.Features.SmartContracts.Tests.csproj index f276479751..5f8cf0afc3 100644 --- a/src/Stratis.Bitcoin.Features.SmartContracts.Tests/Stratis.Bitcoin.Features.SmartContracts.Tests.csproj +++ b/src/Stratis.Bitcoin.Features.SmartContracts.Tests/Stratis.Bitcoin.Features.SmartContracts.Tests.csproj @@ -18,7 +18,7 @@ - + all runtime; build; native; contentfiles; analyzers diff --git a/src/Stratis.Bitcoin.Features.SmartContracts/SmartContractFeature.cs b/src/Stratis.Bitcoin.Features.SmartContracts/SmartContractFeature.cs index e755106ed9..6cddf8fce3 100644 --- a/src/Stratis.Bitcoin.Features.SmartContracts/SmartContractFeature.cs +++ b/src/Stratis.Bitcoin.Features.SmartContracts/SmartContractFeature.cs @@ -115,6 +115,7 @@ public static IFullNodeBuilder AddSmartContracts(this IFullNodeBuilder fullNodeB services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Stratis.Bitcoin.Features.SmartContracts/Stratis.Bitcoin.Features.SmartContracts.csproj b/src/Stratis.Bitcoin.Features.SmartContracts/Stratis.Bitcoin.Features.SmartContracts.csproj index 8a6aa09fdf..6cf2f124a9 100644 --- a/src/Stratis.Bitcoin.Features.SmartContracts/Stratis.Bitcoin.Features.SmartContracts.csproj +++ b/src/Stratis.Bitcoin.Features.SmartContracts/Stratis.Bitcoin.Features.SmartContracts.csproj @@ -11,7 +11,7 @@ - + all runtime; build; native; contentfiles; analyzers diff --git a/src/Stratis.SmartContracts.CLR.Tests/AuctionTests.cs b/src/Stratis.SmartContracts.CLR.Tests/AuctionTests.cs index 292c74f786..5076449a9e 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/AuctionTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/AuctionTests.cs @@ -44,6 +44,7 @@ public AuctionTests() null, getBalance, null, + null, null ); } @@ -88,7 +89,8 @@ public TestSmartContractState( IInternalTransactionExecutor transactionExecutor, Func getBalance, IInternalHashHelper hashHelper, - IContractLogger contractLogger) + IContractLogger contractLogger, + IEcRecoverProvider ecRecoverProvider) { this.Block = block; this.Message = message; @@ -99,6 +101,7 @@ public TestSmartContractState( this.GetBalance = getBalance; this.InternalHashHelper = hashHelper; this.ContractLogger = contractLogger; + this.EcRecoverProvider = ecRecoverProvider; } public IBlock Block { get; } @@ -110,6 +113,7 @@ public TestSmartContractState( public Func GetBalance { get; } public IInternalHashHelper InternalHashHelper { get; } public IContractLogger ContractLogger { get; } + public IEcRecoverProvider EcRecoverProvider { get; } } public class TestBlock : IBlock diff --git a/src/Stratis.SmartContracts.CLR.Tests/ContractLogHolderTests.cs b/src/Stratis.SmartContracts.CLR.Tests/ContractLogHolderTests.cs index a76d10c283..d4dfdd6af1 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/ContractLogHolderTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/ContractLogHolderTests.cs @@ -29,11 +29,11 @@ public void Store_Structs_And_Get_Logs() var contractAddress1 = new uint160(1); var contractAddress2 = new uint160(2); - var state1 = new TestSmartContractState(null, new TestMessage { ContractAddress = contractAddress1.ToAddress() }, null, null, null, null, null, null, null); + var state1 = new TestSmartContractState(null, new TestMessage { ContractAddress = contractAddress1.ToAddress() }, null, null, null, null, null, null, null, null); var log1 = new Example1("Jordan", 12345); var log2 = new Example1("John", 123); - var state2 = new TestSmartContractState(null, new TestMessage { ContractAddress = contractAddress2.ToAddress() }, null, null, null, null, null, null, null); + var state2 = new TestSmartContractState(null, new TestMessage { ContractAddress = contractAddress2.ToAddress() }, null, null, null, null, null, null, null, null); var log3 = new Example2("0x95D34980095380851902ccd9A1Fb4C813C2cb639".HexToAddress(), 16, "This is a test message."); this.logHolder.Log(state1, log1); diff --git a/src/Stratis.SmartContracts.CLR.Validation/Stratis.SmartContracts.CLR.Validation.csproj b/src/Stratis.SmartContracts.CLR.Validation/Stratis.SmartContracts.CLR.Validation.csproj index dcbf471909..535951187d 100644 --- a/src/Stratis.SmartContracts.CLR.Validation/Stratis.SmartContracts.CLR.Validation.csproj +++ b/src/Stratis.SmartContracts.CLR.Validation/Stratis.SmartContracts.CLR.Validation.csproj @@ -9,7 +9,7 @@ - + all diff --git a/src/Stratis.SmartContracts.CLR/EcRecover.cs b/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs similarity index 76% rename from src/Stratis.SmartContracts.CLR/EcRecover.cs rename to src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs index d465ddcc60..0b7dbf9145 100644 --- a/src/Stratis.SmartContracts.CLR/EcRecover.cs +++ b/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs @@ -9,30 +9,37 @@ namespace Stratis.SmartContracts.CLR /// /// This is static for now but when we know more about how we are going to use it we will adjust as necessary. /// - public static class EcRecover + public class EcRecoverProvider : IEcRecoverProvider { // TODO: Not sure what this is yet. private const int RecId = 1; - public static ECDSASignature SignMessage(Key privateKey, byte[] message) + private readonly Network network; + + public EcRecoverProvider(Network network) { - uint256 hashedUint256 = GetUint256FromMessage(message); - return privateKey.Sign(hashedUint256); + this.network = network; + } + + private static uint256 GetUint256FromMessage(byte[] message) + { + return new uint256(HashHelper.Keccak256(message)); } - public static Address GetAddressFromSignatureAndMessage(byte[] signature, byte[] message, Network network) + public Address GetSigner(byte[] message, byte[] signature) { // TODO: Error handling for incorrect signature format etc. uint256 hashedUint256 = GetUint256FromMessage(message); ECDSASignature loadedSignature = new ECDSASignature(signature); PubKey pubKey = ECKeyUtils.RecoverFromSignature(RecId, loadedSignature, hashedUint256, true); - return pubKey.GetAddress(network).ToString().ToAddress(network); + return pubKey.GetAddress(this.network).ToString().ToAddress(this.network); } - private static uint256 GetUint256FromMessage(byte[] message) + public static ECDSASignature SignMessage(Key privateKey, byte[] message) { - return new uint256(HashHelper.Keccak256(message)); + uint256 hashedUint256 = GetUint256FromMessage(message); + return privateKey.Sign(hashedUint256); } } } diff --git a/src/Stratis.SmartContracts.CLR/SmartContractState.cs b/src/Stratis.SmartContracts.CLR/SmartContractState.cs index 4770fd6792..dc1b35bc80 100644 --- a/src/Stratis.SmartContracts.CLR/SmartContractState.cs +++ b/src/Stratis.SmartContracts.CLR/SmartContractState.cs @@ -15,7 +15,8 @@ public SmartContractState( IContractLogger contractLogger, IInternalTransactionExecutor internalTransactionExecutor, IInternalHashHelper internalHashHelper, - Func getBalance) + Func getBalance, + IEcRecoverProvider ecRecoverProvider) { this.Block = block; this.Message = message; @@ -25,6 +26,7 @@ public SmartContractState( this.InternalTransactionExecutor = internalTransactionExecutor; this.InternalHashHelper = internalHashHelper; this.GetBalance = getBalance; + this.EcRecoverProvider = ecRecoverProvider; } public IBlock Block { get; } @@ -34,6 +36,7 @@ public SmartContractState( public IPersistentState PersistentState { get; } public ISerializer Serializer { get; } + public IEcRecoverProvider EcRecoverProvider { get; } public IContractLogger ContractLogger { get; } diff --git a/src/Stratis.SmartContracts.CLR/SmartContractStateFactory.cs b/src/Stratis.SmartContracts.CLR/SmartContractStateFactory.cs index 59f6eb6de7..74d35eaa43 100644 --- a/src/Stratis.SmartContracts.CLR/SmartContractStateFactory.cs +++ b/src/Stratis.SmartContracts.CLR/SmartContractStateFactory.cs @@ -9,13 +9,17 @@ public class SmartContractStateFactory : ISmartContractStateFactory { private readonly ISerializer serializer; + private readonly IEcRecoverProvider ecRecoverProvider; + public SmartContractStateFactory(IContractPrimitiveSerializer primitiveSerializer, IInternalExecutorFactory internalTransactionExecutorFactory, - ISerializer serializer) + ISerializer serializer, + IEcRecoverProvider ecRecoverProvider) { this.serializer = serializer; this.PrimitiveSerializer = primitiveSerializer; this.InternalTransactionExecutorFactory = internalTransactionExecutorFactory; + this.ecRecoverProvider = ecRecoverProvider; } public IContractPrimitiveSerializer PrimitiveSerializer { get; } @@ -44,7 +48,8 @@ public ISmartContractState Create(IState state, RuntimeObserver.IGasMeter gasMet contractLogger, this.InternalTransactionExecutorFactory.Create(gasMeter, state), new InternalHashHelper(), - () => state.GetBalance(address)); + () => state.GetBalance(address), + this.ecRecoverProvider); return contractState; } From 0738617439a08d54eda1551c183af52d4c356dfe Mon Sep 17 00:00:00 2001 From: Jordan Andrews Date: Mon, 6 Jul 2020 11:42:30 +1000 Subject: [PATCH 05/14] Soln building --- .../ContractExecutorTestContext.cs | 4 +++- .../ContractExecutorTests.cs | 4 +++- .../ObserverInstanceRewriterTests.cs | 3 ++- src/Stratis.SmartContracts.CLR.Tests/ObserverTests.cs | 3 ++- .../ReflectionVirtualMachineTests.cs | 4 ++-- .../ECRecoverTests.cs | 6 ++++-- .../PoW/SmartContractMinerTests.cs | 4 +++- 7 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTestContext.cs b/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTestContext.cs index 6303b34cea..2fdabb83d4 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTestContext.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTestContext.cs @@ -31,6 +31,7 @@ public class ContractExecutorTestContext public IContractAssemblyCache ContractCache { get; } public ReflectionVirtualMachine Vm { get; } public ISmartContractStateFactory SmartContractStateFactory { get; } + public IEcRecoverProvider EcRecoverProvider { get; } public StateProcessor StateProcessor { get; } public Serializer Serializer { get; } @@ -51,7 +52,8 @@ public ContractExecutorTestContext() this.Vm = new ReflectionVirtualMachine(this.Validator, this.LoggerFactory, this.AssemblyLoader, this.ModuleDefinitionReader, this.ContractCache); this.StateProcessor = new StateProcessor(this.Vm, this.AddressGenerator); this.InternalTxExecutorFactory = new InternalExecutorFactory(this.LoggerFactory, this.StateProcessor); - this.SmartContractStateFactory = new SmartContractStateFactory(this.ContractPrimitiveSerializer, this.InternalTxExecutorFactory, this.Serializer); + this.EcRecoverProvider = new EcRecoverProvider(this.Network); + this.SmartContractStateFactory = new SmartContractStateFactory(this.ContractPrimitiveSerializer, this.InternalTxExecutorFactory, this.Serializer, this.EcRecoverProvider); } } } diff --git a/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs b/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs index 38d443589e..6ef54fd2e2 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs @@ -41,6 +41,7 @@ public sealed class ContractExecutorTests private readonly IStateProcessor stateProcessor; private readonly ISmartContractStateFactory smartContractStateFactory; private readonly ISerializer serializer; + private readonly IEcRecoverProvider ecRecoverProvider; public ContractExecutorTests() { @@ -61,7 +62,8 @@ public ContractExecutorTests() this.vm = new ReflectionVirtualMachine(this.validator, this.loggerFactory, this.assemblyLoader, this.moduleDefinitionReader, this.contractCache); this.stateProcessor = new StateProcessor(this.vm, this.addressGenerator); this.internalTxExecutorFactory = new InternalExecutorFactory(this.loggerFactory, this.stateProcessor); - this.smartContractStateFactory = new SmartContractStateFactory(this.contractPrimitiveSerializer, this.internalTxExecutorFactory, this.serializer); + this.ecRecoverProvider = new EcRecoverProvider(this.network); + this.smartContractStateFactory = new SmartContractStateFactory(this.contractPrimitiveSerializer, this.internalTxExecutorFactory, this.serializer, this.ecRecoverProvider); this.callDataSerializer = new CallDataSerializer(this.contractPrimitiveSerializer); diff --git a/src/Stratis.SmartContracts.CLR.Tests/ObserverInstanceRewriterTests.cs b/src/Stratis.SmartContracts.CLR.Tests/ObserverInstanceRewriterTests.cs index b43b4ccb6c..c62ae9aafc 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/ObserverInstanceRewriterTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/ObserverInstanceRewriterTests.cs @@ -128,7 +128,8 @@ public ObserverInstanceRewriterTests() new ContractLogHolder(), Mock.Of(), new InternalHashHelper(), - () => 1000); + () => 1000, + Mock.Of()); this.rewriter = new ObserverInstanceRewriter(); } diff --git a/src/Stratis.SmartContracts.CLR.Tests/ObserverTests.cs b/src/Stratis.SmartContracts.CLR.Tests/ObserverTests.cs index 9e4896992a..7b194e7b43 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/ObserverTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/ObserverTests.cs @@ -127,7 +127,8 @@ public ObserverTests() new ContractLogHolder(), Mock.Of(), new InternalHashHelper(), - () => 1000); + () => 1000, + Mock.Of()); this.rewriter = new ObserverRewriter(new Observer(this.gasMeter, new MemoryMeter(ReflectionVirtualMachine.MemoryUnitLimit))); } diff --git a/src/Stratis.SmartContracts.CLR.Tests/ReflectionVirtualMachineTests.cs b/src/Stratis.SmartContracts.CLR.Tests/ReflectionVirtualMachineTests.cs index dbd44c02b2..cfd83ba90b 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/ReflectionVirtualMachineTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/ReflectionVirtualMachineTests.cs @@ -1,7 +1,6 @@ using System; using System.Reflection; using System.Text; -using CSharpFunctionalExtensions; using Moq; using NBitcoin; using Stratis.SmartContracts.CLR.Caching; @@ -46,7 +45,8 @@ public ReflectionVirtualMachineTests() new ContractLogHolder(), Mock.Of(), new InternalHashHelper(), - () => 1000); + () => 1000, + Mock.Of()); this.gasMeter = new GasMeter((RuntimeObserver.Gas)50_000); } diff --git a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs index b63208d344..32e4e84645 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs @@ -10,10 +10,12 @@ namespace Stratis.SmartContracts.IntegrationTests public class ECRecoverTests { private readonly Network network; + private readonly EcRecoverProvider ecRecover; public ECRecoverTests() { this.network = new SmartContractsPoARegTest(); + this.ecRecover = new EcRecoverProvider(this.network); } // 2 things to test: @@ -28,10 +30,10 @@ public void CanSignAndRetrieveSender() byte[] message = new byte[] { 0x69, 0x76, 0xAA }; // Sign a message - ECDSASignature offChainSignature = EcRecover.SignMessage(privateKey, message); + ECDSASignature offChainSignature = EcRecoverProvider.SignMessage(privateKey, message); // Get the address out of the signature - Address recoveredAddress = EcRecover.GetAddressFromSignatureAndMessage(offChainSignature.ToDER(), message, this.network); + Address recoveredAddress = this.ecRecover.GetSigner(offChainSignature.ToDER(), message); // Check that the address matches that generated from the private key. Assert.Equal(address, recoveredAddress); diff --git a/src/Stratis.SmartContracts.IntegrationTests/PoW/SmartContractMinerTests.cs b/src/Stratis.SmartContracts.IntegrationTests/PoW/SmartContractMinerTests.cs index d9a9301681..62c7d0c6ab 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/PoW/SmartContractMinerTests.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/PoW/SmartContractMinerTests.cs @@ -177,6 +177,7 @@ public class TestContext private StateProcessor stateProcessor; private SmartContractStateFactory smartContractStateFactory; public IBlockExecutionResultCache executionCache; + private IEcRecoverProvider ecRecoverProvider; public SmartContractPowConsensusFactory ConsensusFactory { get; private set; } #endregion @@ -346,7 +347,8 @@ private void InitializeSmartContractComponents(string callingMethod) this.internalTxExecutorFactory = new InternalExecutorFactory(this.loggerFactory, this.stateProcessor); this.primitiveSerializer = new ContractPrimitiveSerializer(this.network); this.serializer = new Serializer(this.primitiveSerializer); - this.smartContractStateFactory = new SmartContractStateFactory(this.primitiveSerializer, this.internalTxExecutorFactory, this.serializer); + this.ecRecoverProvider = new EcRecoverProvider(this.network); + this.smartContractStateFactory = new SmartContractStateFactory(this.primitiveSerializer, this.internalTxExecutorFactory, this.serializer, this.ecRecoverProvider); this.stateFactory = new StateFactory(this.smartContractStateFactory); this.ExecutorFactory = new ReflectionExecutorFactory(this.loggerFactory, this.callDataSerializer, this.refundProcessor, this.transferProcessor, this.stateFactory, this.stateProcessor, this.primitiveSerializer); } From b1c9ec874a82dbe8041b775cdbfd551a95742f6a Mon Sep 17 00:00:00 2001 From: Jordan Andrews Date: Wed, 8 Jul 2020 13:53:28 +1000 Subject: [PATCH 06/14] ECRecover Progress --- .../ECRecoverTests.cs | 2 +- .../SmartContracts/EcRecoverContract.cs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/Stratis.SmartContracts.IntegrationTests/SmartContracts/EcRecoverContract.cs diff --git a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs index 32e4e84645..e1aa67bd67 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs @@ -33,7 +33,7 @@ public void CanSignAndRetrieveSender() ECDSASignature offChainSignature = EcRecoverProvider.SignMessage(privateKey, message); // Get the address out of the signature - Address recoveredAddress = this.ecRecover.GetSigner(offChainSignature.ToDER(), message); + Address recoveredAddress = this.ecRecover.GetSigner(message, offChainSignature.ToDER()); // Check that the address matches that generated from the private key. Assert.Equal(address, recoveredAddress); diff --git a/src/Stratis.SmartContracts.IntegrationTests/SmartContracts/EcRecoverContract.cs b/src/Stratis.SmartContracts.IntegrationTests/SmartContracts/EcRecoverContract.cs new file mode 100644 index 0000000000..4c58fb2beb --- /dev/null +++ b/src/Stratis.SmartContracts.IntegrationTests/SmartContracts/EcRecoverContract.cs @@ -0,0 +1,30 @@ + +using Stratis.SmartContracts; + +public class EcRecoverContract : SmartContract +{ + public Address ThirdPartySigner + { + get + { + return this.PersistentState.GetAddress(nameof(this.ThirdPartySigner)); + } + set + { + this.PersistentState.SetAddress(nameof(this.ThirdPartySigner), value); + } + } + + public EcRecoverContract(ISmartContractState state, Address thirdPartySigner) : base(state) + { + this.ThirdPartySigner = thirdPartySigner; + } + + public void CheckThirdPartySignature() + { + byte[] message = new byte[] { 0, 1, 2, 3 }; + Address signerOfMessage = this.EcRecover() + } + + +} From 09de8df0a854289b881ce865886c042d1d2df466 Mon Sep 17 00:00:00 2001 From: Kevin Loubser Date: Fri, 7 Aug 2020 07:21:45 +0200 Subject: [PATCH 07/14] WIP --- .../ECRecoverTests.cs | 140 +++++++++++++++--- .../SmartContracts/EcRecoverContract.cs | 8 +- ...tis.SmartContracts.IntegrationTests.csproj | 3 + 3 files changed, 126 insertions(+), 25 deletions(-) diff --git a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs index e1aa67bd67..2dd4769741 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs @@ -1,7 +1,12 @@ -using NBitcoin; +using System; +using System.Threading.Tasks; using NBitcoin.Crypto; +using Stratis.Bitcoin.Features.SmartContracts.Models; using Stratis.SmartContracts.CLR; -using Stratis.SmartContracts.Networks; +using Stratis.SmartContracts.CLR.Compilation; +using Stratis.SmartContracts.CLR.Serialization; +using Stratis.SmartContracts.Core; +using Stratis.SmartContracts.Tests.Common.MockChain; using Xunit; using Key = NBitcoin.Key; @@ -9,39 +14,134 @@ namespace Stratis.SmartContracts.IntegrationTests { public class ECRecoverTests { - private readonly Network network; - private readonly EcRecoverProvider ecRecover; + // 2 things to test: + + // 1) That we have the ECDSA code and can make it available. - public ECRecoverTests() + [Fact] + public void CanSignAndRetrieveSender() { - this.network = new SmartContractsPoARegTest(); - this.ecRecover = new EcRecoverProvider(this.network); + using (PoWMockChain chain = new PoWMockChain(1)) + { + var network = chain.Nodes[0].CoreNode.FullNode.Network; + var privateKey = new Key(); + Address address = privateKey.PubKey.GetAddress(network).ToString().ToAddress(network); + byte[] message = new byte[] {0x69, 0x76, 0xAA}; + + // Sign a message + ECDSASignature offChainSignature = EcRecoverProvider.SignMessage(privateKey, message); + + var ecRecover = new EcRecoverProvider(network); + // Get the address out of the signature + Address recoveredAddress = ecRecover.GetSigner(message, offChainSignature.ToDER()); + + // Check that the address matches that generated from the private key. + Assert.Equal(address, recoveredAddress); + } } - // 2 things to test: + [Fact] + public async Task CanCallEcRecoverContractWithValidSignatureAsync() + { + using (PoWMockChain chain = new PoWMockChain(2)) + { + var node1 = chain.Nodes[0]; - // 1) That we have the ECDSA code and can make it available. + node1.MineBlocks(1); + + var network = chain.Nodes[0].CoreNode.FullNode.Network; + + var privateKey = new Key(); + string address = privateKey.PubKey.GetAddress(network).ToString(); + byte[] message = new byte[] { 0x69, 0x76, 0xAA }; + byte[] signature = EcRecoverProvider.SignMessage(privateKey, message).ToDER(); + + // TODO: If the incorrect parameters are passed to the constructor, the contract does not get properly created ('Method does not exist on contract'), but a success response is still returned? + + byte[] contract = ContractCompiler.CompileFile("SmartContracts/EcRecoverContract.cs").Compilation; + string[] createParameters = new string[] { string.Format("{0}#{1}", (int)MethodParameterDataType.Address, address) }; + BuildCreateContractTransactionResponse createResult = node1.SendCreateContractTransaction(contract, 1, createParameters); + + Assert.NotNull(createResult); + Assert.True(createResult.Success); + + node1.WaitMempoolCount(1); + node1.MineBlocks(1); + + string[] callParameters = new string[] + { + string.Format("{0}#{1}", (int)MethodParameterDataType.ByteArray, message.ToHexString()), + string.Format("{0}#{1}", (int)MethodParameterDataType.ByteArray, signature.ToHexString()) + }; + + BuildCallContractTransactionResponse response = node1.SendCallContractTransaction("CheckThirdPartySignature", createResult.NewContractAddress, 1, callParameters); + Assert.NotNull(response); + Assert.True(response.Success); + + node1.WaitMempoolCount(1); + node1.MineBlocks(1); + + ReceiptResponse receipt = node1.GetReceipt(response.TransactionId.ToString()); + + Assert.NotNull(receipt); + Assert.True(receipt.Success); + Assert.Equal("True", receipt.ReturnValue); + } + } [Fact] - public void CanSignAndRetrieveSender() + public async Task CanCallEcRecoverContractWithInvalidSignatureAsync() { - Key privateKey = new Key(); - Address address = privateKey.PubKey.GetAddress(this.network).ToString().ToAddress(this.network); - byte[] message = new byte[] { 0x69, 0x76, 0xAA }; + using (PoWMockChain chain = new PoWMockChain(2)) + { + var node1 = chain.Nodes[0]; + + node1.MineBlocks(1); + + var network = chain.Nodes[0].CoreNode.FullNode.Network; - // Sign a message - ECDSASignature offChainSignature = EcRecoverProvider.SignMessage(privateKey, message); + var privateKey = new Key(); + string address = privateKey.PubKey.GetAddress(network).ToString(); + byte[] message = new byte[] { 0x69, 0x76, 0xAA }; + + // Make the signature with a key unrelated to the third party signer for the contract. + byte[] signature = EcRecoverProvider.SignMessage(new Key(), message).ToDER(); - // Get the address out of the signature - Address recoveredAddress = this.ecRecover.GetSigner(message, offChainSignature.ToDER()); + // TODO: If the incorrect parameters are passed to the constructor, the contract does not get properly created ('Method does not exist on contract'), but a success response is still returned? - // Check that the address matches that generated from the private key. - Assert.Equal(address, recoveredAddress); + byte[] contract = ContractCompiler.CompileFile("SmartContracts/EcRecoverContract.cs").Compilation; + string[] createParameters = new string[] { string.Format("{0}#{1}", (int)MethodParameterDataType.Address, address) }; + BuildCreateContractTransactionResponse createResult = node1.SendCreateContractTransaction(contract, 1, createParameters); + + Assert.NotNull(createResult); + Assert.True(createResult.Success); + + node1.WaitMempoolCount(1); + node1.MineBlocks(1); + + string[] callParameters = new string[] + { + string.Format("{0}#{1}", (int)MethodParameterDataType.ByteArray, message.ToHexString()), + string.Format("{0}#{1}", (int)MethodParameterDataType.ByteArray, signature.ToHexString()) + }; + + BuildCallContractTransactionResponse response = node1.SendCallContractTransaction("CheckThirdPartySignature", createResult.NewContractAddress, 1, callParameters); + Assert.NotNull(response); + Assert.True(response.Success); + + node1.WaitMempoolCount(1); + node1.MineBlocks(1); + + ReceiptResponse receipt = node1.GetReceipt(response.TransactionId.ToString()); + + Assert.NotNull(receipt); + Assert.True(receipt.Success); + Assert.Equal("False", receipt.ReturnValue); + } } // 2) That we can enable the method in new contracts without affecting the older contracts // TODO - } } diff --git a/src/Stratis.SmartContracts.IntegrationTests/SmartContracts/EcRecoverContract.cs b/src/Stratis.SmartContracts.IntegrationTests/SmartContracts/EcRecoverContract.cs index 4c58fb2beb..ecde22a2b4 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/SmartContracts/EcRecoverContract.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/SmartContracts/EcRecoverContract.cs @@ -20,11 +20,9 @@ public EcRecoverContract(ISmartContractState state, Address thirdPartySigner) : this.ThirdPartySigner = thirdPartySigner; } - public void CheckThirdPartySignature() + public bool CheckThirdPartySignature(byte[] message, byte[] signature) { - byte[] message = new byte[] { 0, 1, 2, 3 }; - Address signerOfMessage = this.EcRecover() + Address signerOfMessage = this.EcRecover(message, signature); + return (signerOfMessage == this.ThirdPartySigner); } - - } diff --git a/src/Stratis.SmartContracts.IntegrationTests/Stratis.SmartContracts.IntegrationTests.csproj b/src/Stratis.SmartContracts.IntegrationTests/Stratis.SmartContracts.IntegrationTests.csproj index e20aefcadd..d7d0a9dc35 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/Stratis.SmartContracts.IntegrationTests.csproj +++ b/src/Stratis.SmartContracts.IntegrationTests/Stratis.SmartContracts.IntegrationTests.csproj @@ -90,6 +90,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest From 36305e5e0257d22d0dbbab91a7691d5a600e896a Mon Sep 17 00:00:00 2001 From: Kevin Loubser Date: Mon, 10 Aug 2020 09:49:05 +0200 Subject: [PATCH 08/14] Use augmented signature format for recId --- .../EcRecoverProvider.cs | 24 +++++++++++++------ .../ECRecoverTests.cs | 16 ++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs b/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs index 0b7dbf9145..3e51924207 100644 --- a/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs +++ b/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs @@ -11,9 +11,6 @@ namespace Stratis.SmartContracts.CLR /// public class EcRecoverProvider : IEcRecoverProvider { - // TODO: Not sure what this is yet. - private const int RecId = 1; - private readonly Network network; public EcRecoverProvider(Network network) @@ -26,20 +23,33 @@ private static uint256 GetUint256FromMessage(byte[] message) return new uint256(HashHelper.Keccak256(message)); } + /// + /// Retrieves the base58 address of the signer of an ECDSA signature. + /// + /// + /// The ECDSA signature prepended with header information specifying the correct value of recId. + /// The base58 address for the signer of a signature. public Address GetSigner(byte[] message, byte[] signature) { // TODO: Error handling for incorrect signature format etc. uint256 hashedUint256 = GetUint256FromMessage(message); - ECDSASignature loadedSignature = new ECDSASignature(signature); - PubKey pubKey = ECKeyUtils.RecoverFromSignature(RecId, loadedSignature, hashedUint256, true); + PubKey pubKey = PubKey.RecoverCompact(hashedUint256, signature); + return pubKey.GetAddress(this.network).ToString().ToAddress(this.network); } - public static ECDSASignature SignMessage(Key privateKey, byte[] message) + /// + /// Signs a message, returning an ECDSA signature. + /// + /// The private key used to sign the message. + /// The complete message to be signed. + /// The ECDSA signature prepended with header information specifying the correct value of recId. + public static byte[] SignMessage(Key privateKey, byte[] message) { uint256 hashedUint256 = GetUint256FromMessage(message); - return privateKey.Sign(hashedUint256); + + return privateKey.SignCompact(hashedUint256); } } } diff --git a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs index 2dd4769741..1a4e511742 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs @@ -1,6 +1,4 @@ -using System; -using System.Threading.Tasks; -using NBitcoin.Crypto; +using System.Threading.Tasks; using Stratis.Bitcoin.Features.SmartContracts.Models; using Stratis.SmartContracts.CLR; using Stratis.SmartContracts.CLR.Compilation; @@ -29,11 +27,11 @@ public void CanSignAndRetrieveSender() byte[] message = new byte[] {0x69, 0x76, 0xAA}; // Sign a message - ECDSASignature offChainSignature = EcRecoverProvider.SignMessage(privateKey, message); + byte[] offChainSignature = EcRecoverProvider.SignMessage(privateKey, message); var ecRecover = new EcRecoverProvider(network); // Get the address out of the signature - Address recoveredAddress = ecRecover.GetSigner(message, offChainSignature.ToDER()); + Address recoveredAddress = ecRecover.GetSigner(message, offChainSignature); // Check that the address matches that generated from the private key. Assert.Equal(address, recoveredAddress); @@ -41,7 +39,7 @@ public void CanSignAndRetrieveSender() } [Fact] - public async Task CanCallEcRecoverContractWithValidSignatureAsync() + public void CanCallEcRecoverContractWithValidSignatureAsync() { using (PoWMockChain chain = new PoWMockChain(2)) { @@ -54,7 +52,7 @@ public async Task CanCallEcRecoverContractWithValidSignatureAsync() var privateKey = new Key(); string address = privateKey.PubKey.GetAddress(network).ToString(); byte[] message = new byte[] { 0x69, 0x76, 0xAA }; - byte[] signature = EcRecoverProvider.SignMessage(privateKey, message).ToDER(); + byte[] signature = EcRecoverProvider.SignMessage(privateKey, message); // TODO: If the incorrect parameters are passed to the constructor, the contract does not get properly created ('Method does not exist on contract'), but a success response is still returned? @@ -90,7 +88,7 @@ public async Task CanCallEcRecoverContractWithValidSignatureAsync() } [Fact] - public async Task CanCallEcRecoverContractWithInvalidSignatureAsync() + public void CanCallEcRecoverContractWithInvalidSignatureAsync() { using (PoWMockChain chain = new PoWMockChain(2)) { @@ -105,7 +103,7 @@ public async Task CanCallEcRecoverContractWithInvalidSignatureAsync() byte[] message = new byte[] { 0x69, 0x76, 0xAA }; // Make the signature with a key unrelated to the third party signer for the contract. - byte[] signature = EcRecoverProvider.SignMessage(new Key(), message).ToDER(); + byte[] signature = EcRecoverProvider.SignMessage(new Key(), message); // TODO: If the incorrect parameters are passed to the constructor, the contract does not get properly created ('Method does not exist on contract'), but a success response is still returned? From bbe42884586ab40afc4992c2676c924c6ca77cf0 Mon Sep 17 00:00:00 2001 From: YakupIpek Date: Thu, 11 Feb 2021 12:49:29 +0300 Subject: [PATCH 09/14] Version upgraded --- .../Stratis.Bitcoin.Features.SmartContracts.Tests.csproj | 2 +- .../Stratis.Bitcoin.Features.SmartContracts.csproj | 3 +-- .../Stratis.SmartContracts.CLR.Validation.csproj | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Stratis.Bitcoin.Features.SmartContracts.Tests/Stratis.Bitcoin.Features.SmartContracts.Tests.csproj b/src/Stratis.Bitcoin.Features.SmartContracts.Tests/Stratis.Bitcoin.Features.SmartContracts.Tests.csproj index 9dbe0ea401..eced4a05ee 100644 --- a/src/Stratis.Bitcoin.Features.SmartContracts.Tests/Stratis.Bitcoin.Features.SmartContracts.Tests.csproj +++ b/src/Stratis.Bitcoin.Features.SmartContracts.Tests/Stratis.Bitcoin.Features.SmartContracts.Tests.csproj @@ -18,7 +18,7 @@ - + all runtime; build; native; contentfiles; analyzers diff --git a/src/Stratis.Bitcoin.Features.SmartContracts/Stratis.Bitcoin.Features.SmartContracts.csproj b/src/Stratis.Bitcoin.Features.SmartContracts/Stratis.Bitcoin.Features.SmartContracts.csproj index 4599fb20d4..8621204006 100644 --- a/src/Stratis.Bitcoin.Features.SmartContracts/Stratis.Bitcoin.Features.SmartContracts.csproj +++ b/src/Stratis.Bitcoin.Features.SmartContracts/Stratis.Bitcoin.Features.SmartContracts.csproj @@ -14,7 +14,7 @@ - + @@ -24,5 +24,4 @@ - diff --git a/src/Stratis.SmartContracts.CLR.Validation/Stratis.SmartContracts.CLR.Validation.csproj b/src/Stratis.SmartContracts.CLR.Validation/Stratis.SmartContracts.CLR.Validation.csproj index a1640e41ac..f348678c6d 100644 --- a/src/Stratis.SmartContracts.CLR.Validation/Stratis.SmartContracts.CLR.Validation.csproj +++ b/src/Stratis.SmartContracts.CLR.Validation/Stratis.SmartContracts.CLR.Validation.csproj @@ -9,7 +9,7 @@ - + From aba3b966db0d567e44926c7691421135edeafb05 Mon Sep 17 00:00:00 2001 From: Francois de la Rouviere Date: Mon, 15 Feb 2021 10:04:55 +0000 Subject: [PATCH 10/14] Fix Seeder (#425) --- src/Stratis.CirrusDnsD/Program.cs | 23 +++++++++++++------ .../Stratis.CirrusDnsD.csproj | 1 + 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Stratis.CirrusDnsD/Program.cs b/src/Stratis.CirrusDnsD/Program.cs index 4ec582b0f1..b3a5fc1d2f 100644 --- a/src/Stratis.CirrusDnsD/Program.cs +++ b/src/Stratis.CirrusDnsD/Program.cs @@ -4,6 +4,7 @@ using Stratis.Bitcoin; using Stratis.Bitcoin.Builder; using Stratis.Bitcoin.Configuration; +using Stratis.Bitcoin.Consensus; using Stratis.Bitcoin.Features.Api; using Stratis.Bitcoin.Features.BlockStore; using Stratis.Bitcoin.Features.Dns; @@ -12,8 +13,10 @@ using Stratis.Bitcoin.Features.SmartContracts; using Stratis.Bitcoin.Features.SmartContracts.PoA; using Stratis.Bitcoin.Features.SmartContracts.Wallet; +using Stratis.Bitcoin.Networks; using Stratis.Bitcoin.Utilities; using Stratis.Features.Collateral; +using Stratis.Features.Collateral.CounterChain; using Stratis.Features.SQLiteWalletRepository; using Stratis.Sidechains.Networks; @@ -67,9 +70,11 @@ public static async Task Main(string[] args) private static IFullNode GetSideChainFullNode(NodeSettings nodeSettings) { - IFullNode node = new FullNodeBuilder() - .UseNodeSettings(nodeSettings) - .UseBlockStore() + DbType dbType = nodeSettings.GetDbType(); + + IFullNodeBuilder nodeBuilder = new FullNodeBuilder() + .UseNodeSettings(nodeSettings, dbType) + .UseBlockStore(dbType) .UseMempool() .AddSmartContracts(options => { @@ -77,16 +82,20 @@ private static IFullNode GetSideChainFullNode(NodeSettings nodeSettings) options.UsePoAWhitelistedContracts(); }) .AddPoAFeature() - .UsePoAConsensus() + .UsePoAConsensus(dbType) .CheckCollateralCommitment() + + // This needs to be set so that we can check the magic bytes during the Strat to Strax changeover. + // Perhaps we can introduce a block height check rather? + .SetCounterChainNetwork(StraxNetwork.MainChainNetworks[nodeSettings.Network.NetworkType]()) + .UseSmartContractWallet() .AddSQLiteWalletRepository() .UseApi() .AddRPC() - .UseDns() - .Build(); + .UseDns(); - return node; + return nodeBuilder.Build(); } private static IFullNode GetDnsNode(NodeSettings nodeSettings) diff --git a/src/Stratis.CirrusDnsD/Stratis.CirrusDnsD.csproj b/src/Stratis.CirrusDnsD/Stratis.CirrusDnsD.csproj index fd8778f4db..dc3e303e15 100644 --- a/src/Stratis.CirrusDnsD/Stratis.CirrusDnsD.csproj +++ b/src/Stratis.CirrusDnsD/Stratis.CirrusDnsD.csproj @@ -23,6 +23,7 @@ + From d446432c305c9d65ffdbc029cc8d61598f895c74 Mon Sep 17 00:00:00 2001 From: Francois de la Rouviere Date: Mon, 15 Feb 2021 10:15:39 +0000 Subject: [PATCH 11/14] Fix AddressIndexer Console (#426) --- .../AddressIndexing/AddressIndexer.cs | 4 ++-- src/Stratis.Bitcoin.Features.Wallet/WalletFeature.cs | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Stratis.Bitcoin.Features.BlockStore/AddressIndexing/AddressIndexer.cs b/src/Stratis.Bitcoin.Features.BlockStore/AddressIndexing/AddressIndexer.cs index cb9d4da768..0faabfba2d 100644 --- a/src/Stratis.Bitcoin.Features.BlockStore/AddressIndexing/AddressIndexer.cs +++ b/src/Stratis.Bitcoin.Features.BlockStore/AddressIndexing/AddressIndexer.cs @@ -389,7 +389,7 @@ private void SaveAll() private void AddInlineStats(StringBuilder benchLog) { - benchLog.AppendLine("AddressIndexer.Height: ".PadRight(LoggingConfiguration.ColumnLength + 1) + this.IndexerTip.Height.ToString().PadRight(9) + + benchLog.AppendLine("AddressIndexer Height".PadRight(LoggingConfiguration.ColumnLength) + $": {this.IndexerTip.Height}".PadRight(9) + "AddressCache%: " + this.addressIndexRepository.GetLoadPercentage().ToString().PadRight(8) + "OutPointCache%: " + this.outpointsRepository.GetLoadPercentage().ToString().PadRight(8) + $"Ms/block: {Math.Round(this.averageTimePerBlock.Average, 2)}"); @@ -673,7 +673,7 @@ public LastBalanceDecreaseTransactionModel GetLastBalanceDecreaseTransaction(str // Height 0 is used as a placeholder height for compacted address balance records, so ignore them if they are the only record. if (lastBalanceHeight == 0) return null; - + ChainedHeader header = this.chainIndexer.GetHeader(lastBalanceHeight); if (header == null) diff --git a/src/Stratis.Bitcoin.Features.Wallet/WalletFeature.cs b/src/Stratis.Bitcoin.Features.Wallet/WalletFeature.cs index cce70ce1a3..3fa56db638 100644 --- a/src/Stratis.Bitcoin.Features.Wallet/WalletFeature.cs +++ b/src/Stratis.Bitcoin.Features.Wallet/WalletFeature.cs @@ -100,8 +100,6 @@ private void AddInlineStats(StringBuilder log) log.AppendLine("Wallet Height".PadRight(LoggingConfiguration.ColumnLength) + $": {this.walletManager.WalletTipHeight}".PadRight(10) + $"(Hash: {this.walletManager.WalletTipHash})"); else log.AppendLine("Wallet Height".PadRight(LoggingConfiguration.ColumnLength) + ": No Wallet"); - - log.AppendLine(); } private void AddComponentStats(StringBuilder log) From a57bf78f73e246014da1b6d2cf92b05eee1147c5 Mon Sep 17 00:00:00 2001 From: Francois de la Rouviere Date: Mon, 15 Feb 2021 10:20:33 +0000 Subject: [PATCH 12/14] Update ConnectionManager.cs (#427) --- src/Stratis.Bitcoin/Connection/ConnectionManager.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Stratis.Bitcoin/Connection/ConnectionManager.cs b/src/Stratis.Bitcoin/Connection/ConnectionManager.cs index 5ece69e57e..bc0d6021c3 100644 --- a/src/Stratis.Bitcoin/Connection/ConnectionManager.cs +++ b/src/Stratis.Bitcoin/Connection/ConnectionManager.cs @@ -316,6 +316,7 @@ void AddPeerInfo(StringBuilder peerBuilder, INetworkPeer peer) int inbound = this.ConnectedPeers.Count(x => x.Inbound); + builder.AppendLine(); builder.AppendLine($">> Connections (In:{inbound}) (Out:{this.ConnectedPeers.Count() - inbound})"); builder.AppendLine("Data Transfer".PadRight(LoggingConfiguration.ColumnLength, ' ') + $": Received: {totalRead.BytesToMegaBytes()} MB Sent: {totalWritten.BytesToMegaBytes()} MB"); From e014e505f56639efe9337e62459f01343968523d Mon Sep 17 00:00:00 2001 From: YakupIpek Date: Mon, 12 Apr 2021 08:10:18 +0300 Subject: [PATCH 13/14] Improved address conversation --- src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs b/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs index 3e51924207..61bad66b22 100644 --- a/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs +++ b/src/Stratis.SmartContracts.CLR/EcRecoverProvider.cs @@ -11,13 +11,6 @@ namespace Stratis.SmartContracts.CLR /// public class EcRecoverProvider : IEcRecoverProvider { - private readonly Network network; - - public EcRecoverProvider(Network network) - { - this.network = network; - } - private static uint256 GetUint256FromMessage(byte[] message) { return new uint256(HashHelper.Keccak256(message)); @@ -36,7 +29,7 @@ public Address GetSigner(byte[] message, byte[] signature) uint256 hashedUint256 = GetUint256FromMessage(message); PubKey pubKey = PubKey.RecoverCompact(hashedUint256, signature); - return pubKey.GetAddress(this.network).ToString().ToAddress(this.network); + return pubKey.Hash.ToBytes().ToAddress(); } /// From 50203a186389d56104d4376b56c91e2cefe5bc67 Mon Sep 17 00:00:00 2001 From: YakupIpek Date: Mon, 12 Apr 2021 08:13:43 +0300 Subject: [PATCH 14/14] Removed network parameter --- .../ContractExecutorTestContext.cs | 2 +- src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs | 2 +- src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs | 2 +- .../PoW/SmartContractMinerTests.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTestContext.cs b/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTestContext.cs index eb1bc72083..85de0ef61d 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTestContext.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTestContext.cs @@ -51,7 +51,7 @@ public ContractExecutorTestContext() this.Vm = new ReflectionVirtualMachine(this.Validator, this.LoggerFactory, this.AssemblyLoader, this.ModuleDefinitionReader, this.ContractCache); this.StateProcessor = new StateProcessor(this.Vm, this.AddressGenerator); this.InternalTxExecutorFactory = new InternalExecutorFactory(this.LoggerFactory, this.StateProcessor); - this.EcRecoverProvider = new EcRecoverProvider(this.Network); + this.EcRecoverProvider = new EcRecoverProvider(); this.SmartContractStateFactory = new SmartContractStateFactory(this.ContractPrimitiveSerializer, this.InternalTxExecutorFactory, this.Serializer, this.EcRecoverProvider); } } diff --git a/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs b/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs index 0f00d7fa70..f4bf09a20a 100644 --- a/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs +++ b/src/Stratis.SmartContracts.CLR.Tests/ContractExecutorTests.cs @@ -61,7 +61,7 @@ public ContractExecutorTests() this.vm = new ReflectionVirtualMachine(this.validator, this.loggerFactory, this.assemblyLoader, this.moduleDefinitionReader, this.contractCache); this.stateProcessor = new StateProcessor(this.vm, this.addressGenerator); this.internalTxExecutorFactory = new InternalExecutorFactory(this.loggerFactory, this.stateProcessor); - this.ecRecoverProvider = new EcRecoverProvider(this.network); + this.ecRecoverProvider = new EcRecoverProvider(); this.smartContractStateFactory = new SmartContractStateFactory(this.contractPrimitiveSerializer, this.internalTxExecutorFactory, this.serializer, this.ecRecoverProvider); this.callDataSerializer = new CallDataSerializer(this.contractPrimitiveSerializer); diff --git a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs index 1a4e511742..0a3dacfda4 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/ECRecoverTests.cs @@ -29,7 +29,7 @@ public void CanSignAndRetrieveSender() // Sign a message byte[] offChainSignature = EcRecoverProvider.SignMessage(privateKey, message); - var ecRecover = new EcRecoverProvider(network); + var ecRecover = new EcRecoverProvider(); // Get the address out of the signature Address recoveredAddress = ecRecover.GetSigner(message, offChainSignature); diff --git a/src/Stratis.SmartContracts.IntegrationTests/PoW/SmartContractMinerTests.cs b/src/Stratis.SmartContracts.IntegrationTests/PoW/SmartContractMinerTests.cs index 011b23e53a..d4bc3c3646 100644 --- a/src/Stratis.SmartContracts.IntegrationTests/PoW/SmartContractMinerTests.cs +++ b/src/Stratis.SmartContracts.IntegrationTests/PoW/SmartContractMinerTests.cs @@ -349,7 +349,7 @@ private void InitializeSmartContractComponents(string callingMethod) this.internalTxExecutorFactory = new InternalExecutorFactory(this.loggerFactory, this.stateProcessor); this.primitiveSerializer = new ContractPrimitiveSerializer(this.network); this.serializer = new Serializer(this.primitiveSerializer); - this.ecRecoverProvider = new EcRecoverProvider(this.network); + this.ecRecoverProvider = new EcRecoverProvider(); this.smartContractStateFactory = new SmartContractStateFactory(this.primitiveSerializer, this.internalTxExecutorFactory, this.serializer, this.ecRecoverProvider); this.stateFactory = new StateFactory(this.smartContractStateFactory); this.ExecutorFactory = new ReflectionExecutorFactory(this.loggerFactory, this.callDataSerializer, this.refundProcessor, this.transferProcessor, this.stateFactory, this.stateProcessor, this.primitiveSerializer);