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

Commit

Permalink
merge changes for sending from contract addr
Browse files Browse the repository at this point in the history
  • Loading branch information
localhuman committed Oct 14, 2017
1 parent b73036d commit 12dee9f
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 32 deletions.
10 changes: 2 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 @@ -76,12 +76,7 @@ def ToScriptHash(scripts):
@staticmethod
def RawBytesToScriptHash(raw):
rawh = binascii.unhexlify(raw)

rawhashstr = binascii.unhexlify(bytes(Crypto.Hash160(rawh), encoding='utf-8'))
# h160bytes = bytearray(rawhashstr)
# h160bytes.reverse()
# out = bytes(h160bytes.hex(), encoding='utf-8')
# return out
return UInt160(data=rawhashstr)

@staticmethod
Expand All @@ -95,8 +90,8 @@ def VerifyScripts(verifiable):
print("couldng get script hashes %s " % e)
return False


if len(hashes) != len(verifiable.Scripts):
print("hashes not same length as verifiable scripts")
return False

for i in range(0, len(hashes)):
Expand All @@ -112,7 +107,6 @@ def VerifyScripts(verifiable):
else:
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())
Expand Down
1 change: 0 additions & 1 deletion neo/Core/TX/Transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ def GetScriptHashesForVerifying(self):

hashlist = list(hashes)
hashlist.sort()

return hashlist


Expand Down
2 changes: 1 addition & 1 deletion neo/Implementations/Wallets/peewee/Models.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Contract(ModelBase):
RawData = CharField()
ScriptHash = CharField()
PublicKeyHash = CharField()
Account = ForeignKeyField(Account)
Account = ForeignKeyField(Account, null=True)
Address = ForeignKeyField(Address)

class Key(ModelBase):
Expand Down
44 changes: 37 additions & 7 deletions neo/Implementations/Wallets/peewee/UserWallet.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from autologging import logged
import json
from playhouse.migrate import *

@logged
class UserWallet(Wallet):
Expand All @@ -52,6 +53,14 @@ def BuildDatabase(self):
print("Couldnt build database %s " % e)
self.__log.debug("couldnt build database %s " % e)

def Migrate(self):
db = PWDatabase.ContextDB()
migrator = SqliteMigrator(db)

migrate(
migrator.drop_not_null('Contract','Account_id')
)

def DB(self):
return PWDatabase.Context()

Expand Down Expand Up @@ -319,10 +328,10 @@ def PubKeys(self):
for ct in self._contracts.values():
if ct.PublicKeyHash == k.PublicKeyHash:
signature_contract = ct
if signature_contract:
addr = signature_contract.Address

addr = signature_contract.Address

jsn.append( {'Address': addr, 'Public Key': pub.decode('utf-8')})
jsn.append( {'Address': addr, 'Public Key': pub.decode('utf-8')})

return jsn

Expand All @@ -334,14 +343,28 @@ def DeleteAddress(self, script_hash):
try:
c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex)
c.delete_instance()
print("deleted coin!!!")
except Exception as e:
print("Couldnt delete coin %s %s " % (e, coin))
self.__log.debug("could not delete coin %s %s " % (coin, e))

address = Address.get(ScriptHash = bytes(script_hash.ToArray()))
address.delete_instance()

todelete = bytes(script_hash.ToArray())

for c in Contract.select():

address = c.Address
if address.ScriptHash == todelete:

c.delete_instance()
address.delete_instance()

try:
address = Address.get(ScriptHash = todelete)
address.delete_instance()
except Exception as e:
pass

print("Deleted address %s " % script_hash)
return True,coins_toremove


Expand All @@ -357,10 +380,17 @@ def ToJson(self, verbose=False):
jsn['path'] = self._path

addresses = [Crypto.ToAddress(UInt160(data=addr.ScriptHash)) for addr in Address.select()]

balances = []
for asset in assets:
bc_asset = Blockchain.Default().GetAssetState(asset.ToBytes())
total = self.GetBalance(asset).value / Fixed8.D
balances.append("[%s]: %s " % (bc_asset.GetName(), total))

