Edit an account

Edit an account in Bitsocial React Hooks. Usage, options, and examples.

import {useAccount, setAccount, useResolvedAuthorAddress} from '@bitsocial/bitsocial-react-hooks'
const account = useAccount() // or useAccount({ accountName: 'Account KoXpxTwfnjA5' }) to use an account by name

// `account.author.wallets` only auto-generates an `eth` wallet by default, derived from the account's
// key, with a BSIP-22 proof over the key address (account.signer.address), so it stays valid when
// author.address changes to a name. When accounts load, including the reload setAccount does after
// saving, an eth wallet whose proof is invalid (e.g. signed before BSIP-22) is replaced by the
// built-in wallet, freshly signed, so publishing keeps working; a wallet the user signed elsewhere
// (e.g. 5chan Pass) has to be signed again to get it back. A proof dated ahead of the device's clock
// is kept. The repair is best effort: if it fails (a malformed key, or the proof code failing to
// load), the wallet stays as it is and publishing can fail until a later load repairs it.
// Validate a wallet with validateEthWallet before saving it.
// `account.chainProviders` is the canonical chain config for wallets, NFT lookups, and other chain reads.
// Defaults use multiple explicit Ethereum RPCs for `.bso` author-name resolution.
// Web defaults run a browser libp2p client and use multiple HTTP routers to discover peers.
// IPFS gateways and HTTP pubsub providers remain available as explicit account or app overrides.
// `account.nameResolversChainProviders` optionally overrides only the RPCs used for that resolution.
console.log(account.author.wallets.eth)

const ethResolverRpcUrls = [
  'https://ethereum-rpc.publicnode.com',
  'https://eth.drpc.org',
  'https://ethereum.publicnode.com',
  'https://rpc.mevblocker.io',
  'https://1rpc.io/eth',
  'https://eth-pokt.nodies.app',
]

const httpRoutersOptions = [
  'https://peers.pleb.bot',
  'https://routing.lol',
  'https://peers.forumindex.com',
  'https://peers.plebpubsub.xyz',
  'https://routerofbitsocial.xyz',
  'https://bsotracker.online',
]

const author: {...account.author, displayName: 'John'}
const editedAccount = {
  ...account,
  author,
  chainProviders: {
    ...account.chainProviders,
    eth: { urls: [...ethResolverRpcUrls, 'ethers.js'], chainId: 1 },
  },
  pkcOptions: {
    ...account.pkcOptions,
    httpRoutersOptions,
  },
  nameResolversChainProviders: {
    eth: { urls: ethResolverRpcUrls, chainId: 1 },
  },
}

await setAccount(editedAccount)

// domain names must be lowercase: setAccount throws an error with code 'ERR_AUTHOR_NAME_HAS_CAPITAL_LETTER'
// when author.name or author.address is set to a name like 'Username.bso', and createCommunity and
// publishCommunityEdit throw 'ERR_COMMUNITY_NAME_HAS_CAPITAL_LETTER' for a community name like 'MyBoard.bso'

// check if the user has set their .bso author name properly, use {cache: false} or it won't update
const author = {...account.author, address: 'username.bso'}
// authorAddress should equal to account.signer.address
const {resolvedAddress, state, error, chainProvider, nameResolver} = useResolvedAuthorAddress({author, cache: false})

// result
if (state === 'succeeded') {
  console.log('Succeeded resolving address', resolvedAddress)
}
if (state === 'failed') {
  console.log('Failed resolving address', error.message)
}

// pending, only until the name has a result: later polls with {cache: false} keep the last result
if (state === 'resolving' && nameResolver) {
  console.log(`Resolving ${nameResolver.nameSystem} address from ${nameResolver.providerLabel}`)
  console.log('Matching chain provider URLs', chainProvider?.urls)
}

Migrating to 0.4.0. Communities on pkc-js 0.0.155 or later reject a publication carrying an author.wallets proof that doesn’t follow BSIP-22, and 0.4.0 signs and checks proofs that way. validateEthWallet(wallet, authorPublicKey) now takes the key address of the account’s signing key (account.signer.address, 12D3KooW...) instead of author.address, and rejects a proof in the old format. To sign an external wallet in, have it sign getWalletMessageToSign(account.signer.address, walletAddress, timestamp), imported from @bitsocial/bitsocial-react-hooks/author-wallet-proof. When accounts load, an eth wallet whose proof is invalid is replaced by the built-in wallet, so a user signed in to an external wallet in the old format is signed out of it and signs again.

From 0.4.3, which runs pkc-js 0.0.163, a publish whose author.wallets has an entry BSIP-22 finds invalid also fails on the device, before anything is sent: publish() rejects with the code ERR_AUTHOR_WALLET_PROOF_IS_INVALID, so onError gets it as a final error and the publish ends failed. Only the eth entry is repaired when accounts load; sign any other wallet an account publishes, under any other key, with getWalletMessageToSign.