ckb-js / nexus

A UTxO friendly wallet extension for CKB
MIT License
15 stars 11 forks source link

How to get a CKB address through RPC interface? #238

Closed ahonn closed 1 year ago

ahonn commented 1 year ago

It seems that the CKB address is not displayed in the extension, and no method for returning the address was found in the provided RPC interface.

homura commented 1 year ago

You can try the getOffChainLocks or getOnChainLocks

await window.ckb.request({ 
  method: 'wallet_fullOwnership_getOffChainLocks', 
  params: { change: 'internal' } 
});

To parse a lock to an address, we can use the @ckb-lumos/helpers

import { encodeToAddress } from '@ckb-lumos/helpers'

console.log(encodeToAddress(fetchedScript))
ahonn commented 1 year ago

Thanks, And it would be even better if encodeToAddress or method like wallet_getAddress could be built-in.

homura commented 1 year ago

Thanks, And it would be even better if encodeToAddress or method like wallet_getAddress could be built-in.

Address is indeed easier to understand with, but the address is relative, the mainnet address is different from other networks, so to keep return of getXLocks be correct, lock may be a better choice. Even more, we recommend to de-emphasize the concept of the address in dApp and use nickname instead, because in the UTxO model, addresses can easily change over time