/// <summary>
        /// Create a multi-sig account from a list of public keys.
        ///
        /// </summary>
        /// <param name="signingThreshold">Minimum number of signatures required for verification. Must be larger than 0 and less than number of keys provided.</param>
        /// <param name="pubKeys">List of public keys to form the account. 2-16 keys allowed. Order is important.</param>
        /// <param name="label"></param>
        public Account CreateMultiSigAccount(int signingThreshold, string[] pubKeys, string label)
        {
            if (signingThreshold >= pubKeys.Length)
            {
                throw new ArgumentOutOfRangeException($"{nameof(signingThreshold)} must be larger than 0 and less than number of keys provided");
            }

            var publicKeys = new List <NeoECPoint>();

            foreach (var pubKey in pubKeys)
            {
                publicKeys.Add(NeoECPoint.Parse(pubKey, ECCurve.Secp256r1));
            }

            var verificationScript = Helper.CreateMultiSigRedeemScript(signingThreshold, publicKeys.ToArray());
            var contract           = new Contract
            {
                Script     = verificationScript,
                Parameters = Enumerable.Repeat(new Parameter("signature", ParameterType.Signature), signingThreshold).ToList(),
                Deployed   = false
            };

            var account = new Account(verificationScript.ToScriptHash(), label, false, false, null, contract, null);

            AddAccount(account);
            return(account);
        }
Exemple #2
0
 /// <summary>
 /// Verifies the signature of a message using the public key
 /// </summary>
 /// <param name="message"></param>
 /// <param name="signature"></param>
 /// <param name="pubkey"></param>
 /// <returns></returns>
 public static bool VerifySignature(byte[] message, byte[] signature, byte[] pubkey)
 {
     if (pubkey.Length == 33 && (pubkey[0] == 0x02 || pubkey[0] == 0x03))
     {
         try
         {
             pubkey = ECPoint.DecodePoint(pubkey, Cryptography.ECC.ECCurve.Secp256r1).EncodePoint(false).Skip(1).ToArray();
         }
         catch
         {
             return(false);
         }
     }
     else if (pubkey.Length == 65 && pubkey[0] == 0x04)
     {
         pubkey = pubkey.Skip(1).ToArray();
     }
     else if (pubkey.Length != 64)
     {
         throw new ArgumentException();
     }
     using (var ecdsa = ECDsa.Create(new ECParameters
     {
         Curve = ECCurve.NamedCurves.nistP256,
         Q = new System.Security.Cryptography.ECPoint
         {
             X = pubkey.Take(32).ToArray(),
             Y = pubkey.Skip(32).ToArray()
         }
     }))
     {
         return(ecdsa.VerifyData(message, signature, HashAlgorithmName.SHA256));
     }
 }
Exemple #3
0
 /// <summary>
 ///     Creates the public address of a public key
 /// </summary>
 /// <param name="publicKey"></param>
 /// <returns></returns>
 public static byte[] CreateSignatureRedeemScript(ECPoint publicKey)
 {
     using (var sb = new ScriptBuilder())
     {
         sb.EmitPush(publicKey.EncodePoint(true));
         sb.Emit(OpCode.CHECKSIG);
         return(sb.ToArray());
     }
 }