Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/Neo/Builders/SignerBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// SignerBuilder.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 System;

namespace Neo.Builders
{
public sealed class SignerBuilder
{
private readonly Signer _signer = new Signer()
{
Account = UInt160.Zero,
AllowedContracts = [],
AllowedGroups = [],
Rules = [],
Scopes = WitnessScope.None,
};

private SignerBuilder() { }

public static SignerBuilder CreateEmpty()
{
return new SignerBuilder();
}

public SignerBuilder Account(UInt160 scriptHash)
{
_signer.Account = scriptHash;
return this;
}

public SignerBuilder AllowContract(UInt160 contractHash)
{
_signer.AllowedContracts = [.. _signer.AllowedContracts, contractHash];
return this;
}

public SignerBuilder AllowGroup(ECPoint publicKey)
{
_signer.AllowedGroups = [.. _signer.AllowedGroups, publicKey];
return this;
}

public SignerBuilder AddWitnessScope(WitnessScope scope)
{
_signer.Scopes |= scope;
return this;
}

public SignerBuilder AddWitnessRule(WitnessRuleAction action, Action<WitnessRuleBuilder> config)
{
var rb = WitnessRuleBuilder.Create(action);
config(rb);
_signer.Rules = [.. _signer.Rules, rb.Build()];
return this;
}

public Signer Build()
{
return _signer;
}
}
}
74 changes: 74 additions & 0 deletions src/Neo/Builders/TransactionAttributesBuilder.cs
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> config)
{
var conflicts = new Conflicts();
config(conflicts);
_attributes = [.. _attributes, conflicts];
return this;
}

public TransactionAttributesBuilder AddOracleResponse(Action<OracleResponse> config)
{
var oracleResponse = new OracleResponse();
config(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;
}
}
}
111 changes: 111 additions & 0 deletions src/Neo/Builders/TransactionBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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 readonly Transaction _tx = new()
{
Script = new[] { (byte)OpCode.RET },
Attributes = [],
Signers = [],
Witnesses = [],
};

private TransactionBuilder() { }

public static TransactionBuilder CreateEmpty()
{
return new TransactionBuilder();
}

public TransactionBuilder Version(byte version)
{
_tx.Version = version;
return this;
}

public TransactionBuilder Nonce(uint nonce)
{
_tx.Nonce = nonce;
return this;
}

public TransactionBuilder SystemFee(uint systemFee)
{
_tx.SystemFee = systemFee;
return this;
}

public TransactionBuilder NetworkFee(uint networkFee)
{
_tx.NetworkFee = networkFee;
return this;
}

public TransactionBuilder ValidUntil(uint blockIndex)
{
_tx.ValidUntilBlock = blockIndex;
return this;
}

public TransactionBuilder AttachSystem(Action<ScriptBuilder> config)
{
var sb = new ScriptBuilder();
config(sb);
_tx.Script = sb.ToArray();
return this;
}

public TransactionBuilder AddAttributes(Action<TransactionAttributesBuilder> config)
{
var ab = TransactionAttributesBuilder.CreateEmpty();
config(ab);
_tx.Attributes = ab.Build();
return this;
}

public TransactionBuilder AddWitness(Action<WitnessBuilder> config)
{
var wb = WitnessBuilder.CreateEmpty();
config(wb);
_tx.Witnesses = [.. _tx.Witnesses, wb.Build()];
return this;
}

public TransactionBuilder AddWitness(Action<WitnessBuilder, Transaction> config)
{
var wb = WitnessBuilder.CreateEmpty();
config(wb, _tx);
_tx.Witnesses = [.. _tx.Witnesses, wb.Build()];
return this;
}

public TransactionBuilder AddSigner(Action<SignerBuilder, Transaction> config)
{
var wb = SignerBuilder.CreateEmpty();
config(wb, _tx);
_tx.Signers = [.. _tx.Signers, wb.Build()];
return this;
}

public Transaction Build()
{
return _tx;
}
}
}
67 changes: 67 additions & 0 deletions src/Neo/Builders/WitnessBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.Network.P2P.Payloads;
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> config)
{
var sb = new ScriptBuilder();
config(sb);
_invocationScript = sb.ToArray();
return this;
}

public WitnessBuilder AddInvocation(byte[] bytes)
{
_invocationScript = bytes;
return this;
}

public WitnessBuilder AddVerification(Action<ScriptBuilder> config)
{
var sb = new ScriptBuilder();
config(sb);
_verificationScript = sb.ToArray();
return this;
}

public WitnessBuilder AddVerification(byte[] bytes)
{
_verificationScript = bytes;
return this;
}

public Witness Build()
{
return new Witness()
{
InvocationScript = _invocationScript,
VerificationScript = _verificationScript,
};
}
}
}
Loading