jsn['addresses'] = addresses
jsn['height'] = self._current_height
jsn['percent_synced'] = percent_synced
jsn['balances'] = ["%s : %s " % (asset.ToString(), self.GetBalance(asset).value / Fixed8.D) for asset in assets]
jsn['balances'] = balances
jsn['public_keys'] = self.PubKeys()

if verbose:
Expand Down
1 change: 0 additions & 1 deletion neo/Network/NodeLeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def RemoveConnectedPeer(self, peer):


def ResetBlockRequestsAndCache(self):
print("RESETTING BLOCK REQUESTS AND CACHE!")
BC.Default().BlockSearchTries = 0
for p in self.Peers:
p.myblockrequests= set()
Expand Down
2 changes: 1 addition & 1 deletion neo/Prompt/Commands/LoadSmartContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def ImportContractAddr(wallet, args):
if contract is not None:

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

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

Expand Down
13 changes: 11 additions & 2 deletions neo/Prompt/Commands/Send.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from neo.SmartContract.ContractParameterContext import ContractParametersContext
from neo.Network.NodeLeader import NodeLeader
from neo.Prompt.Utils import parse_param,get_arg


import json
from neo.Core.TX.TransactionAttribute import TransactionAttribute,TransactionAttributeUsage

def construct_and_send(prompter, wallet, arguments):
try:
Expand Down Expand Up @@ -71,15 +71,24 @@ def construct_and_send(prompter, wallet, arguments):
print("incorrect password")
return

standard_contract = wallet.GetChangeAddress()
data = standard_contract.Data
tx.Attributes = [TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=data)]


context = ContractParametersContext(tx)
wallet.Sign(context)

if context.Completed:

tx.scripts = context.GetScripts()


wallet.SaveTransaction(tx)

# print("will send tx: %s " % json.dumps(tx.ToJson(),indent=4))

relayed = NodeLeader.Instance().Relay(tx)

if relayed:
Expand Down
15 changes: 13 additions & 2 deletions neo/SmartContract/ContractParameterContext.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def Completed(self):

for p in item.ContractParameters:
if p is None or p.Value is None:
return False
#
if p.Type == 16:
p.Value = []
else:
return False

return True

Expand Down Expand Up @@ -87,6 +91,7 @@ def CreateItem(self, contract):
return item



def AddSignature(self, contract, pubkey, signature):

if contract.Type == ContractType.MultiSigContract:
Expand Down Expand Up @@ -144,11 +149,17 @@ def GetScripts(self):
plist.reverse()

for p in plist:
sb.push(p.Value)

if type(p.Value) is list:
sb.push(0)
else:
sb.push(p.Value)

vscript = bytearray(0)

if item.Script is not None:
if type(item.Script) is str:
item.Script = item.Script.encode('utf-8')
vscript = item.Script

witness = Witness(
Expand Down
Empty file modified neo/SmartContract/ContractParameterType.py
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion neo/UInt160.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def CompareTo(self, other):

length = len(x)

for i in range(0, length):
for i in range(length-1, 0, -1):

if x[i] > y[i]:
return 1
Expand Down
12 changes: 8 additions & 4 deletions neo/Wallets/Wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def FindUnspentCoinsByAssetAndTotal(self, asset_id, amount):
for coin in coins:
sum = sum + coin.Output.Value


if sum < amount:
return None

Expand Down Expand Up @@ -288,10 +287,15 @@ def GetAvailable(self, asset_id):
def GetBalance(self, asset_id):
total=Fixed8(0)
for coin in self.GetCoins():
if coin.State & CoinState.Spent == 0 \
and coin.Output.AssetId == asset_id:
if coin.Output.AssetId == asset_id:

if coin.State & CoinState.Confirmed > 0 and \
coin.State & CoinState.Spent == 0 and \
coin.State & CoinState.Locked == 0 and \
coin.State & CoinState.Frozen == 0 and \
coin.State & CoinState.WatchOnly == 0:

total = total + coin.Output.Value
total = total + coin.Output.Value

return total

Expand Down
4 changes: 0 additions & 4 deletions prompt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env python


"""
"""

import json
import logging
import datetime
Expand Down

0 comments on commit 12dee9f

Please sign in to comment.