mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-22 05:47:51 +00:00
4 rounds of reviews
This commit is contained in:
1 parent
4eafc41c80
commit
1a0d2300f4
7 files changed
+32
-65
No files matched your search
+2
-1
@@ -1,12 +1,13 @@
|
||||
# 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 the store service sends to the chain: the commitment to commit, and the registration with 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, 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
|
||||
|
||||
```bash
|
||||
cd names-web
|
||||
npm install
|
||||
npx playwright install chromium
|
||||
npm run build
|
||||
```
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
{
|
||||
"name": "@simplex-chat/names-web",
|
||||
"version": "0.1.0",
|
||||
"description": "SimpleX names purchase page: wallet, redemption code and signed registration intent in the browser",
|
||||
"license": "AGPL-3.0-only",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
|
||||
@@ -39,6 +39,8 @@ function hashStruct(typeString: string, words: Uint8Array[]): Uint8Array {
|
||||
}
|
||||
|
||||
function registrationTuple(r: Registration): Uint8Array {
|
||||
const reverseRecord = uint256(0n)
|
||||
const referrer = new Uint8Array(32)
|
||||
return abiTuple([
|
||||
dynamic(abiBytes(utf8ToBytes(r.label))),
|
||||
fixed(addressWord(r.owner)),
|
||||
@@ -46,8 +48,8 @@ function registrationTuple(r: Registration): Uint8Array {
|
||||
fixed(r.secret),
|
||||
fixed(addressWord(r.resolver)),
|
||||
dynamic(abiArray(recordCalls(r).map(abiBytes))),
|
||||
fixed(uint256(0n)),
|
||||
fixed(new Uint8Array(32)),
|
||||
fixed(reverseRecord),
|
||||
fixed(referrer),
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ test("prepares a commitment and a registration signed by the code's wallet", asy
|
||||
|
||||
const wallet = walletFromEntropy(parseNameCode(code)!)
|
||||
expect(request.owner).toBe(bytesToHex(wallet.address))
|
||||
expect(request.channelLinks).toEqual(["https://smp1.simplex.im/c#one", "https://smp2.simplex.im/c#two"])
|
||||
expect(request).toMatchObject({label: "bakery1", contactLinks: ["https://smp.simplex.im/a#contact"], channelLinks: ["https://smp1.simplex.im/c#one", "https://smp2.simplex.im/c#two"]})
|
||||
|
||||
const registration: Registration = {
|
||||
...request,
|
||||
@@ -26,7 +26,7 @@ test("prepares a commitment and a registration signed by the code's wallet", asy
|
||||
secret: hexToBytes(request.secret),
|
||||
resolver: hexToBytes(request.resolver),
|
||||
}
|
||||
expect(bytesToHex(makeCommitment(registration))).toBe(commitment)
|
||||
expect(commitment).toBe(bytesToHex(makeCommitment(registration)))
|
||||
|
||||
const digest = registerIntentDigest(chainId, sponsor, registration, BigInt(request.nonce), BigInt(request.deadline))
|
||||
expect(request.signature).toBe(bytesToHex(signDigest(wallet.privateKey, digest)))
|
||||
|
||||
@@ -6,11 +6,8 @@
|
||||
"lib": ["ES2022"],
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"declaration": true,
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
@@ -18,10 +18,10 @@ element("register").addEventListener("click", () => {
|
||||
contactLinks: lines("contact"),
|
||||
channelLinks: lines("channel"),
|
||||
}
|
||||
element("commit-request").textContent = json({commitment: makeCommitment(registration)})
|
||||
const nonce = 0n
|
||||
const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600)
|
||||
const signature = signDigest(wallet.privateKey, registerIntentDigest(chainId, sponsor, registration, nonce, deadline))
|
||||
element("commit-request").textContent = json({commitment: makeCommitment(registration)})
|
||||
element("register-request").textContent = json({...registration, nonce, deadline, signature})
|
||||
})
|
||||
|
||||
|
||||
+22
-53
@@ -6,27 +6,23 @@
|
||||
module NamesWebTests (namesWebTests) where
|
||||
|
||||
import Control.Concurrent.STM (TVar, atomically)
|
||||
import Control.Monad (forM_, replicateM, replicateM_)
|
||||
import Control.Monad (forM_, replicateM_)
|
||||
import Crypto.Random (ChaChaDRG)
|
||||
import qualified Data.ByteArray as BA
|
||||
import qualified Data.ByteArray.Encoding as BAE
|
||||
import Data.Bifunctor (first)
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.ByteString.Char8 as BC
|
||||
import Data.Char (toLower)
|
||||
import Data.List (intercalate)
|
||||
import Data.Word (Word64)
|
||||
import qualified Simplex.Messaging.Crypto as C
|
||||
import Simplex.Messaging.Crypto.BIP32 (masterKey, xkKey)
|
||||
import Simplex.Messaging.Crypto.BIP39 (EntropyStrength (..), WalletEntropy, entropySeed, randomEntropy, unEntropy)
|
||||
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.Encoding.String (strDecode)
|
||||
import Simplex.Messaging.Eth.Address (Address, deriveAddress, unAddress)
|
||||
import Simplex.Messaging.Eth.Address (deriveAddress, unAddress)
|
||||
import Simplex.Messaging.Eth.EIP712
|
||||
import System.Directory (doesDirectoryExist)
|
||||
import System.Random (randomIO)
|
||||
import System.Process (CreateProcess (..), proc, readCreateProcess)
|
||||
import Test.Hspec hiding (fit, it)
|
||||
import Util
|
||||
@@ -40,53 +36,27 @@ namesWebTests = do
|
||||
if distExists
|
||||
then do
|
||||
g <- runIO C.newRandom
|
||||
it "derives the wallet address from the entropy as Haskell does" $ testWalletAddress g
|
||||
it "signs a digest as Haskell does" $ testSignature g
|
||||
it "hashes the RegisterName intent as Haskell does" testIntentDigest
|
||||
it "derives the wallet and signs a digest as Haskell does" $ testWallet g
|
||||
it "formats and parses the simplex-chat name codes" testNameCodes
|
||||
it "encodes the simplex-chat registration vectors" testChatVectors
|
||||
else
|
||||
it "skipped (run 'cd names-web && npm install && npm run build' first)" $
|
||||
pendingWith "TS project not compiled"
|
||||
|
||||
testWalletAddress :: TVar ChaChaDRG -> IO ()
|
||||
testWalletAddress g =
|
||||
testWallet :: TVar ChaChaDRG -> IO ()
|
||||
testWallet g =
|
||||
replicateM_ 5 $ do
|
||||
entropy <- atomically $ randomEntropy ES128 g
|
||||
(_, addr) <- wallet g entropy
|
||||
callNode ("walletFromEntropy(" <> jsUint8 (BA.convert $ unEntropy entropy) <> ").address") `shouldReturn` hex (unAddress addr)
|
||||
|
||||
testSignature :: TVar ChaChaDRG -> IO ()
|
||||
testSignature g =
|
||||
replicateM_ 5 $ do
|
||||
entropy <- atomically $ randomEntropy ES128 g
|
||||
(key, _) <- wallet g entropy
|
||||
let master = either error id $ masterKey $ entropySeed entropy ""
|
||||
(xk, addr) <- either error id <$> deriveAddress g master (bip44Path Ethereum $ either error id $ mkAccountIndex 0)
|
||||
digest <- atomically $ C.randomBytes 32 g
|
||||
signature <- either error id <$> S.signRecoverable g key digest
|
||||
callNode ("signDigest(walletFromEntropy(" <> jsUint8 (BA.convert $ unEntropy entropy) <> ").privateKey, " <> jsUint8 digest <> ")")
|
||||
`shouldReturn` hex (serializeSignature signature)
|
||||
|
||||
testIntentDigest :: IO ()
|
||||
testIntentDigest =
|
||||
replicateM_ 5 $ do
|
||||
[owner, resolver, sponsor] <- replicateM 3 $ B.pack <$> replicateM 20 randomIO
|
||||
chainId <- randomIO :: IO Word64
|
||||
nonce <- randomIO :: IO Word64
|
||||
deadline <- randomIO :: IO Word64
|
||||
let domain = Eip712Domain {edName = "SimplexSponsor", edVersion = "1", edChainId = chainId, edVerifyingContract = address sponsor}
|
||||
registration =
|
||||
"{label: 'bakery1', tld: 'simplex', owner: " <> jsUint8 owner <> ", duration: 0n, secret: new Uint8Array(32), resolver: "
|
||||
<> jsUint8 resolver <> ", contactLinks: [], channelLinks: []}"
|
||||
callNode ("registerIntentDigest(" <> show chainId <> "n, " <> jsUint8 sponsor <> ", " <> registration <> ", " <> show nonce <> "n, " <> show deadline <> "n)")
|
||||
`shouldReturn` hex
|
||||
( hashTypedData
|
||||
domain
|
||||
"RegisterName(string label,address owner,address resolver,bytes[] data,uint256 nonce,uint256 deadline)"
|
||||
[VBytes "bakery1", VAddress $ address owner, VAddress $ address resolver, VArray [], VUint $ fromIntegral nonce, VUint $ fromIntegral deadline]
|
||||
)
|
||||
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 ("signDigest(" <> jsWallet <> ".privateKey, " <> jsUint8 digest <> ")") `shouldReturn` hex (serializeSignature signature)
|
||||
|
||||
testNameCodes :: IO ()
|
||||
testNameCodes =
|
||||
testNameCodes = do
|
||||
forM_
|
||||
[ ("00000000000000000000000000000000", "SN-00000-00000-00000-00000-00000-0VME2"),
|
||||
("ffffffffffffffffffffffffffffffff", "SN-ZZZZZ-ZZZZZ-ZZZZZ-ZZZZZ-ZZZZZ-XDCD9"),
|
||||
@@ -95,23 +65,25 @@ testNameCodes =
|
||||
$ \(entropy, code) -> do
|
||||
callNode ("formatNameCode(" <> jsHex entropy <> ")") `shouldReturn` BC.pack code
|
||||
callNode ("parseNameCode('" <> map toLower code <> "')") `shouldReturn` BC.pack entropy
|
||||
callNode "parseNameCode('SN-OOOOO-OOOOO-OOOOO-OOOOO-OOOOO-OVME2')" `shouldReturn` "00000000000000000000000000000000"
|
||||
callNode "String(parseNameCode('SN-KT45V-59ATD-HCNTT-EZRTA-HT8VT-8A0BV'))" `shouldReturn` "null"
|
||||
|
||||
testChatVectors :: IO ()
|
||||
testChatVectors = do
|
||||
callNode ("makeCommitment(" <> knownRegistration <> ")") `shouldReturn` "43aeb4617085160f4dc7ad495d1119f39bca441276b1990123fd3ecb8a40a360"
|
||||
callNode ("signDigest(walletFromEntropy(new Uint8Array(16)).privateKey, registerIntentDigest(31337n, " <> jsHex "9a676e781a523b5d0c0e43731313a708cb607508" <> ", " <> knownRegistration <> ", 0n, 4102444800n))")
|
||||
callNode (knownSignature knownRegistration)
|
||||
`shouldReturn` "74af637cf246cd336c3713f95fd9ed19862971598fcab5f4fb5d044ed033a3854c2ce30ffd36c25ed5f9a8ef691d004b3662f3553113403416f658d9c604732b1b"
|
||||
callNode (knownSignature $ "{..." <> knownRegistration <> ", contactLinks: [], channelLinks: []}")
|
||||
`shouldReturn` "caac218bca1ac887c6438f41f9f55f4ad026b61f16951b420785cc8d98b990f2306eaa67f18adb7b537225fff38dcd6012e53492e2d07745b565d9b09bd0d9221b"
|
||||
where
|
||||
knownSignature r = "signDigest(walletFromEntropy(new Uint8Array(16)).privateKey, registerIntentDigest(31337n, " <> jsHex "9a676e781a523b5d0c0e43731313a708cb607508" <> ", " <> r <> ", 0n, 4102444800n))"
|
||||
knownRegistration =
|
||||
"{label: 'bakery1', tld: 'simplex', owner: walletFromEntropy(new Uint8Array(16)).address, duration: 63072000n, secret: new Uint8Array(32).fill(0x11), resolver: "
|
||||
"{label: 'bakery1', tld: 'simplex', owner: "
|
||||
<> jsHex "9858effd232b4033e47d90003d41ec34ecaeda94"
|
||||
<> ", duration: 63072000n, secret: new Uint8Array(32).fill(0x11), resolver: "
|
||||
<> jsHex "2279b7a0a67db372996a5fab50d91eaa73d2ebe6"
|
||||
<> ", contactLinks: ['https://smp.simplex.im/a#contact'], channelLinks: ['https://smp1.simplex.im/c#one', 'https://smp2.simplex.im/c#two']}"
|
||||
|
||||
wallet :: TVar ChaChaDRG -> WalletEntropy -> IO (S.Secp256k1PrivateKey, Address)
|
||||
wallet g entropy = first xkKey . either error id <$> deriveAddress g master (bip44Path Ethereum $ either error id $ mkAccountIndex 0)
|
||||
where
|
||||
master = either error id $ masterKey $ entropySeed entropy ""
|
||||
|
||||
callNode :: String -> IO ByteString
|
||||
callNode expr =
|
||||
BC.pack
|
||||
@@ -130,8 +102,5 @@ jsUint8 bs = "new Uint8Array([" <> intercalate "," (map show $ B.unpack bs) <> "
|
||||
jsHex :: String -> String
|
||||
jsHex = jsUint8 . either error id . BAE.convertFromBase BAE.Base16 . BC.pack
|
||||
|
||||
address :: ByteString -> Address
|
||||
address = either error id . strDecode . hex
|
||||
|
||||
hex :: ByteString -> ByteString
|
||||
hex = BAE.convertToBase BAE.Base16
|
||||
Reference in new issue
Block a user