压缩公钥与非压缩公钥的转换及应用

Qitmeer 的地址生成使用的是压缩公钥通过 HASH160 (ripemd160(blake2b256(data)))计算得到的,这一点和BTC类似。

有趣的是,Qitmeer、BTC 和 ETH 的地址都是使用公钥通过不同的哈希算法得到,类似下面的列子:

publickey = 0291560b870b74930ffc8d06d874bc0329f8d24897a8967ec9dfb79d73226aaca2

Qitmeer Address 为:

qx hash160 0291560b870b74930ffc8d06d874bc0329f8d24897a8967ec9dfb79d73226aaca2 | qx base58check-encode

~ TmdC2pcpL6UmEPeB7eM7jZCtbYNoS5fAmVx

BTC Address 为:

bx bitcoin160 0291560b870b74930ffc8d06d874bc0329f8d24897a8967ec9dfb79d73226aaca2 | bx base58check-encode

~ 18sK9rfS8tgmrPQVEw4wsi5Nxztkz7yprn

ETH Address 为:

qx keccak-256 91560b870b74930ffc8d06d874bc0329f8d24897a8967ec9dfb79d73226aaca2cf9f98b8218a50596b2e2233d7683afc743c431fa5bd4afc21fd6366bccd7da8

# 取后 80 个字节
~ 0xa506ed95b3bd137c542a8406e094b3e494d08a4f

可以看到只要知道公钥就可以在不知道秘钥的情况下推导出对应的 BTC 和 ETH 地址,这样可以简化原子交换的用户交互步骤。

在原来的原子交换中设计中,合约用到的是 publicKeyHash :

OP_DROP OP_DUP OP_HASH160  < publicKeyHash >  OP_ENDIF OP_EQUALVERIFY

如果与 ETH 做交换我们需要对方提供 ETH 地址,这增加了交互流程,我们可以利用公钥共享这个特点直接从 Qitmeer 交易推算到对方的 ETH 地址,这节省了操作。

举个例子:

在交易 75e61c254429c54eaf16eb18f060f439f453018093d28fa40583d7872192e123 中:

