def StandbyValidators(): if len(Blockchain.__validators) < 1: vlist = settings.STANDBY_VALIDATORS for pkey in settings.STANDBY_VALIDATORS: Blockchain.__validators.append(ECDSA.decode_secp256r1(pkey).G) return Blockchain.__validators
def StandbyValidators(): if len(Blockchain.__validators) < 1: vlist = settings.STANDBY_VALIDATORS for pkey in settings.STANDBY_VALIDATORS: Blockchain.__validators.append(ECDSA.decode_secp256r1(pkey).G) return Blockchain.__validators
def gather_param(index, param_type, do_continue=True): ptype = ContractParameterType(param_type) prompt_message = '[Param %s] %s input: ' % (index, ptype.name) try: result = get_input_prompt(prompt_message) except Exception as e: print(str(e)) # no results, abort True return None, True try: if ptype == ContractParameterType.String: return str(result), False elif ptype == ContractParameterType.Integer: return int(result), False elif ptype == ContractParameterType.Boolean: return bool(result), False elif ptype == ContractParameterType.PublicKey: return ECDSA.decode_secp256r1(result).G, False elif ptype == ContractParameterType.ByteArray: if isinstance(result, str) and len(result) == 34 and result[0] == 'A': return Helper.AddrStrToScriptHash(result).Data, False res = eval( result, {"__builtins__": { 'bytearray': bytearray, 'bytes': bytes }}, {}) if isinstance(res, bytes): return bytearray(res), False return res, False elif ptype == ContractParameterType.Array: res = eval(result) if isinstance(res, list): return res, False raise Exception("Please provide a list") else: raise Exception("Unknown param type %s " % ptype.name) except KeyboardInterrupt: # Control-C pressed: exit return None, True except Exception as e: print("Could not parse param as %s : %s " % (ptype, e)) if do_continue: return gather_param(index, param_type, do_continue) return None, True
def Validator_Register(self, engine): pubkey = ECDSA.decode_secp256r1(engine.EvaluationStack.Pop().GetByteArray(), unhex=False, check_on_curve=True).G if pubkey.IsInfinity: return False if not self.CheckWitnessPubkey(engine, pubkey): return False vstate = ValidatorState(pub_key=pubkey) validator = self._validators.GetOrAdd(pubkey.ToBytes(), vstate) engine.EvaluationStack.PushT(StackItem.FromInterface(validator)) return True
def Validator_Register(self, engine): pubkey = ECDSA.decode_secp256r1(engine.EvaluationStack.Pop().GetByteArray(), unhex=False, check_on_curve=True).G if pubkey.IsInfinity: return False if not self.CheckWitnessPubkey(engine, pubkey): return False vstate = ValidatorState(pub_key=pubkey) validator = self._validators.GetOrAdd(pubkey.ToBytes(), vstate) engine.EvaluationStack.PushT(StackItem.FromInterface(validator)) return True
def gather_signatures(context, itx, owners): do_exit = False print("owners %s " % owners) print("\n\n*******************\n") print("Gather Signatures for Transaction:\n%s " % json.dumps(itx.ToJson(), indent=4)) print("Please use a client to sign the following: %s " % itx.GetHashData()) owner_index = 0 while not context.Completed and not do_exit: next_script = owners[owner_index] next_addr = scripthash_to_address(next_script.Data) try: print("\n*******************\n") owner_input = prompt('Public Key and Signature for %s> ' % next_addr) items = owner_input.split(' ') pubkey = ECDSA.decode_secp256r1(items[0]).G sig = items[1] contract = Contract.CreateSignatureContract(pubkey) if contract.Address == next_addr: context.Add(contract, 0, sig) print("Adding signature %s " % sig) owner_index += 1 else: print("Public Key does not match address %s " % next_addr) except EOFError: # Control-D pressed: quit do_exit = True except KeyboardInterrupt: # Control-C pressed: do nothing do_exit = True except Exception as e: print("Could not parse input %s " % e) if context.Completed: print("Signatures complete") itx.scripts = context.GetScripts() return True else: print("Could not finish signatures") return False
def Runtime_CheckWitness(self, engine: ExecutionEngine): hashOrPubkey = engine.CurrentContext.EvaluationStack.Pop( ).GetByteArray() result = False if len(hashOrPubkey) == 20: result = self.CheckWitnessHash(engine, UInt160(data=hashOrPubkey)) elif len(hashOrPubkey) == 33: point = ECDSA.decode_secp256r1(hashOrPubkey, unhex=False).G result = self.CheckWitnessPubkey(engine, point) else: return False engine.CurrentContext.EvaluationStack.PushT(result) return True
def FromJson(json): """ Convert a json object to a ContractParameter object Args: item (dict): The item to convert to a ContractParameter object Returns: ContractParameter """ type = ContractParameterType.FromString(json['type']) value = json['value'] param = ContractParameter(type=type, value=None) if type == ContractParameterType.Signature or type == ContractParameterType.ByteArray: param.Value = bytearray.fromhex(value) elif type == ContractParameterType.Boolean: param.Value = bool(value) elif type == ContractParameterType.Integer: param.Value = int(value) elif type == ContractParameterType.Hash160: param.Value = UInt160.ParseString(value) elif type == ContractParameterType.Hash256: param.Value = UInt256.ParseString(value) # @TODO Not sure if this is working... elif type == ContractParameterType.PublicKey: param.Value = ECDSA.decode_secp256r1(value).G elif type == ContractParameterType.String: param.Value = str(value) elif type == ContractParameterType.Array: val = [ContractParameter.FromJson(item) for item in value] param.Value = val return param
def FromJson(json): """ Convert a json object to a ContractParameter object Args: item (dict): The item to convert to a ContractParameter object Returns: ContractParameter """ type = ContractParameterType.FromString(json['type']) value = json['value'] param = ContractParameter(type=type, value=None) if type == ContractParameterType.Signature or type == ContractParameterType.ByteArray: param.Value = bytearray.fromhex(value) elif type == ContractParameterType.Boolean: param.Value = bool(value) elif type == ContractParameterType.Integer: param.Value = int(value) elif type == ContractParameterType.Hash160: param.Value = UInt160.ParseString(value) elif type == ContractParameterType.Hash256: param.Value = UInt256.ParseString(value) # @TODO Not sure if this is working... elif type == ContractParameterType.PublicKey: param.Value = ECDSA.decode_secp256r1(value).G elif type == ContractParameterType.String: param.Value = str(value) elif type == ContractParameterType.Array: val = [ContractParameter.FromJson(item) for item in value] param.Value = val return param
def Runtime_CheckWitness(self, engine): hashOrPubkey = engine.EvaluationStack.Pop().GetByteArray() if len(hashOrPubkey) == 66 or len(hashOrPubkey) == 40: hashOrPubkey = binascii.unhexlify(hashOrPubkey) result = False if len(hashOrPubkey) == 20: result = self.CheckWitnessHash(engine, UInt160(data=hashOrPubkey)) elif len(hashOrPubkey) == 33: point = ECDSA.decode_secp256r1(hashOrPubkey, unhex=False).G result = self.CheckWitnessPubkey(engine, point) else: result = False engine.EvaluationStack.PushT(result) return True
def Runtime_CheckWitness(self, engine): hashOrPubkey = engine.EvaluationStack.Pop().GetByteArray() if len(hashOrPubkey) == 66 or len(hashOrPubkey) == 40: hashOrPubkey = binascii.unhexlify(hashOrPubkey) result = False if len(hashOrPubkey) == 20: result = self.CheckWitnessHash(engine, UInt160(data=hashOrPubkey)) elif len(hashOrPubkey) == 33: point = ECDSA.decode_secp256r1(hashOrPubkey, unhex=False).G result = self.CheckWitnessPubkey(engine, point) else: result = False engine.EvaluationStack.PushT(result) return True
def CreateMultiSigContract(publicKeyHash, m, publicKeys): pk = [ECDSA.decode_secp256r1(p).G for p in publicKeys] return Contract(Contract.CreateMultiSigRedeemScript(m, pk), bytearray([ContractParameterType.Signature] * 3), publicKeyHash)
def Asset_Create(self, engine): tx = engine.ScriptContainer asset_type = int(engine.EvaluationStack.Pop().GetBigInteger()) if asset_type not in AssetType.AllTypes() or \ asset_type == AssetType.CreditFlag or \ asset_type == AssetType.DutyFlag or \ asset_type == AssetType.GoverningToken or \ asset_type == AssetType.UtilityToken: return False if len(engine.EvaluationStack.Peek().GetByteArray()) > 1024: return False name = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') amount = Fixed8(engine.EvaluationStack.Pop().GetBigInteger()) if amount == Fixed8.Zero() or amount < Fixed8.NegativeSatoshi(): return False if asset_type == AssetType.Invoice and amount != Fixed8.NegativeSatoshi( ): return False precision = int(engine.EvaluationStack.Pop().GetBigInteger()) if precision > 8: return False if asset_type == AssetType.Share and precision != 0: return False if amount != Fixed8.NegativeSatoshi() and amount.value % pow( 10, 8 - precision) != 0: return False ownerData = engine.EvaluationStack.Pop().GetByteArray() owner = ECDSA.decode_secp256r1(ownerData, unhex=False).G if owner.IsInfinity: return False if not self.CheckWitnessPubkey(engine, owner): logger.error("check witness false...") return False admin = UInt160(data=engine.EvaluationStack.Pop().GetByteArray()) issuer = UInt160(data=engine.EvaluationStack.Pop().GetByteArray()) new_asset = AssetState(asset_id=tx.Hash, asset_type=asset_type, name=name, amount=amount, available=Fixed8.Zero(), precision=precision, fee_mode=0, fee=Fixed8.Zero(), fee_addr=UInt160(), owner=owner, admin=admin, issuer=issuer, expiration=Blockchain.Default().Height + 1 + 2000000, is_frozen=False) asset = self._assets.GetOrAdd(tx.Hash.ToBytes(), new_asset) # print("*****************************************************") # print("CREATED ASSET %s " % tx.Hash.ToBytes()) # print("*****************************************************") engine.EvaluationStack.PushT(StackItem.FromInterface(asset)) return True
def CreateMultiSigContract(publicKeyHash, m, publicKeys): pk = [ECDSA.decode_secp256r1(p).G for p in publicKeys] return Contract(Contract.CreateMultiSigRedeemScript(m, pk), bytearray(b'\x00\x00\x00'), publicKeyHash)
def Asset_Create(self, engine): tx = engine.ScriptContainer asset_type = int(engine.EvaluationStack.Pop().GetBigInteger()) if asset_type not in AssetType.AllTypes() or \ asset_type == AssetType.CreditFlag or \ asset_type == AssetType.DutyFlag or \ asset_type == AssetType.GoverningToken or \ asset_type == AssetType.UtilityToken: return False if len(engine.EvaluationStack.Peek().GetByteArray()) > 1024: return False name = engine.EvaluationStack.Pop().GetByteArray().decode('utf-8') amount = Fixed8(engine.EvaluationStack.Pop().GetBigInteger()) if amount == Fixed8.Zero() or amount < Fixed8.NegativeSatoshi(): return False if asset_type == AssetType.Invoice and amount != Fixed8.NegativeSatoshi(): return False precision = int(engine.EvaluationStack.Pop().GetBigInteger()) if precision > 8: return False if asset_type == AssetType.Share and precision != 0: return False if amount != Fixed8.NegativeSatoshi() and amount.value % pow(10, 8 - precision) != 0: return False ownerData = engine.EvaluationStack.Pop().GetByteArray() owner = ECDSA.decode_secp256r1(ownerData, unhex=False).G if owner.IsInfinity: return False if not self.CheckWitnessPubkey(engine, owner): logger.error("check witness false...") return False admin = UInt160(data=engine.EvaluationStack.Pop().GetByteArray()) issuer = UInt160(data=engine.EvaluationStack.Pop().GetByteArray()) new_asset = AssetState( asset_id=tx.Hash, asset_type=asset_type, name=name, amount=amount, available=Fixed8.Zero(), precision=precision, fee_mode=0, fee=Fixed8.Zero(), fee_addr=UInt160(), owner=owner, admin=admin, issuer=issuer, expiration=Blockchain.Default().Height + 1 + 2000000, is_frozen=False ) asset = self._assets.GetOrAdd(tx.Hash.ToBytes(), new_asset) # print("*****************************************************") # print("CREATED ASSET %s " % tx.Hash.ToBytes()) # print("*****************************************************") engine.EvaluationStack.PushT(StackItem.FromInterface(asset)) return True
def CreateMultiSigContract(publicKeyHash, m, publicKeys): pk = [ECDSA.decode_secp256r1(p).G for p in publicKeys] return Contract(Contract.CreateMultiSigRedeemScript(m, pk), bytearray(m), publicKeyHash)