Skip to content
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

[Add] Transaction Builder #3477

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

cschuchardt88
Copy link
Member

@cschuchardt88 cschuchardt88 commented Sep 4, 2024

Description

Easy simple builders for creating with signing, along with creating scripts for transactions.

Change Log

  • Add new Transaction, Witness, Signer, TransactionAttribute, WitnessRule and WitnessCondition builders

Example

var myPublicKey = ECPoint.Parse("021821807f923a3da004fb73871509d7635bcc05f41edef2a3ca5c941d8bbc1231", ECCurve.Secp256r1);
var tx = TransactionBuilder.CreateEmpty()
    .ValidUntil(NativeContract.Ledger.CurrentIndex(store) + 1)
    .AttachSystem(script => script.EmitDynamicCall(NativeContract.NEO.Hash, "transfer", UInt160.Zero, UInt160.Zero, 10, null))
    .AddAttributes(attr => attr.AddHighPriority())
    .AddSigner((signer, tx) =>
    {
        signer.Account(UInt160.Zero);
        signer.AddWitnessScope(WitnessScope.WitnessRules);
        signer.AddWitnessRule(WitnessRuleAction.Deny, rule =>
        {
            rule.AddCondition(condition =>
            {
                condition.And(and =>
                {
                    and.ScriptHash(UInt160.Zero);
                });
            });
        });
    })
    .AddWitness((witness, tx) =>
    {
        witness.AddInvocation(s => s.Emit(VM.OpCode.PUSHDATA1, [64, tx.Sign(kp, ProtocolSettings.Default.Network)]));
        witness.AddVerification(Contract.CreateSignatureRedeemScript(myPublicKey));
    })
    .Build();

Applies To

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Wrote unit tests

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link
Member

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good idea, @cschuchardt88 .

Maybe it would be good to create an issue to present the Neo.Builders with more details and where are the planned use cases.

@cschuchardt88
Copy link
Member Author

cschuchardt88 commented Sep 4, 2024

@shargon @roman-khimov @vncoelho @Jim8y
Can you give me an example of a transaction with complex witness rules? For example how to interconnect in/with Not, Or, And, ScriptHash, Group, Boolean and CalledBy* conditions.

@cschuchardt88
Copy link
Member Author

If anyone wants to test it, and give me some feedback on anything that needs to be improved.

@Jim8y
Copy link
Contributor

Jim8y commented Sep 6, 2024

@shargon @roman-khimov @vncoelho @Jim8y Can you give me an example of a transaction with complex witness rules? For example how to interconnect in/with Not, Or, And, ScriptHash, Group, Boolean and CalledBy* conditions.

@superboyiii @Hecate2 do you have any idea?

@Hecate2
Copy link
Contributor

Hecate2 commented Sep 6, 2024

@shargon @roman-khimov @vncoelho @Jim8y Can you give me an example of a transaction with complex witness rules? For example how to interconnect in/with Not, Or, And, ScriptHash, Group, Boolean and CalledBy* conditions.

@superboyiii @Hecate2 do you have any idea?

@Jim8y @cschuchardt88
https://github.com/Hecate2/neo-fairy-client/blob/master/test_witness_rule.py

@cschuchardt88
Copy link
Member Author

You want to test it @Hecate2? Then give some feedback on the builder.

src/Neo/Builders/WitnessConditionBuilder.cs Outdated Show resolved Hide resolved
src/Neo/Builders/TransactionBuilder.cs Outdated Show resolved Hide resolved
@Hecate2
Copy link
Contributor

Hecate2 commented Sep 7, 2024

You want to test it @Hecate2? Then give some feedback on the builder.

I think we can try to fill most properties of a tx automatically, unless the user specifies something else, or we do not have access to current chain enviromment. If we leave everything to be filled by the user, the complexity of building a tx is not reduced at all.
For example, the builder can try to get current block index to fill ValidUntil, and try to get current opened wallet to fill signer (may be difficult because wallets exists in Neo.Cli and RpcServer instead of Neo).
Ideally the user may just specify the contract call target and the params to build a new tx.

@cschuchardt88
Copy link
Member Author

cschuchardt88 commented Sep 7, 2024

You want to test it @Hecate2? Then give some feedback on the builder.

I think we can try to fill most properties of a tx automatically, unless the user specifies something else, or we do not have access to current chain enviromment. If we leave everything to be filled by the user, the complexity of building a tx is not reduced at all. For example, the builder can try to get current block index to fill ValidUntil, and try to get current opened wallet to fill signer (may be difficult because wallets exists in Neo.Cli and RpcServer instead of Neo). Ideally the user may just specify the contract call target and the params to build a new tx.

I could create a new Create method in extensions or in class that would get additional environment data for building. Now this is the 1st part of the builders. Next Im going to build ApplicationEngine builder, than integrate it in transaction builder for getting SystemFee. Same with NetworkFee and others.

Jim8y
Jim8y previously approved these changes Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants