Ejemplo n.º 1
0
        public void Init()
        {
            // Deploy native contracts
            var block = new Network.P2P.Payloads.Block()
            {
                Index         = 0,
                ConsensusData = new Network.P2P.Payloads.ConsensusData(),
                Transactions  = new Network.P2P.Payloads.Transaction[0],
                Witness       = new Network.P2P.Payloads.Witness()
                {
                    InvocationScript   = new byte[0],
                    VerificationScript = Contract.CreateSignatureRedeemScript(ECPoint.FromBytes(pubKey, ECCurve.Secp256k1))
                },
                NextConsensus = UInt160.Zero,
                MerkleRoot    = UInt256.Zero,
                PrevHash      = UInt256.Zero
            };

            _engine = new TestEngine(TriggerType.Application, block);
            ((TestSnapshot)_engine.Snapshot).SetPersistingBlock(block);
            _engine.Snapshot.DeployNativeContracts();

            _engine.Reset();
            _engine.AddEntryScript("./TestClasses/Contract_Native.cs");
        }
Ejemplo n.º 2
0
        public NeoKey(byte[] privateKey)
        {
            if (privateKey.Length != 32 && privateKey.Length != 96 && privateKey.Length != 104)
            {
                throw new ArgumentException();
            }
            this.PrivateKey = new byte[32];
            Buffer.BlockCopy(privateKey, privateKey.Length - 32, PrivateKey, 0, 32);

            ECPoint pKey;

            if (privateKey.Length == 32)
            {
                pKey = ECCurve.Secp256r1.G * privateKey;
            }
            else
            {
                pKey = ECPoint.FromBytes(privateKey, ECCurve.Secp256r1);
            }

            var bytes = pKey.EncodePoint(true).ToArray();

            this.CompressedPublicKey = bytes;

            this.PublicKeyHash = CryptoUtils.ToScriptHash(bytes);

            this.signatureScript = CreateSignatureScript(bytes);
            signatureHash        = CryptoUtils.ToScriptHash(signatureScript);

            this.PublicKey = pKey.EncodePoint(false).Skip(1).ToArray();

            this.address = CryptoUtils.ToAddress(signatureHash);
            this.WIF     = GetWIF();
        }
Ejemplo n.º 3
0
        public void Init()
        {
            // Deploy native contracts
            var block = new Network.P2P.Payloads.Block()
            {
                Index         = 0,
                ConsensusData = new Network.P2P.Payloads.ConsensusData(),
                Transactions  = new Network.P2P.Payloads.Transaction[0],
                Witness       = new Network.P2P.Payloads.Witness()
                {
                    InvocationScript   = new byte[0],
                    VerificationScript = Contract.CreateSignatureRedeemScript(ECPoint.FromBytes(pubKey, ECCurve.Secp256k1))
                },
                NextConsensus = UInt160.Zero,
                MerkleRoot    = UInt256.Zero,
                PrevHash      = UInt256.Zero
            };

            _engine = new TestEngine(TriggerType.Application, block);
            ((TestSnapshot)_engine.Snapshot).SetPersistingBlock(block);

            using (var script = new ScriptBuilder())
            {
                script.EmitSysCall(TestEngine.Native_Deploy);
                _engine.LoadScript(script.ToArray());
                Assert.AreEqual(VMState.HALT, _engine.Execute());
            }

            _engine.Reset();
            _engine.AddEntryScript("./TestClasses/Contract_Native.cs");
        }
Ejemplo n.º 4
0
        public bool Sign(SignatureContext context)
        {
            bool fSuccess = false;

            foreach (UInt160 scriptHash in context.ScriptHashes)
            {
                Contract contract = GetContract(scriptHash);
                if (contract == null)
                {
                    continue;
                }
                Account account = GetAccountByScriptHash(scriptHash);
                if (account == null)
                {
                    continue;
                }
                byte[] signature;
                using (account.Decrypt())
                {
                    signature = context.Signable.Sign(account.PrivateKey, account.PublicKey);
                }
                fSuccess |= context.Add(contract.RedeemScript, ECPoint.FromBytes(account.PublicKey, ECCurve.Secp256r1), signature);
            }
            return(fSuccess);
        }
Ejemplo n.º 5
0
        private bool VerifyKeyAndSignature(Signature signature, out ECPoint pubKey)
        {
            pubKey = null;

            // 0. only support ecdsa and secp256k1 for now, hashing is sha256
            if (signature.SignatureType != SignatureType.Ecdsa || signature.PublicKey.CurveType != CurveType.Secp256r1)
            {
                return(false);
            }

            // 1. check public key bytes
            try
            {
                pubKey = ECPoint.FromBytes(signature.PublicKey.Bytes, ECCurve.Secp256r1);
            }
            catch (Exception)
            {
                return(false);
            }

            // 2. check if public key matches address
            if (("21" + pubKey.EncodePoint(true).ToHexString() + "ac").HexToBytes().ToScriptHash().ToAddress() != signature.SigningPayload.Address)
            {
                return(false);
            }

            // 3. check if public key and signature matches
            return(Crypto.Default.VerifySignature(signature.SigningPayload.Bytes, signature.Bytes, pubKey.EncodePoint(false)));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Derive returns the network-specific address associated with a public key.
        /// Blockchains that require an on-chain action to create an account should not implement this method.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JObject ConstructionDerive(ConstructionDeriveRequest request)
        {
            if (request.NetworkIdentifier?.Blockchain?.ToLower() != "neo n3")
            {
                return(Error.NETWORK_IDENTIFIER_INVALID.ToJson());
            }
            if (request.NetworkIdentifier?.Network?.ToLower() != network)
            {
                return(Error.NETWORK_IDENTIFIER_INVALID.ToJson());
            }
            if (request.PublicKey.CurveType != CurveType.Secp256r1)
            {
                return(Error.CURVE_NOT_SUPPORTED.ToJson());
            }

            ECPoint pubKey;

            try
            {
                pubKey = ECPoint.FromBytes(request.PublicKey.HexBytes.HexToBytes(), ECCurve.Secp256r1);
            }
            catch (Exception)
            {
                return(Error.INVALID_PUBLIC_KEY.ToJson());
            }

            string address = Contract.CreateSignatureRedeemScript(pubKey).ToScriptHash().ToAddress(system.Settings.AddressVersion);
            ConstructionDeriveResponse response = new ConstructionDeriveResponse(new AccountIdentifier(address));

            return(response.ToJson());
        }
Ejemplo n.º 7
0
        string IAddressGenerator.GenerateAddress(byte[] pubKeyBytes)
        {
            var     pubKey     = ECPoint.FromBytes(pubKeyBytes, ECCurve.Secp256r1);
            UInt160 scriptHash = Contract.CreateSignatureContract(pubKey).ScriptHash;
            string  address    = scriptHash.ToAddress(ProtocolSettings.Default.AddressVersion);

            return(address);
        }
Ejemplo n.º 8
0
 public Account(byte[] privateKey)
 {
     if (privateKey.Length != 32 && privateKey.Length != 96 && privateKey.Length != 104)
     {
         throw new ArgumentException();
     }
     this.PrivateKey = new byte[32];
     Buffer.BlockCopy(privateKey, privateKey.Length - 32, PrivateKey, 0, 32);
     if (privateKey.Length == 32)
     {
         this.PublicKey = ECCurve.Secp256r1.G * privateKey;
     }
     else
     {
         this.PublicKey = ECPoint.FromBytes(privateKey, ECCurve.Secp256r1);
     }
     this.PublicKeyHash = PublicKey.EncodePoint(true).ToScriptHash();
     ProtectedMemory.Protect(PrivateKey, MemoryProtectionScope.SameProcess);
 }
Ejemplo n.º 9
0
        public KeyPair(byte[] privateKey)
        {
            if (privateKey.Length != 32 && privateKey.Length != 96 && privateKey.Length != 104)
            {
                throw new ArgumentException();
            }
            PrivateKey = new byte[32];
            Buffer.BlockCopy(privateKey, privateKey.Length - 32, PrivateKey, 0, 32);
            if (privateKey.Length == 32)
            {
                PublicKey = ECCurve.Secp256r1.G * privateKey;
            }
            else
            {
                PublicKey = ECPoint.FromBytes(privateKey, ECCurve.Secp256r1);
            }
#if NET47
            ProtectedMemory.Protect(PrivateKey, MemoryProtectionScope.SameProcess);
#endif
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Derive returns the network-specific address associated with a public key.
        /// Blockchains that require an on-chain action to create an account should not implement this method.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JObject ConstructionDerive(ConstructionDeriveRequest request)
        {
            if (request.PublicKey.CurveType != CurveType.Secp256r1)
            {
                return(Error.CURVE_NOT_SUPPORTED.ToJson());
            }

            ECPoint pubKey;

            try
            {
                pubKey = ECPoint.FromBytes(request.PublicKey.Bytes, ECCurve.Secp256r1);
            }
            catch (Exception)
            {
                return(Error.INVALID_PUBLIC_KEY.ToJson());
            }

            string address = ("21" + pubKey.EncodePoint(true).ToHexString() + "ac").HexToBytes().ToScriptHash().ToAddress();
            ConstructionDeriveResponse response = new ConstructionDeriveResponse(address);

            return(response.ToJson());
        }
Ejemplo n.º 11
0
        public void Init()
        {
            // Deploy native contracts
            var block = new Block()
            {
                Header = new Header()
                {
                    Index   = 0,
                    Witness = new Witness()
                    {
                        InvocationScript   = System.Array.Empty <byte>(),
                        VerificationScript = Contract.CreateSignatureRedeemScript(ECPoint.FromBytes(pubKey, ECCurve.Secp256k1))
                    },
                    NextConsensus = UInt160.Zero,
                    MerkleRoot    = UInt256.Zero,
                    PrevHash      = UInt256.Zero
                },
                Transactions = System.Array.Empty <Transaction>(),
            };

            _engine = new TestEngine(TriggerType.Application, block, new TestDataCache(block));
            _engine.Reset();
            _engine.AddEntryScript("./TestClasses/Contract_Native.cs");
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Public Key => (UInt160)address hash
        /// </summary>
        /// <param name="publicKeyHex"></param>
        /// <returns></returns>
        public static UInt160 ToUInt160FromPublicKey(this string publicKeyHex)
        {
            var point = ECPoint.FromBytes(publicKeyHex.HexToBytes(), ECCurve.Secp256r1);

            return(point.ToUInt160FromPublicKey());
        }
Ejemplo n.º 13
0
 private MinerWallet(byte[] key_exported)
 {
     this.key_exported = key_exported;
     this.PublicKey    = ECPoint.FromBytes(key_exported, ECCurve.Secp256r1);
     ProtectedMemory.Protect(key_exported, MemoryProtectionScope.SameProcess);
 }