Пример #1
0
    def __init__(self,
                 password: str,
                 private_key: Optional[bytes] = None,
                 watch_only: bool = False,
                 address: Optional[str] = None,
                 label: Optional[str] = None,
                 lock: bool = False,
                 contract: Optional[contracts.Contract] = None,
                 extra: Optional[Dict[str, Any]] = None):
        """
        Instantiate an account. This constructor should only be directly called when it's desired to create a new
        account using just a password and a randomly generated private key, otherwise use the alternative constructors
        """
        public_key: Optional[cryptography.ECPoint] = None
        encrypted_key: Optional[bytes] = None
        contract_script: Optional[bytes] = None

        if watch_only:
            if address is None:
                raise ValueError(
                    "Creating a watch only account requires an address")
            else:
                self.validate_address(address)

        else:
            key_pair: cryptography.KeyPair

            if private_key is None:
                key_pair = cryptography.KeyPair.generate()
                private_key = key_pair.private_key
            else:
                key_pair = cryptography.KeyPair(private_key)
            encrypted_key = self.private_key_to_nep2(private_key, password)
            contract_script = contracts.Contract.create_signature_redeemscript(
                key_pair.public_key)
            script_hash = to_script_hash(contract_script)
            address = address if address else self.script_hash_to_address(
                script_hash)
            public_key = key_pair.public_key

        self.label: Optional[str] = label
        self.address: str = address
        self.public_key = public_key
        self.encrypted_key = encrypted_key
        self.lock = lock

        if not isinstance(contract, AccountContract):
            if contract is not None:
                contract = AccountContract.from_contract(contract)
            elif contract_script is not None:
                default_parameters_list = [
                    contracts.ContractParameterDefinition(
                        name='signature',
                        type=contracts.ContractParameterType.SIGNATURE)
                ]
                contract = AccountContract(contract_script,
                                           default_parameters_list)

        self.contract: Optional[AccountContract] = contract
        self.extra = extra if extra else {}
Пример #2
0
    def init(self):
        super(NonFungibleToken, self).init()
        self.key_total_suppply = storage.StorageKey(self._id, b'\x0b')
        self.key_token = storage.StorageKey(self._id, b'\x05')
        self.key_account = storage.StorageKey(self._id, b'\x07')

        self.manifest.abi.events = [
            contracts.ContractEventDescriptor(
                "Transfer",
                parameters=[
                    contracts.ContractParameterDefinition("from", contracts.ContractParameterType.HASH160),
                    contracts.ContractParameterDefinition("to", contracts.ContractParameterType.HASH160),
                    contracts.ContractParameterDefinition("amount", contracts.ContractParameterType.INTEGER),
                    contracts.ContractParameterDefinition("tokenId", contracts.ContractParameterType.BYTEARRAY)
                ]
            )
        ]
Пример #3
0
    def init(self):
        super(FungibleToken, self).init()

        self.key_account = storage.StorageKey(self._id, b'\x14')
        self.key_total_supply = storage.StorageKey(self._id, b'\x0B')

        self.manifest.supported_standards = ["NEP-17"]
        self.manifest.abi.events = [
            contracts.ContractEventDescriptor(
                "Transfer",
                parameters=[
                    contracts.ContractParameterDefinition("from", contracts.ContractParameterType.HASH160),
                    contracts.ContractParameterDefinition("to", contracts.ContractParameterType.HASH160),
                    contracts.ContractParameterDefinition("amount", contracts.ContractParameterType.INTEGER)
                ]
            )
        ]
        self.factor = pow(vm.BigInteger(10), vm.BigInteger(self._decimals))
Пример #4
0
    def from_contract(cls, contract: contracts.Contract) -> AccountContract:
        if isinstance(contract, AccountContract):
            return contract

        parameters = [
            contracts.ContractParameterDefinition(
                f"arg{index}", contract.parameter_list[index])
            for index in range(len(contract.parameter_list))
        ]
        return cls(script=contract.script, parameter_list=parameters)
Пример #5
0
 def init(self):
     super(DesignationContract, self).init()
     self.manifest.abi.events = [
         contracts.ContractEventDescriptor(
             "Designation",
             parameters=[
                 contracts.ContractParameterDefinition(
                     "Role", contracts.ContractParameterType.INTEGER),
                 contracts.ContractParameterDefinition(
                     "BlockIndex", contracts.ContractParameterType.INTEGER),
             ]),
         contracts.ContractEventDescriptor(
             "OracleResponse",
             parameters=[
                 contracts.ContractParameterDefinition(
                     "Id", contracts.ContractParameterType.INTEGER),
                 contracts.ContractParameterDefinition(
                     "OriginalTx", contracts.ContractParameterType.HASH160)
             ])
     ]
Пример #6
0
 def setUpClass(cls) -> None:
     cls.parameters = [
         contracts.ContractParameterDefinition(
             "param", contracts.ContractParameterType.STRING)
     ]
     cls.expected = {
         "name": "MainMethod",
         "offset": 0,
         "parameters": [cls.parameters[0].to_json()],
         "returntype": "Boolean"
     }
Пример #7
0
 def setUpClass(cls) -> None:
     cls.expected = {
         "name": "MainEvent",
         "parameters": [{
             "name": "param",
             "type": "String"
         }]
     }
     cls.parameters = [
         contracts.ContractParameterDefinition(
             "param", contracts.ContractParameterType.STRING)
     ]
Пример #8
0
 def init(self):
     super(OracleContract, self).init()
     self.manifest.abi.events = [
         contracts.ContractEventDescriptor(
             "OracleRequest",
             parameters=[
                 contracts.ContractParameterDefinition(
                     "Id", contracts.ContractParameterType.INTEGER),
                 contracts.ContractParameterDefinition(
                     "RequestContract",
                     contracts.ContractParameterType.HASH160),
                 contracts.ContractParameterDefinition(
                     "Url", contracts.ContractParameterType.STRING),
                 contracts.ContractParameterDefinition(
                     "Filter", contracts.ContractParameterType.STRING)
             ]),
         contracts.ContractEventDescriptor(
             "OracleResponse",
             parameters=[
                 contracts.ContractParameterDefinition(
                     "Id", contracts.ContractParameterType.INTEGER),
                 contracts.ContractParameterDefinition(
                     "OriginalTx", contracts.ContractParameterType.HASH160)
             ])
     ]
Пример #9
0
    def init(self):
        super(ManagementContract, self).init()

        self.manifest.abi.events = [
            contracts.ContractEventDescriptor(
                "Deploy",
                parameters=[
                    contracts.ContractParameterDefinition(
                        "Hash", contracts.ContractParameterType.HASH160)
                ]),
            contracts.ContractEventDescriptor(
                "Update",
                parameters=[
                    contracts.ContractParameterDefinition(
                        "Hash", contracts.ContractParameterType.HASH160)
                ]),
            contracts.ContractEventDescriptor(
                "Destroy",
                parameters=[
                    contracts.ContractParameterDefinition(
                        "Hash", contracts.ContractParameterType.HASH160)
                ]),
        ]
Пример #10
0
    def __init__(self, func: Callable):
        self.handler = func
        self.name: str = func.name  # type:ignore
        self.cpu_price: int = func.cpu_price  # type: ignore
        self.storage_price: int = func.storage_price  # type: ignore
        self.required_flags: contracts.CallFlags = func.flags  # type: ignore
        self.add_engine = False
        self.add_snapshot = False
        self.return_type = None

        parameter_types = []
        parameter_names = []
        for k, v in get_type_hints(func).items():
            if k == 'return':
                if v != type(None):
                    self.return_type = v
                continue
            if v == contracts.ApplicationEngine:
                self.add_engine = True
                continue
            elif v == storage.Snapshot:
                self.add_snapshot = True
                continue
            parameter_types.append(v)
            parameter_names.append(k)

        params = []
        for t, n in zip(parameter_types, parameter_names):
            params.append(
                contracts.ContractParameterDefinition(
                    name=n, type=contracts.ContractParameterType.from_type(t)))
        self.parameter_types = parameter_types
        self.descriptor = contracts.ContractMethodDescriptor(
            name=func.name,  # type: ignore
            offset=0,
            return_type=contracts.ContractParameterType.from_type(
                self.return_type),
            parameters=params,
            safe=(func.flags & ~contracts.CallFlags.READ_ONLY) ==
            0  # type: ignore
        )
Пример #11
0
 def test_to_json(self):
     cpd = contracts.ContractParameterDefinition(
         "Main", contracts.ContractParameterType.STRING)
     self.assertEqual(self.expected, cpd.to_json())