Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
implemented local verification script check before node relay
Browse files Browse the repository at this point in the history
  • Loading branch information
localhuman committed Oct 12, 2017
1 parent 6b9f94c commit 7a42080
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 48 deletions.
14 changes: 6 additions & 8 deletions neo/Core/Helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from neo.SmartContract import TriggerType
from neo import Settings
from base58 import b58decode

import pdb
class Helper(object):


Expand Down Expand Up @@ -37,10 +37,8 @@ def GetHashData(hashable):
def Sign(verifiable, keypair):

prikey = bytes(keypair.PrivateKey)
print("private key %s " % prikey)
hashdata = verifiable.GetHashData()
res = Crypto.Default().Sign(hashdata, prikey, keypair.PublicKey)
print("result is %s " % res)
return res

@staticmethod
Expand Down Expand Up @@ -105,29 +103,29 @@ def VerifyScripts(verifiable):
verification = verifiable.Scripts[i].VerificationScript



if len(verification) == 0:
# print("VERIFICATION IS 0, EMITTING APP CALL")
sb = ScriptBuilder()
sb.EmitAppCall(hashes[i].Data)
verification = sb.ToArray()

else:
if hashes[i] != verification:
verification_hash = Crypto.ToScriptHash(verification,unhex=False)
if hashes[i] != verification_hash:
print("hashes not equal to script hash!")
return False

engine = ApplicationEngine(TriggerType.Verification, verifiable, GetBlockchain(), GetStateReader(), Fixed8.Zero())
engine.LoadScript(verification, False)
engine.LoadScript(verifiable.Scripts[i].InvocationScript, True)
invoction = verifiable.Scripts[i].InvocationScript
engine.LoadScript(invoction, True)

res = engine.Execute()
if not res:
print("engine did not execute")
return False


if engine.EvaluationStack.Count != 1 or not engine.EvaluationStack.Pop().GetBoolean():
# print("stack not one, or stack false")
return False

return True
Expand Down
10 changes: 8 additions & 2 deletions neo/Core/TX/Transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ def getAllInputs(self):
def ResetReferences(self):
self.__references = None

@property
def Scripts(self):
return self.scripts

@property
def References(self):

Expand Down Expand Up @@ -384,8 +388,11 @@ def ToJson(self):
def Verify(self, mempool):
self.__log.debug("Verifying transaction: %s " % self.Hash.ToBytes())


return Helper.VerifyScripts(self)

# print("return true for now ...")
return True
# return True

# for i in range(1, len(self.inputs)):
# j=0
Expand Down Expand Up @@ -456,7 +463,6 @@ def Verify(self, mempool):
# if usageECDH > 1:
# return False
#
# return Helper.VerifyScripts(self)



Expand Down
16 changes: 15 additions & 1 deletion neo/Cryptography/Crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,29 @@ def Sign(message, private_key, public_key):
@staticmethod
def VerifySignature(message, signature, public_key):

Crypto.SetupSignatureCurve()

if type(public_key) is EllipticCurve.ECPoint:

pubkey_x = public_key.x.value.to_bytes(32,'big')
pubkey_y = public_key.y.value.to_bytes(32,'big')

public_key = pubkey_x + pubkey_y

m = message
try:
m = binascii.unhexlify(message)
except Exception as e:
print("could not get m")

if len(public_key) == 33:

public_key = bitcoin.decompress(public_key)
public_key = public_key[1:]

vk = VerifyingKey.from_string( public_key,curve=NIST256p, hashfunc=hashlib.sha256 )
return vk.verify(signature, message)
res = vk.verify(signature, m,hashfunc=hashlib.sha256)
return res


class CryptoInstance():
Expand Down
4 changes: 4 additions & 0 deletions neo/Implementations/Wallets/peewee/UserWallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def AddContract(self, contract):
db_contract = None
try:
db_contract = Contract.get(ScriptHash = contract.ScriptHash.ToBytes())
db_contract.delete_instance()
db_contract = None
print("got existing contract??")
except Exception as e:
self.__log.debug("contract does not exist yet")

Expand All @@ -132,6 +135,7 @@ def AddContract(self, contract):
self.__log.debug("Creating db contract %s " % db_contract)

db_contract.save()
print("created db contract: %s " % db_contract)

def AddWatchOnly(self, script_hash):
super(UserWallet,self).AddWatchOnly(script_hash)
Expand Down
16 changes: 3 additions & 13 deletions neo/Prompt/Commands/LoadSmartContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,19 @@ def ImportContractAddr(wallet, args):
if len(pubkey) != 66:
print("invalid public key format")


pubkey_script_hash = Crypto.ToScriptHash(pubkey,unhex=True)

