如何使用 Web3 访问受访问权限保护的智能合约功能?
Posted
技术标签:
【中文标题】如何使用 Web3 访问受访问权限保护的智能合约功能?【英文标题】:How to access a smart contract function protected by access rights using Web3? 【发布时间】:2019-09-12 06:07:04 【问题描述】:我有一个安全令牌的智能合约地址,它的某些功能受访问权限的保护,我有一个地址可以访问这些功能,但是我不知道如何通过以下方式调用该功能指定权限。
from web3 import HTTPProvider, Web3, exceptions
w3 = Web3(HTTPProvider('https://ropsten.infura.io/RPw9nHRS7Ue47RaKVvHM'))
contract_address = '0x635209612bf0e830ac348ef30357ee4f0e5bf560'
provider_abi = ["anonymous":False,"inputs":["indexed":False,"name":"addr","type":"address","indexed":False,"name":"propertyKey","type":"bytes32","indexed":False,"name":"propertyValue","type":"bytes32"],"name":"PropertySet","type":"event","constant":False,"inputs":["name":"_addr","type":"address","name":"_propertyKey","type":"bytes32","name":"_propertyValue","type":"bytes32"],"name":"setProperty","outputs":["name":"","type":"bool"],"payable":False,"stateMutability":"nonpayable","type":"function","constant":True,"inputs":["name":"_addr","type":"address","name":"_propertyKey","type":"bytes32"],"name":"getProperty","outputs":["name":"","type":"bytes32"],"payable":False,"stateMutability":"view","type":"function"]
instance = w3.eth.contract(
address=Web3.toChecksumAddress(contract_address),
abi = provider_abi
)
user_address = "0x25BEADE120E501D7b984498D196eFe4AbE6a11F6"
country_key = "country"
country_byte_32 = Web3.toHex(Web3.toBytes(text=country_key))
print(country_byte_32) # Prints 0x636f756e747279
country_val = "IN"
country_val_byte_32 = Web3.toHex(Web3.toBytes(text=country_val))
print(country_val_byte_32) # Prints 0x494e
try:
result = instance.call().setProperty(user_address,country_byte_32,country_val_byte_32)
print(result) # Prints False
except exceptions.MismatchedABI as ve :
print(ve)
import traceback
print(traceback.format_exc())
谁能告诉我,如何提供访问权限地址?
【问题讨论】:
你能分享你的solidity代码吗? 我没有solidity代码,因为合同不是我的,但我有abi。 你能仔细检查一下语句 instance.call().setProperty(user_address,country_byte_32,country_val_byte_32) 。因为我认为调用方法应该在函数名之后。 合约创建者要求我传递 from 属性,因为该函数受访问权限保护。我不确定如何以及在哪里通过它。两个版本都可以,调用方法也可以传入end,它也返回False 【参考方案1】:要将值放入表单字段中,您可以这样做
result = instance.call("from": user_address ).setProperty(user_address,country_byte_32,country_val_byte_32)
【讨论】:
以上是关于如何使用 Web3 访问受访问权限保护的智能合约功能?的主要内容,如果未能解决你的问题,请参考以下文章
php curl - 需要使用 curl 提供对受保护目录的访问权限