hi,我在尝试将以太坊地址转换到godwoken地址,参考了RFC0008,但是转换出来的地址一只不对,有人知道如何解决吗?
测试使用的是这里的例子,以太坊地址是0xd173313a51f8fc37bcf67569b463abd89d81844f
, godwoken短地址应该是0x8016dcd1af7c8cceda53e4d2d2cd4e2924e245b6
.
下面是我的代码:
import hashlib
# ethAccountLockCodeHash = "0x1563080d175bf8ddd44a48e850cecf0c0b4575835756eb5ffd53ad830931b9f9"
# rollupTypeHash = "0x40d73f0d3c561fcaae330eabc030d8d96a9d0af36d0c5114883658a350cb9e3b"
ethAccountLockCodeHash = "0x4cc2e6526204ae6a2e8fcf12f7ad472f41a1606d5b9624beebd215d780809f6a"
rollupTypeHash = "0xdeec13a7b8e100579541384ccaf4b5223733e4a5483c3aec95ddc4c1d5ea5b22"
def serialize_script(value):
code_hash = bytes(bytearray.fromhex(value['code_hash'][2:]))
# code_hash = int.to_bytes(len(code_hash), 4, byteorder='little') + code_hash
ty = int.to_bytes(value['hash_type'], 1, byteorder='little')
args = bytes(bytearray.fromhex(value['args'][2:]))
args = int.to_bytes(len(args), 4, byteorder='little') + args
totalSize = 4 * (3 + 1)
totalSize += len(code_hash) + len(ty) + len(args)
offsets = [16, 16+len(code_hash), 16+len(code_hash)+len(ty)]
ret = b''
ret += int.to_bytes(totalSize, 4, byteorder='little')
ret += int.to_bytes(offsets[0], 4, byteorder='little')
ret += int.to_bytes(offsets[1], 4, byteorder='little')
ret += int.to_bytes(offsets[2], 4, byteorder='little')
return ret + code_hash + ty + args
def get_eth_account_lock(addr):
return {
'code_hash': ethAccountLockCodeHash,
'hash_type': 1, #'type',
'args': rollupTypeHash + addr[2:]
}
def get_script_hash(script):
b = serialize_script(script)
p = 'ckb-default-hash'
h = hashlib.blake2b(digest_size=32, person=p.encode())
h.update(b)
return h.hexdigest()
# convert ethereum EOA to godwoken short address
def to_gw_addr(addr):
lock = get_eth_account_lock(addr.lower())
scriptHash = get_script_hash(lock)
return '0x' + scriptHash[:40]
万分感谢!!!