mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-23 06:49:21 +00:00
print mnemonic and wallet address on UI
This commit is contained in:
1 parent
a5f9b3581e
commit
f9204e6801
6 files changed
+24
-8
No files matched your search
+1
-1
@@ -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
|
||||
|
||||
|
||||
+11
-2
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
<body>
|
||||
<h1>SimpleX Names</h1>
|
||||
<p>Your redemption code: <code id="code"></code></p>
|
||||
<p>Wallet address: <code id="address"></code></p>
|
||||
<p>Recovery phrase: <code id="mnemonic"></code></p>
|
||||
<p><i>reload will destroy it and generate a new one</i></p>
|
||||
<p><label>Name <input id="label">.simplex</label></p>
|
||||
<p><label>Contact link <input id="contact" pattern="[^;]*" title="One link, without ;"></label></p>
|
||||
|
||||
@@ -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 = ""
|
||||
|
||||
@@ -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) <> "])"
|
||||
|
||||
Reference in new issue
Block a user