Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
26 changes: 26 additions & 0 deletions src/Neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ protected Wallet(string path, ProtocolSettings settings)
Path = path;
}

public WalletAccount CreateMultiSigAccount(params ECPoint[] publicKeys) =>
CreateMultiSigAccount(publicKeys.Length, publicKeys);

public WalletAccount CreateMultiSigAccount(int m, params ECPoint[] publicKeys)
{
ArgumentOutOfRangeException.ThrowIfEqual(publicKeys.Length, 0, nameof(publicKeys));
ArgumentOutOfRangeException.ThrowIfGreaterThan(m, publicKeys.Length, nameof(publicKeys));
ArgumentOutOfRangeException.ThrowIfLessThan(m, 1, nameof(m));
ArgumentOutOfRangeException.ThrowIfGreaterThan(m, 1024, nameof(m));

var contract = Contract.CreateMultiSigContract(m, publicKeys);
var account = GetAccounts().FirstOrDefault(
Copy link
Member

Choose a reason for hiding this comment

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

It choose a random key if we have multiple pubkeys in the wallet, how can the use know that he will sign with the first or second one?

Copy link
Member Author

@cschuchardt88 cschuchardt88 Oct 15, 2025

Choose a reason for hiding this comment

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

It can be any PublicKey in the list. That is how multi-signature accounts work. I added more checking in the test to verify this returned account, to ensure that it is indeed an account from the PublicKey list. Also they may not have the wallet open for that account. So it would use the PublicKey in the list that is in the currently opened wallet.

f =>
f.HasKey &&
f.Lock == false &&
publicKeys.Contains(f.GetKey().PublicKey));

return CreateAccount(contract, account?.GetKey());
}

/// <summary>
/// Creates a standard account for the wallet.
/// </summary>
Expand Down Expand Up @@ -237,6 +257,12 @@ public WalletAccount CreateAccount(Contract contract, byte[] privateKey)
return result;
}

public IEnumerable<WalletAccount> GetMultiSigAccounts() =>
GetAccounts()
.Where(static w =>
w.Lock == false &&
IsMultiSigContract(w.Contract.Script));

/// <summary>
/// Gets the account with the specified public key.
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions tests/Neo.UnitTests/Wallets/UT_Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.Extensions.Factories;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Sign;
Expand All @@ -22,7 +23,9 @@
using Neo.Wallets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Helper = Neo.SmartContract.Helper;

namespace Neo.UnitTests.Wallets
{
Expand Down Expand Up @@ -511,5 +514,28 @@ public void TestContainsKeyPair()
contains = wallet.ContainsSignable(pair.PublicKey);
Assert.IsFalse(contains); // locked
}

[TestMethod]
public void TestMultiSigAccount()
{
var expectedWallet = new MyWallet();
var expectedPrivateKey = RandomNumberFactory.NextBytes(32, cryptography: true);

var expectedWalletAccount = expectedWallet.CreateAccount(expectedPrivateKey);
var expectedAccountKey = expectedWalletAccount.GetKey();
var actualMultiSigAccount = expectedWallet.CreateMultiSigAccount([expectedAccountKey.PublicKey]);

Assert.IsNotNull(actualMultiSigAccount);
Assert.AreNotEqual(expectedWalletAccount.ScriptHash, actualMultiSigAccount.ScriptHash);
Assert.AreEqual(expectedAccountKey.PublicKey, actualMultiSigAccount.GetKey().PublicKey);
Assert.IsTrue(Helper.IsMultiSigContract(actualMultiSigAccount.Contract.Script));
Assert.IsTrue(expectedWallet.GetMultiSigAccounts().Contains(actualMultiSigAccount));

var notExpectedAccountKeys = new ECPoint[1025];
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => expectedWallet.CreateMultiSigAccount());
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => expectedWallet.CreateMultiSigAccount(2, [expectedAccountKey.PublicKey]));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => expectedWallet.CreateMultiSigAccount(0, [expectedAccountKey.PublicKey]));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => expectedWallet.CreateMultiSigAccount(1025, notExpectedAccountKeys));
}
}
}