-
Notifications
You must be signed in to change notification settings - Fork 1k
[Add] Transaction Builder
#3477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NGDAdmin
merged 18 commits into
neo-project:master
from
cschuchardt88:add/builders/transaction
Sep 27, 2024
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d421c59
Added Builders with tests
cschuchardt88 a4c71f3
Added SignerBuilder and started WitnessRuleBuilder
cschuchardt88 4582df6
Added `WitnessConditionBuilder` with tests
cschuchardt88 abcc0bb
Added more logic
cschuchardt88 48ddcc2
Fixed `SignerBuilder` class
cschuchardt88 3f30e3b
Merge branch 'master' into add/builders/transaction
cschuchardt88 a4da230
Code touch ups
cschuchardt88 675b6f8
Added more tests
cschuchardt88 f773a06
Merge branch 'master' into add/builders/transaction
c8593ab
Update src/Neo/Builders/TransactionBuilder.cs
shargon 4a6104d
Merge branch 'master' into add/builders/transaction
cschuchardt88 5220266
Fixed `And` `Or` and `Not` conditions
cschuchardt88 9c2cb5f
Merge branch 'master' into add/builders/transaction
cschuchardt88 1322cfb
Merge branch 'master' into add/builders/transaction
cschuchardt88 3a14a1f
Fixed Memory leak
cschuchardt88 6b6dc94
Merge branch 'add/builders/transaction' of https://github.com/cschuch…
cschuchardt88 68469d9
Added error message for Witness scripts
cschuchardt88 8afada7
Merge branch 'master' into add/builders/transaction
NGDAdmin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright (C) 2015-2024 The Neo Project. | ||
| // | ||
| // TransactionAttributesBuilder.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Network.P2P.Payloads; | ||
| using System; | ||
| using System.Linq; | ||
|
|
||
| namespace Neo.Builders | ||
| { | ||
| public sealed class TransactionAttributesBuilder | ||
| { | ||
| private TransactionAttribute[] _attributes = []; | ||
|
|
||
| private TransactionAttributesBuilder() { } | ||
|
|
||
| public static TransactionAttributesBuilder CreateEmpty() | ||
| { | ||
| return new TransactionAttributesBuilder(); | ||
| } | ||
|
|
||
| public TransactionAttributesBuilder AddConflict(Action<Conflicts> conflictAction) | ||
| { | ||
| var conflicts = new Conflicts(); | ||
| conflictAction(conflicts); | ||
| _attributes = [.. _attributes, conflicts]; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionAttributesBuilder AddOracleResponse(Action<OracleResponse> oracleResponseAction) | ||
| { | ||
| var oracleResponse = new OracleResponse(); | ||
| oracleResponseAction(oracleResponse); | ||
| _attributes = [.. _attributes, oracleResponse]; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionAttributesBuilder AddHighPriority() | ||
| { | ||
| if (_attributes.Any(a => a is HighPriorityAttribute)) | ||
| throw new InvalidOperationException("HighPriority already exists in the attributes."); | ||
|
|
||
| var highPriority = new HighPriorityAttribute(); | ||
| _attributes = [.. _attributes, highPriority]; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionAttributesBuilder AddNotValidBefore(uint block) | ||
| { | ||
| if (_attributes.Any(a => a is NotValidBefore b && b.Height == block)) | ||
| throw new InvalidOperationException($"Block {block} already exists in the attributes."); | ||
|
|
||
| var validUntilBlock = new NotValidBefore() | ||
| { | ||
| Height = block | ||
| }; | ||
|
|
||
| _attributes = [.. _attributes, validUntilBlock]; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionAttribute[] Build() | ||
| { | ||
| return _attributes; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright (C) 2015-2024 The Neo Project. | ||
| // | ||
| // TransactionBuilder.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Network.P2P.Payloads; | ||
| using Neo.VM; | ||
| using System; | ||
|
|
||
| namespace Neo.Builders | ||
| { | ||
| public sealed class TransactionBuilder | ||
| { | ||
|
|
||
| private byte _version = 0; | ||
| private uint _nonce = (uint)new Random().Next(); | ||
| private uint _systemFee = 0; | ||
| private uint _networkFee = 0; | ||
| private uint _validUntilBlock = 0; | ||
| private byte[] _script = []; | ||
| private TransactionAttribute[] _attributes = []; | ||
| private Signer[] _signers = []; | ||
| private Witness[] _witnesses = []; | ||
|
|
||
| private TransactionBuilder() { } | ||
|
|
||
| public static TransactionBuilder CreateEmpty() | ||
| { | ||
| return new TransactionBuilder(); | ||
| } | ||
|
|
||
| public TransactionBuilder Version(byte version) | ||
| { | ||
| _version = version; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionBuilder Nonce(uint nonce) | ||
| { | ||
| _nonce = nonce; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionBuilder SystemFee(uint systemFee) | ||
| { | ||
| _systemFee = systemFee; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionBuilder NetworkFee(uint networkFee) | ||
| { | ||
| _networkFee = networkFee; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionBuilder ValidUntil(uint blockIndex) | ||
| { | ||
| _validUntilBlock = blockIndex; | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionBuilder AttachSystem(Action<ScriptBuilder> scriptBuilder) | ||
| { | ||
| var sb = new ScriptBuilder(); | ||
| scriptBuilder(sb); | ||
| _script = sb.ToArray(); | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionBuilder AddAttributes(Action<TransactionAttributesBuilder> transactionAttributeBuilder) | ||
| { | ||
| var ab = TransactionAttributesBuilder.CreateEmpty(); | ||
| transactionAttributeBuilder(ab); | ||
| _attributes = ab.Build(); | ||
| return this; | ||
| } | ||
|
|
||
| public TransactionBuilder AddWitness(Action<WitnessBuilder> witnessBuilder) | ||
| { | ||
| var wb = WitnessBuilder.CreateEmpty(); | ||
| witnessBuilder(wb); | ||
| _witnesses = [.. _witnesses, wb.Build()]; | ||
| return this; | ||
| } | ||
|
|
||
| public Transaction Build() | ||
| { | ||
| return new Transaction() | ||
| { | ||
| Version = _version, | ||
| Nonce = _nonce, | ||
| SystemFee = _systemFee, | ||
| NetworkFee = _networkFee, | ||
| ValidUntilBlock = _validUntilBlock, | ||
| Script = _script, | ||
| Attributes = _attributes, | ||
| Signers = _signers, | ||
| Witnesses = _witnesses | ||
| }; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (C) 2015-2024 The Neo Project. | ||
| // | ||
| // WitnessBuilder.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Cryptography.ECC; | ||
| using Neo.Network.P2P.Payloads; | ||
| using Neo.SmartContract; | ||
| using Neo.VM; | ||
| using System; | ||
|
|
||
| namespace Neo.Builders | ||
| { | ||
| public sealed class WitnessBuilder | ||
| { | ||
| private byte[] _invocationScript = []; | ||
| private byte[] _verificationScript = []; | ||
|
|
||
| private WitnessBuilder() { } | ||
|
|
||
| public static WitnessBuilder CreateEmpty() | ||
| { | ||
| return new WitnessBuilder(); | ||
| } | ||
|
|
||
| public WitnessBuilder AddInvocation(Action<ScriptBuilder> scriptBuilder) | ||
| { | ||
| var sb = new ScriptBuilder(); | ||
| scriptBuilder(sb); | ||
| _invocationScript = sb.ToArray(); | ||
| return this; | ||
| } | ||
|
|
||
| public WitnessBuilder AddSigner(Action<ScriptBuilder> scriptBuilder) | ||
| { | ||
| var sb = new ScriptBuilder(); | ||
| scriptBuilder(sb); | ||
| _verificationScript = sb.ToArray(); | ||
| return this; | ||
| } | ||
|
|
||
| public WitnessBuilder AddSigner(Action<Contract> contract) | ||
| { | ||
| var c = new Contract(); | ||
| contract(c); | ||
| _verificationScript = c.Script; | ||
| return this; | ||
| } | ||
|
|
||
| public WitnessBuilder AddSigner(ECPoint publicKey) | ||
| { | ||
| _verificationScript = Contract.CreateSignatureRedeemScript(publicKey); | ||
| return this; | ||
| } | ||
|
|
||
| public Witness Build() | ||
| { | ||
| return new Witness() | ||
| { | ||
| InvocationScript = _invocationScript, | ||
| VerificationScript = _verificationScript | ||
| }; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Copyright (C) 2015-2024 The Neo Project. | ||
| // | ||
| // UT_TransactionBuilder.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Neo.Builders; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Neo.UnitTests.Builders | ||
| { | ||
| [TestClass] | ||
| public class UT_TransactionBuilder | ||
| { | ||
| [TestMethod] | ||
| public void TestVersion() | ||
| { | ||
| byte expectedVersion = 1; | ||
| var tx = TransactionBuilder.CreateEmpty() | ||
| .Version(expectedVersion) | ||
| .Build(); | ||
|
|
||
| Assert.AreEqual(expectedVersion, tx.Version); | ||
| Assert.IsNotNull(tx.Hash); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestNonce() | ||
| { | ||
| var expectedNonce = (uint)Random.Shared.Next(); | ||
| var tx = TransactionBuilder.CreateEmpty() | ||
| .Nonce(expectedNonce) | ||
| .Build(); | ||
|
|
||
| Assert.AreEqual(expectedNonce, tx.Nonce); | ||
| Assert.IsNotNull(tx.Hash); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestSystemFee() | ||
| { | ||
| var expectedSystemFee = (uint)Random.Shared.Next(); | ||
| var tx = TransactionBuilder.CreateEmpty() | ||
| .SystemFee(expectedSystemFee) | ||
| .Build(); | ||
|
|
||
| Assert.AreEqual(expectedSystemFee, tx.SystemFee); | ||
| Assert.IsNotNull(tx.Hash); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestNetworkFee() | ||
| { | ||
| var expectedNetworkFee = (uint)Random.Shared.Next(); | ||
| var tx = TransactionBuilder.CreateEmpty() | ||
| .NetworkFee(expectedNetworkFee) | ||
| .Build(); | ||
|
|
||
| Assert.AreEqual(expectedNetworkFee, tx.NetworkFee); | ||
| Assert.IsNotNull(tx.Hash); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestValidUntilBlock() | ||
| { | ||
| var expectedValidUntilBlock = (uint)Random.Shared.Next(); | ||
| var tx = TransactionBuilder.CreateEmpty() | ||
| .ValidUntil(expectedValidUntilBlock) | ||
| .Build(); | ||
|
|
||
| Assert.AreEqual(expectedValidUntilBlock, tx.ValidUntilBlock); | ||
| Assert.IsNotNull(tx.Hash); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.