public void TestCreateMultiSigContract() { byte[] privateKey1 = new byte[32]; RandomNumberGenerator rng1 = RandomNumberGenerator.Create(); rng1.GetBytes(privateKey1); KeyPair key1 = new KeyPair(privateKey1); byte[] privateKey2 = new byte[32]; RandomNumberGenerator rng2 = RandomNumberGenerator.Create(); rng2.GetBytes(privateKey2); KeyPair key2 = new KeyPair(privateKey2); Neo.Cryptography.ECC.ECPoint[] publicKeys = new Neo.Cryptography.ECC.ECPoint[2]; publicKeys[0] = key1.PublicKey; publicKeys[1] = key2.PublicKey; publicKeys = publicKeys.OrderBy(p => p).ToArray(); Contract contract = Contract.CreateMultiSigContract(2, publicKeys); byte[] expectedArray = new byte[75]; expectedArray[0] = 0x52; expectedArray[1] = 0x21; Array.Copy(publicKeys[0].EncodePoint(true), 0, expectedArray, 2, 33); expectedArray[35] = 0x21; Array.Copy(publicKeys[1].EncodePoint(true), 0, expectedArray, 36, 33); expectedArray[69] = 0x52; expectedArray[70] = 0x68; Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_CheckMultiSig), 0, expectedArray, 71, 4); Assert.AreEqual(Encoding.Default.GetString(expectedArray), Encoding.Default.GetString(contract.Script)); Assert.AreEqual(2, contract.ParameterList.Length); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[0]); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[1]); }
public void TestCreateMultiSigRedeemScript() { byte[] privateKey1 = new byte[32]; RandomNumberGenerator rng1 = RandomNumberGenerator.Create(); rng1.GetBytes(privateKey1); KeyPair key1 = new KeyPair(privateKey1); byte[] privateKey2 = new byte[32]; RandomNumberGenerator rng2 = RandomNumberGenerator.Create(); rng2.GetBytes(privateKey2); KeyPair key2 = new KeyPair(privateKey2); Neo.Cryptography.ECC.ECPoint[] publicKeys = new Neo.Cryptography.ECC.ECPoint[2]; publicKeys[0] = key1.PublicKey; publicKeys[1] = key2.PublicKey; publicKeys = publicKeys.OrderBy(p => p).ToArray(); Action action = () => Contract.CreateMultiSigRedeemScript(0, publicKeys); action.Should().Throw <ArgumentException>(); byte[] script = Contract.CreateMultiSigRedeemScript(2, publicKeys); byte[] expectedArray = new byte[75]; expectedArray[0] = 0x52; expectedArray[1] = 0x21; Array.Copy(publicKeys[0].EncodePoint(true), 0, expectedArray, 2, 33); expectedArray[35] = 0x21; Array.Copy(publicKeys[1].EncodePoint(true), 0, expectedArray, 36, 33); expectedArray[69] = 0x52; expectedArray[70] = 0x68; Array.Copy(BitConverter.GetBytes(InteropService.Neo_Crypto_CheckMultiSig), 0, expectedArray, 71, 4); Assert.AreEqual(Encoding.Default.GetString(expectedArray), Encoding.Default.GetString(script)); }
public void TestCreateMultiSigRedeemScript() { byte[] privateKey1 = new byte[32]; RandomNumberGenerator rng1 = RandomNumberGenerator.Create(); rng1.GetBytes(privateKey1); KeyPair key1 = new KeyPair(privateKey1); byte[] privateKey2 = new byte[32]; RandomNumberGenerator rng2 = RandomNumberGenerator.Create(); rng2.GetBytes(privateKey2); KeyPair key2 = new KeyPair(privateKey2); Neo.Cryptography.ECC.ECPoint[] publicKeys = new Neo.Cryptography.ECC.ECPoint[2]; publicKeys[0] = key1.PublicKey; publicKeys[1] = key2.PublicKey; publicKeys = publicKeys.OrderBy(p => p).ToArray(); Action action = () => Contract.CreateMultiSigRedeemScript(0, publicKeys); action.Should().Throw <ArgumentException>(); byte[] script = Contract.CreateMultiSigRedeemScript(2, publicKeys); byte[] expectedArray = new byte[77]; expectedArray[0] = (byte)OpCode.PUSH2; expectedArray[1] = (byte)OpCode.PUSHDATA1; expectedArray[2] = 0x21; Array.Copy(publicKeys[0].EncodePoint(true), 0, expectedArray, 3, 33); expectedArray[36] = (byte)OpCode.PUSHDATA1; expectedArray[37] = 0x21; Array.Copy(publicKeys[1].EncodePoint(true), 0, expectedArray, 38, 33); expectedArray[71] = (byte)OpCode.PUSH2; expectedArray[72] = (byte)OpCode.SYSCALL; Array.Copy(BitConverter.GetBytes(ApplicationEngine.System_Crypto_CheckMultisig), 0, expectedArray, 73, 4); CollectionAssert.AreEqual(expectedArray, script); }
public void TestCreateMultiSigRedeemScriptFee() { byte[] privateKey1 = new byte[32]; RandomNumberGenerator rng1 = RandomNumberGenerator.Create(); rng1.GetBytes(privateKey1); KeyPair key1 = new KeyPair(privateKey1); byte[] privateKey2 = new byte[32]; RandomNumberGenerator rng2 = RandomNumberGenerator.Create(); rng2.GetBytes(privateKey2); KeyPair key2 = new KeyPair(privateKey2); Neo.Cryptography.ECC.ECPoint[] publicKeys = new Neo.Cryptography.ECC.ECPoint[2]; publicKeys[0] = key1.PublicKey; publicKeys[1] = key2.PublicKey; publicKeys = publicKeys.OrderBy(p => p).ToArray(); byte[] verification = Contract.CreateMultiSigRedeemScript(2, publicKeys); byte[] invocation = new ScriptBuilder().EmitPush(UInt160.Zero).EmitPush(UInt160.Zero).ToArray(); long fee = PolicyContract.DefaultExecFeeFactor * (ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * (2 + 2) + ApplicationEngine.OpCodePrices[OpCode.PUSHINT8] * 2 + ApplicationEngine.OpCodePrices[OpCode.SYSCALL] + ApplicationEngine.CheckSigPrice * 2); using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty <Signer>(), Attributes = Array.Empty <TransactionAttribute>() }, null, settings: TestBlockchain.TheNeoSystem.Settings)) { engine.LoadScript(invocation.Concat(verification).ToArray(), configureState: p => p.CallFlags = CallFlags.None); engine.Execute(); engine.GasConsumed.Should().Be(fee); } }
public void TestIsMultiSigContract() { ECPoint[] publicKeys1 = new ECPoint[20]; for (int i = 0; i < 20; i++) { byte[] privateKey1 = new byte[32]; RandomNumberGenerator rng1 = RandomNumberGenerator.Create(); rng1.GetBytes(privateKey1); KeyPair key1 = new KeyPair(privateKey1); publicKeys1[i] = key1.PublicKey; } byte[] script1 = Contract.CreateMultiSigRedeemScript(20, publicKeys1); Assert.AreEqual(true, Neo.SmartContract.Helper.IsMultiSigContract(script1, out _, out ECPoint[] p1)); CollectionAssert.AreEqual(publicKeys1.OrderBy(p => p).ToArray(), p1); Neo.Cryptography.ECC.ECPoint[] publicKeys2 = new Neo.Cryptography.ECC.ECPoint[256]; for (int i = 0; i < 256; i++) { byte[] privateKey2 = new byte[32]; RandomNumberGenerator rng2 = RandomNumberGenerator.Create(); rng2.GetBytes(privateKey2); KeyPair key2 = new KeyPair(privateKey2); publicKeys2[i] = key2.PublicKey; } byte[] script2 = Contract.CreateMultiSigRedeemScript(256, publicKeys2); Assert.AreEqual(true, Neo.SmartContract.Helper.IsMultiSigContract(script2, out _, out ECPoint[] p2)); CollectionAssert.AreEqual(publicKeys2.OrderBy(p => p).ToArray(), p2); Neo.Cryptography.ECC.ECPoint[] publicKeys3 = new Neo.Cryptography.ECC.ECPoint[3]; for (int i = 0; i < 3; i++) { byte[] privateKey3 = new byte[32]; RandomNumberGenerator rng3 = RandomNumberGenerator.Create(); rng3.GetBytes(privateKey3); KeyPair key3 = new KeyPair(privateKey3); publicKeys3[i] = key3.PublicKey; } byte[] script3 = Contract.CreateMultiSigRedeemScript(3, publicKeys3); Assert.AreEqual(true, Neo.SmartContract.Helper.IsMultiSigContract(script3, out _, out ECPoint[] p3)); CollectionAssert.AreEqual(publicKeys3.OrderBy(p => p).ToArray(), p3); Neo.Cryptography.ECC.ECPoint[] publicKeys4 = new Neo.Cryptography.ECC.ECPoint[3]; for (int i = 0; i < 3; i++) { byte[] privateKey4 = new byte[32]; RandomNumberGenerator rng4 = RandomNumberGenerator.Create(); rng4.GetBytes(privateKey4); KeyPair key4 = new KeyPair(privateKey4); publicKeys4[i] = key4.PublicKey; } byte[] script4 = Contract.CreateMultiSigRedeemScript(3, publicKeys4); script4[script4.Length - 1] = 0x00; Assert.AreEqual(false, Neo.SmartContract.Helper.IsMultiSigContract(script4, out _, out ECPoint[] p4)); Assert.IsNull(p4); }
public void TestCreateMultiSigContract() { byte[] privateKey1 = new byte[32]; RandomNumberGenerator rng1 = RandomNumberGenerator.Create(); rng1.GetBytes(privateKey1); KeyPair key1 = new KeyPair(privateKey1); byte[] privateKey2 = new byte[32]; RandomNumberGenerator rng2 = RandomNumberGenerator.Create(); rng2.GetBytes(privateKey2); KeyPair key2 = new KeyPair(privateKey2); Neo.Cryptography.ECC.ECPoint[] publicKeys = new Neo.Cryptography.ECC.ECPoint[2]; publicKeys[0] = key1.PublicKey; publicKeys[1] = key2.PublicKey; publicKeys = publicKeys.OrderBy(p => p).ToArray(); Contract contract = Contract.CreateMultiSigContract(2, publicKeys); byte[] expectedArray = new byte[78]; expectedArray[0] = (byte)OpCode.PUSH2; expectedArray[1] = (byte)OpCode.PUSHDATA1; expectedArray[2] = 0x21; Array.Copy(publicKeys[0].EncodePoint(true), 0, expectedArray, 3, 33); expectedArray[36] = (byte)OpCode.PUSHDATA1; expectedArray[37] = 0x21; Array.Copy(publicKeys[1].EncodePoint(true), 0, expectedArray, 38, 33); expectedArray[71] = (byte)OpCode.PUSH2; expectedArray[72] = (byte)OpCode.PUSHNULL; expectedArray[73] = (byte)OpCode.SYSCALL; Array.Copy(BitConverter.GetBytes(ApplicationEngine.Neo_Crypto_CheckMultisigWithECDsaSecp256r1), 0, expectedArray, 74, 4); CollectionAssert.AreEqual(expectedArray, contract.Script); Assert.AreEqual(2, contract.ParameterList.Length); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[0]); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[1]); }