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

offline signer for neo-cli #3

Open
dusmart opened this issue Feb 17, 2023 · 0 comments
Open

offline signer for neo-cli #3

dusmart opened this issue Feb 17, 2023 · 0 comments

Comments

@dusmart
Copy link

dusmart commented Feb 17, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant