def register(file, account): """Register a media asset.""" ic = ipfs_client() iscc_result = lib.iscc_from_file(file) iscc_code = iscc_result.pop("iscc") fname = basename(file.name) log.info(f"ISCC-CODE for {fname}: {iscc_code}") # Check for probable ISCC-ID w3 = w3_client() addr = w3.eth.accounts[account] iscc_id = find_next(RegEntry(iscc=iscc_code, actor=addr)) log.info(f"ISCC-ID will probably be: {iscc_id}") iscc_result["filename"] = fname authority = iscc_registry.settings.verification_domain if authority: log.debug(f"Set domain to {authority} in metadata.") iscc_result["domain"] = authority meta_blob = cbor2.dumps(iscc_result) ipfs_result = ic.add(io.BytesIO(meta_blob)) ipfs_cid = ipfs_result["Hash"] log.info(f"CID for {fname} metadata: {ipfs_cid}") if click.confirm(f"Register ISCC-CODE?"): txid = publish(iscc_code, ipfs_cid, account) click.echo(f"Registered ISCC-CODE: {iscc_code}") click.echo(f"Actor wallet address: {addr}") click.echo(f"Registration TX-ID: {txid}") click.echo(f"Probable ISCC-ID: {iscc_id}")
def deploy(account=0): w3 = w3_client() w3.eth.defaultAccount = w3.eth.accounts[account] contract_obj = get_contract() tx_hash = contract_obj.constructor().transact() address = w3.eth.getTransactionReceipt(tx_hash)["contractAddress"] log.debug(f"Contract deployed to {address}") return address
def create_proof(domain: str, address: str) -> str: w3 = w3_client() sig = w3.eth.sign(address, text=domain) proof = { "address": address, "domain": domain, "signature": sig.hex(), } log.info(f"Signing {domain} with address {address}") return json.dumps(proof, indent=2)
def deploy(): """Deploy ISCC Registry contract to chain.""" w3 = w3_client() if click.confirm(f"Deploy registry contract to {chain_name(w3)}"): addr = deploy_contract() click.echo(f"ISCC Registry contract deployed to {addr}") if click.confirm(f"Save contract address to {iscc_registry.ENV_PATH}?"): with open(iscc_registry.ENV_PATH, "wt") as outf: outf.write(f"CONTRACT_ADDRESS={addr}\n") click.echo("Contract address configuration saved.") else: click.echo(f"Remember to set CONTRACT_ADDRESS env variable to {addr}")
def valid(domain: str, address: str, proof: dict) -> bool: w3 = w3_client() if address != proof["address"]: return False if domain != proof["domain"]: return False if not proof.get('signature'): return False msg = encode_defunct(text=proof["domain"]) try: raddr = w3.eth.account.recover_message(msg, signature=proof["signature"]) except Exception: return False if address != raddr: return False return True
def prove(domain, account): """Create domain based self-verification proof""" if not domain: domain = click.prompt( "Domain to prove (example: https://example.com/)") w3 = w3_client() address = w3.eth.accounts[account] proof = create_proof(domain, address) if click.confirm(f"Save {domain} to {iscc_registry.ENV_PATH}?"): with open(iscc_registry.ENV_PATH, "a+") as outf: outf.write(f"VERIFICATION_DOMAIN={domain}\n") click.echo("Self-verification domain saved.") else: click.echo( f"Remember to set VERIFICATION_DOMAIN env variable to {domain}") url = domain + "/iscc-proof.json" click.echo( f"Publish the following data at {url} to validate your account {address}:" ) click.echo(proof)
def get_contract(): """Build registry contract object.""" return w3_client().eth.contract(**compile_registry())