print("import contract address %s %s " % (contract_hash, pubkey))
print("pubkey script hash %s " % pubkey_script_hash)
contract = Blockchain.Default().GetContract(contract_hash)

if contract is not None:

reedeem_script = contract.Code.Script.hex()
param_list = bytearray(b'\x00')

reedeem_script = contract.Code.Script
param_list = contract.Code.ParameterList



verification_contract = Contract.Create(pubkey_script_hash,param_list,reedeem_script)
verification_contract = Contract.Create(reedeem_script,param_list,pubkey_script_hash)

address = verification_contract.Address

print("address %s " % address)

print("contract objcet is %s " % contract)

wallet.AddContract(verification_contract)

print("Added contract addres %s to wallet" % address)
Expand Down
2 changes: 1 addition & 1 deletion neo/SmartContract/Contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, redeem_script=None, param_list=None, pubkey_hash=None):
self._address = None

@staticmethod
def Create(publicKeyHash, parameterList, redeemScript):
def Create(redeemScript, parameterList, publicKeyHash):

return Contract(redeemScript, parameterList, publicKeyHash)

Expand Down
9 changes: 0 additions & 9 deletions neo/SmartContract/ContractParameterContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,14 @@ def __init__(self, verifiable):
def Completed(self):

if len(self.ContextItems) < len(self.ScriptHashes):
print("context items, script hashes %s %s " % (self.ContextItems, self.ScriptHashes))
return False

for item in self.ContextItems.values():
print("going through items %s " % item.Script)
if item is None:
print("item is none...")
return False

for p in item.ContractParameters:
print("item parameters??? %s" % p)
if p is None or p.Value is None:
print("parameter is none")
return False

return True
Expand All @@ -70,7 +65,6 @@ def Completed(self):
def Add(self, contract, index, parameter):

item = self.CreateItem(contract)
print("ADDING iTEM:: %s " % item)
item.ContractParameters[index].Value = parameter

# pdb.set_trace()
Expand All @@ -84,7 +78,6 @@ def CreateItem(self, contract):
return self.ContextItems[ contract.ScriptHash.ToBytes()]

if not contract.ScriptHash in self.ScriptHashes:
print("script hash not in self script hashes...")
return None

item = ContextItem(contract)
Expand Down Expand Up @@ -113,7 +106,6 @@ def AddSignature(self, contract, pubkey, signature):
else:
index = i

print("ADDING SIG!! .... %s %s %s " % (contract, index,signature))
return self.Add(contract, index, signature)

def GetIndex(self, script_hash):
Expand Down Expand Up @@ -145,7 +137,6 @@ def GetScripts(self):
for i in range(0, len(self.ScriptHashes)):

item = self.ContextItems[self.ScriptHashes[i].ToBytes()]
print("GETTING SCRIPTS, item is %s " % item)

sb = ScriptBuilder()

Expand Down
15 changes: 9 additions & 6 deletions neo/VM/ExecutionEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,14 @@ def ExecuteOp(self, opcode, context):

try:

self.Crypto.VerifySignature( self.ScriptContainer.GetMessage(), pubkey, sig)
res = self.Crypto.VerifySignature( self.ScriptContainer.GetMessage(), sig, pubkey)
estack.PushT(res)

except Exception as e:

print("couldnt operate signature verification")
estack.PushT(False)
traceback.print_stack()
traceback.print_exc()


elif opcode == CHECKMULTISIG:
Expand Down Expand Up @@ -795,10 +798,10 @@ def StepInto(self):
else:
op = self.CurrentContext.OpReader.ReadByte(do_ord=False)

# opname = ToName(op)
# print("____________________________________________________")
# print("%s -> %s" % (op, opname))
# print("-----------------------------------")
# opname = ToName(op)
# print("____________________________________________________")
# print("%s -> %s" % (op, opname))
# print("-----------------------------------")

self.ops_processed += 1

Expand Down
12 changes: 4 additions & 8 deletions neo/Wallets/Wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,25 +599,21 @@ def Sign(self, context):

for hash in context.ScriptHashes:

print("checkhing hash...")
contract = self.GetContract(hash)
if contract is None:
print("contract is none, return")
continue

key = self.GetKeyByScriptHash(hash)
print("key is %s " % key)

if key is None:
print("key is none")
continue

print("Signing.... %s %s " % (context.Verifiable, key))
signature = Helper.Sign(context.Verifiable, key)
print("signature %s " % signature)

res = context.AddSignature(contract, key.PublicKey, signature)
print("result is %s " % res)

success |=res
print("success is %s " % success)

return success


Expand Down

0 comments on commit 7a42080

Please sign in to comment.