{
    "hex": "0100000002fb3638063dfa5f028328ff291bf2d3707ffdc774e9509fb63c8d62b2485bdf3801000000fffffffff089a6c449bef1c669a7b1342674c9096e13fa8e24db615205aee35e45bf0c2c01000000ffffffff026043993b000000001976a91463646554c08a60cfbe65931d78b4d6f9bc09827388ac0065cd1d000000001976a914afda839fa515ffdbcbc8630b60909c64cfd73f7a88ac00000000000000000280b2e60e0000000047060000020000006b483045022100f3c7f9ac01ae1d0d7768e44b22cd59e69cfc9388123152adadd4d5ba08be8e5a022020520109cd3ff03242e4615e7a43431ac108ebafbc1b944aa902d307c2619928012103383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593807c814a0000000047060000010000006a4730440220740801fc84c89acd6ec3df07b10d153f93728f59e79271295be22b41cb71da40022000e3c82af16ecc075a4ad80c217039c1db7c338d2d952c62147f476523d1074c012103383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593",
    "hexnowit": "0100010002fb3638063dfa5f028328ff291bf2d3707ffdc774e9509fb63c8d62b2485bdf3801000000fffffffff089a6c449bef1c669a7b1342674c9096e13fa8e24db615205aee35e45bf0c2c01000000ffffffff026043993b000000001976a91463646554c08a60cfbe65931d78b4d6f9bc09827388ac0065cd1d000000001976a914afda839fa515ffdbcbc8630b60909c64cfd73f7a88ac0000000000000000",
    "hexwit": "010002000280b2e60e0000000047060000020000006b483045022100f3c7f9ac01ae1d0d7768e44b22cd59e69cfc9388123152adadd4d5ba08be8e5a022020520109cd3ff03242e4615e7a43431ac108ebafbc1b944aa902d307c2619928012103383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593807c814a0000000047060000010000006a4730440220740801fc84c89acd6ec3df07b10d153f93728f59e79271295be22b41cb71da40022000e3c82af16ecc075a4ad80c217039c1db7c338d2d952c62147f476523d1074c012103383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593",
    "txid": "75e61c254429c54eaf16eb18f060f439f453018093d28fa40583d7872192e123",
    "txhash": "f172761ebd36a1c12617e1bbed3b50cbea1b6e8783a5adab59a367030ce485e9",
    "version": 1,
    "locktime": 0,
    "expire": 0,
    "vin": [
        {
            "txid": "38df5b48b2628d3cb69f50e974c7fd7f70d3f21b29ff2883025ffa3d063836fb",
            "vout": 1,
            "sequence": 4294967295,
            "amountin": 250000000,
            "blockheight": 1607,
            "txindex": 2,
            "scriptSig": {
                "asm": "3045022100f3c7f9ac01ae1d0d7768e44b22cd59e69cfc9388123152adadd4d5ba08be8e5a022020520109cd3ff03242e4615e7a43431ac108ebafbc1b944aa902d307c261992801 03383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593",
                "hex": "483045022100f3c7f9ac01ae1d0d7768e44b22cd59e69cfc9388123152adadd4d5ba08be8e5a022020520109cd3ff03242e4615e7a43431ac108ebafbc1b944aa902d307c2619928012103383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593"
            }
        },
        {
            "txid": "2c0cbf455ee3ae055261db248efa136e09c9742634b1a769c6f1be49c4a689f0",
            "vout": 1,
            "sequence": 4294967295,
            "amountin": 1250000000,
            "blockheight": 1607,
            "txindex": 1,
            "scriptSig": {
                "asm": "30440220740801fc84c89acd6ec3df07b10d153f93728f59e79271295be22b41cb71da40022000e3c82af16ecc075a4ad80c217039c1db7c338d2d952c62147f476523d1074c01 03383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593",
                "hex": "4730440220740801fc84c89acd6ec3df07b10d153f93728f59e79271295be22b41cb71da40022000e3c82af16ecc075a4ad80c217039c1db7c338d2d952c62147f476523d1074c012103383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593"
            }
        }
    ],
    "vout": [
        {
            "amount": 999900000,
            "scriptPubKey": {
                "asm": "OP_DUP OP_HASH160 63646554c08a60cfbe65931d78b4d6f9bc098273 OP_EQUALVERIFY OP_CHECKSIG",
                "hex": "76a91463646554c08a60cfbe65931d78b4d6f9bc09827388ac",
                "reqSigs": 1,
                "type": "pubkeyhash",
                "addresses": [
                    "TmY1csYefT33q9fooXnT6eJprzaVaABfCAz"
                ]
            }
        },
        {
            "amount": 500000000,
            "scriptPubKey": {
                "asm": "OP_DUP OP_HASH160 afda839fa515ffdbcbc8630b60909c64cfd73f7a OP_EQUALVERIFY OP_CHECKSIG",
                "hex": "76a914afda839fa515ffdbcbc8630b60909c64cfd73f7a88ac",
                "reqSigs": 1,
                "type": "pubkeyhash",
                "addresses": [
                    "Tmeyuj8ZBaQC8F47wNKxDmYAWUFti3XMrLb"
                ]
            }
        }
    ],
    "blockhash": "0000000016d2ee5980bb17364644890d10ee2a6898a9f479724677fddcc42ba8",
    "blockheight": 1607,
    "confirmations": 37
}

TmY1csYefT33q9fooXnT6eJprzaVaABfCAz 地址向 Tmeyuj8ZBaQC8F47wNKxDmYAWUFti3XMrLb 支付了 5 个 MEER,在 vin 字段中, 我们得到了 TmY1csYefT33q9fooXnT6eJprzaVaABfCAz 地址的公钥 03383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b593,将该公钥转换为非压缩公钥 :
04383b0e1f1e92bd9cd26d961169b68944899c1600c8cb23757c25313b0091b5933419a5918b498140189a8571bcbc0676c59713b967df7267869a0a840ff6f76d,从而推出 TmY1csYefT33q9fooXnT6eJprzaVaABfCAz 对应的 ETH 地址为:0xc5de620b3b1d0501dacb9723ce3157218bc1c87f,这样在做跨 ETH 的交换时,可以直接将 ETH 合约绑定到该地址,这一切和在客户端自动实现,从而简化了交互。

1 Like