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

/// <summary>
/// Constructs a special contract with empty script, will get the script with
/// scriptHash from blockchain when doing the verification.
/// <code>
/// Note:
/// Creates "m" out of "n" type verification script using <paramref name="publicKeys"/> length
/// with the default BFT assumptions of Ceiling(n - (n-1) / 3) for "m".
/// </code>
/// </summary>
/// <param name="publicKeys">The public keys of the contract.</param>
/// <returns>Multi-Signature contract <see cref="WalletAccount"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="publicKeys" /> is empty.
/// </exception>
/// <seealso cref="CreateMultiSigAccount(int, ECPoint[])"/>
public WalletAccount CreateMultiSigAccount(params ECPoint[] publicKeys) =>
CreateMultiSigAccount(1, publicKeys);

/// <summary>
/// Constructs a special contract with empty script, will get the script with
/// scriptHash from blockchain when doing the verification.
/// </summary>
/// <param name="m">The number of correct signatures that need to be provided in order for the verification to pass.</param>
/// <param name="publicKeys">The public keys of the contract.</param>
/// <returns>Multi-Signature contract <see cref="WalletAccount"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="publicKeys" /> is empty or <paramref name="m"/> is greater than <paramref name="publicKeys"/> length or
/// <paramref name="m"/> is less than 1 or <paramref name="m"/> is greater than 1024.
/// </exception>
/// <seealso cref="CreateMultiSigAccount(ECPoint[])"/>
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 +284,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));
}
}
}