diff --git a/names-web/README.md b/names-web/README.md index 5211de6a1..1915e725e 100644 --- a/names-web/README.md +++ b/names-web/README.md @@ -1,6 +1,6 @@ # names-web -Prototype page for buying a SimpleX name in the browser, without a wallet extension. It makes a wallet for the name, shows its redemption code, and prepares the two requests to the service: the commit request with the commitment, and the register request with the registration and the owner's signed `RegisterName` intent. +Prototype page for buying a SimpleX name in the browser, without a wallet extension. It makes a wallet for the name, shows its redemption code, address and recovery phrase, and prepares the two requests to the service: the commit request with the commitment, and the register request with the registration and the owner's signed `RegisterName` intent. ## Setup diff --git a/names-web/src/wallet.ts b/names-web/src/wallet.ts index 12dd14409..53f5c0191 100644 --- a/names-web/src/wallet.ts +++ b/names-web/src/wallet.ts @@ -1,4 +1,5 @@ import {secp256k1} from "@noble/curves/secp256k1" +import {bytesToHex, utf8ToBytes} from "@noble/curves/utils" import {keccak_256} from "@noble/hashes/sha3" import {HDKey} from "@scure/bip32" import {entropyToMnemonic, mnemonicToSeedSync} from "@scure/bip39" @@ -6,15 +7,23 @@ import {wordlist} from "@scure/bip39/wordlists/english" export interface Wallet { entropy: Uint8Array + mnemonic: string privateKey: Uint8Array address: Uint8Array } export function walletFromEntropy(entropy: Uint8Array): Wallet { - const seed = mnemonicToSeedSync(entropyToMnemonic(entropy, wordlist)) + const mnemonic = entropyToMnemonic(entropy, wordlist) + const seed = mnemonicToSeedSync(mnemonic) const privateKey = HDKey.fromMasterSeed(seed).derive("m/44'/60'/0'/0/0").privateKey! const address = keccak_256(secp256k1.getPublicKey(privateKey, false).slice(1)).slice(12) - return {entropy, privateKey, address} + return {entropy, mnemonic, privateKey, address} +} + +export function checksumAddress(address: Uint8Array): string { + const hex = bytesToHex(address) + const hash = bytesToHex(keccak_256(utf8ToBytes(hex))) + return "0x" + [...hex].map((c, i) => (parseInt(hash[i], 16) >= 8 ? c.toUpperCase() : c)).join("") } export function signDigest(privateKey: Uint8Array, digest: Uint8Array): Uint8Array { diff --git a/names-web/test/page.spec.ts b/names-web/test/page.spec.ts index 029be3709..33becc0e2 100644 --- a/names-web/test/page.spec.ts +++ b/names-web/test/page.spec.ts @@ -1,5 +1,5 @@ import {expect, test} from "@playwright/test" -import {bytesToHex, hexToBytes} from "viem" +import {bytesToHex, getAddress, hexToBytes} from "viem" import {parseNameCode} from "../src/code" import {makeCommitment, registerIntentDigest, type Registration} from "../src/registration" import {signDigest, walletFromEntropy} from "../src/wallet" @@ -16,6 +16,8 @@ test("prepares a commitment and a registration signed by the code's wallet", asy const request = JSON.parse(await page.locator("#register-request").innerText()) const wallet = walletFromEntropy(parseNameCode(code)!) + expect(await page.locator("#address").innerText()).toBe(getAddress(bytesToHex(wallet.address))) + expect(await page.locator("#mnemonic").innerText()).toBe(wallet.mnemonic) expect(request).toEqual({ label: "bakery1", tld, diff --git a/names-web/web/index.html b/names-web/web/index.html index e4634614e..b54f1734b 100644 --- a/names-web/web/index.html +++ b/names-web/web/index.html @@ -9,6 +9,8 @@

SimpleX Names

Your redemption code:

+

Wallet address:

+

Recovery phrase:

reload will destroy it and generate a new one

diff --git a/names-web/web/main.ts b/names-web/web/main.ts index fff039ee5..a7d28868a 100644 --- a/names-web/web/main.ts +++ b/names-web/web/main.ts @@ -1,11 +1,13 @@ import {bytesToHex} from "@noble/curves/utils" import {formatNameCode} from "../src/code" import {makeCommitment, registerIntentDigest, type Registration} from "../src/registration" -import {signDigest, walletFromEntropy} from "../src/wallet" +import {checksumAddress, signDigest, walletFromEntropy} from "../src/wallet" import {chainId, duration, minLabelLength, resolver, sponsor, tld} from "./config" const wallet = walletFromEntropy(crypto.getRandomValues(new Uint8Array(16))) element("code").textContent = formatNameCode(wallet.entropy) +element("address").textContent = checksumAddress(wallet.address) +element("mnemonic").textContent = wallet.mnemonic element("register").addEventListener("click", () => { element("commit-request").textContent = "" diff --git a/tests/NamesWebTests.hs b/tests/NamesWebTests.hs index 7d757c937..4087bdde2 100644 --- a/tests/NamesWebTests.hs +++ b/tests/NamesWebTests.hs @@ -20,7 +20,8 @@ import Simplex.Messaging.Crypto.BIP32 (masterKey, xkKey) import Simplex.Messaging.Crypto.BIP39 (EntropyStrength (..), entropySeed, randomEntropy, unEntropy) import Simplex.Messaging.Crypto.BIP44 (CoinType (..), bip44Path, mkAccountIndex) import qualified Simplex.Messaging.Crypto.Secp256k1 as S -import Simplex.Messaging.Eth.Address (deriveAddress, unAddress) +import Simplex.Messaging.Encoding.String (strEncode) +import Simplex.Messaging.Eth.Address (deriveAddress) import Simplex.Messaging.Eth.EIP712 import System.Directory (doesDirectoryExist) import System.Process (CreateProcess (..), proc, readCreateProcess) @@ -52,7 +53,7 @@ testWallet g = digest <- atomically $ C.randomBytes 32 g signature <- either error id <$> S.signRecoverable g (xkKey xk) digest let jsWallet = "walletFromEntropy(" <> jsUint8 (BA.convert $ unEntropy entropy) <> ")" - callNode (jsWallet <> ".address") `shouldReturn` hex (unAddress addr) + callNode ("checksumAddress(" <> jsWallet <> ".address)") `shouldReturn` BC.unpack (strEncode addr) callNode ("signDigest(" <> jsWallet <> ".privateKey, " <> jsUint8 digest <> ")") `shouldReturn` hex (serializeSignature signature) testNameCodes :: IO () @@ -93,7 +94,7 @@ callNode expr = imports = "import {formatNameCode, parseNameCode} from './dist/code.js';" <> "import {makeCommitment, registerIntentDigest} from './dist/registration.js';" - <> "import {signDigest, walletFromEntropy} from './dist/wallet.js';" + <> "import {checksumAddress, signDigest, walletFromEntropy} from './dist/wallet.js';" jsUint8 :: ByteString -> String jsUint8 bs = "new Uint8Array([" <> intercalate "," (map show $ B.unpack bs) <> "])"