Skip to content

offline signer for neo-cli #3

@dusmart

Description

@dusmart

build this neo cli plugin and you will have the ability to sign the transaction generated by neooffline

<PackageReference Include="Neo.ConsoleService" Version="1.2.0" />
using Neo.Network.P2P.Payloads;
using Neo.Wallets;
using Neo.ConsoleService;
using Neo.IO;
namespace Neo.Plugins;
public class OfflineSigner : Plugin
{
    public override string Description => "Offline Transaction Signer";
    private IWalletProvider? walletProvider;
    private Wallets.Wallet? w;
    public NeoSystem? neoSystem;
    protected override void OnSystemLoaded(NeoSystem system)
    {
        neoSystem = system;
        neoSystem.ServiceAdded += NeoSystem_ServiceAdded!;
    }
    private void NeoSystem_ServiceAdded(object sender, object service)
    {
        if (service is IWalletProvider)
        {
            walletProvider = service as IWalletProvider;
            neoSystem!.ServiceAdded -= NeoSystem_ServiceAdded!;
            walletProvider!.WalletChanged += WalletProvider_WalletChanged!;
        }
    }
    private void WalletProvider_WalletChanged(object sender, Wallet wallet)
    {
        walletProvider!.WalletChanged -= WalletProvider_WalletChanged!;
        w = wallet;
    }

    [ConsoleCommand("offsign", Category = "Lazynode", Description = "sign the hex encoded transaction")]
    private void OnOffSign(string txhex)
    {
        if (w == null)
        {
            ConsoleHelper.Error("You have to open the wallet first.");
            return;
        }

        byte[] data = Convert.FromHexString(txhex);
        Transaction tx = new();
        MemoryReader reader = new(data);
        tx.DeserializeUnsigned(ref reader);
        tx.Witnesses = tx.Signers.Select(v => new Witness { }).ToArray();
        Console.WriteLine(tx.ToJson(neoSystem.Settings).ToString(true));

        w.GetAccounts().Where(v => tx.Signers.Any(w => w.Account == v.ScriptHash)).Select(account =>
        {
            var signature = tx.Sign(account.GetKey(), neoSystem!.Settings.Network);
            Console.WriteLine($"{account.Address}: {signature.ToHexString()}");
            return 0;
        }).ToArray().ToString();
    }
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions