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); Vauth.Cryptography.ECC.ECPoint[] publicKeys = new Vauth.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); Vauth.Cryptography.ECC.ECPoint[] publicKeys = new Vauth.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.TheVauthSystem.Settings)) { engine.LoadScript(invocation.Concat(verification).ToArray(), configureState: p => p.CallFlags = CallFlags.None); engine.Execute(); engine.ValtConsumed.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(privateKey1); publicKeys1[i] = key1.PublicKey; } byte[] script1 = Contract.CreateMultiSigRedeemScript(20, publicKeys1); Assert.AreEqual(true, Vauth.SmartContract.Helper.IsMultiSigContract(script1, out _, out ECPoint[] p1)); CollectionAssert.AreEqual(publicKeys1.OrderBy(p => p).ToArray(), p1); ECPoint[] publicKeys2 = new ECPoint[256]; for (int i = 0; i < 256; i++) { byte[] privateKey2 = new byte[32]; RandomNumberGenerator rng2 = RandomNumberGenerator.Create(); rng2.GetBytes(privateKey2); KeyPair key2 = new(privateKey2); publicKeys2[i] = key2.PublicKey; } byte[] script2 = Contract.CreateMultiSigRedeemScript(256, publicKeys2); Assert.AreEqual(true, Vauth.SmartContract.Helper.IsMultiSigContract(script2, out _, out ECPoint[] p2)); CollectionAssert.AreEqual(publicKeys2.OrderBy(p => p).ToArray(), p2); ECPoint[] publicKeys3 = new ECPoint[3]; for (int i = 0; i < 3; i++) { byte[] privateKey3 = new byte[32]; RandomNumberGenerator rng3 = RandomNumberGenerator.Create(); rng3.GetBytes(privateKey3); KeyPair key3 = new(privateKey3); publicKeys3[i] = key3.PublicKey; } byte[] script3 = Contract.CreateMultiSigRedeemScript(3, publicKeys3); Assert.AreEqual(true, Vauth.SmartContract.Helper.IsMultiSigContract(script3, out _, out ECPoint[] p3)); CollectionAssert.AreEqual(publicKeys3.OrderBy(p => p).ToArray(), p3); ECPoint[] publicKeys4 = new ECPoint[3]; for (int i = 0; i < 3; i++) { byte[] privateKey4 = new byte[32]; RandomNumberGenerator rng4 = RandomNumberGenerator.Create(); rng4.GetBytes(privateKey4); KeyPair key4 = new(privateKey4); publicKeys4[i] = key4.PublicKey; } byte[] script4 = Contract.CreateMultiSigRedeemScript(3, publicKeys4); script4[^ 1] = 0x00;
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); Vauth.Cryptography.ECC.ECPoint[] publicKeys = new Vauth.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[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, contract.Script); Assert.AreEqual(2, contract.ParameterList.Length); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[0]); Assert.AreEqual(ContractParameterType.Signature, contract.ParameterList[1]); }