encryption

This commit is contained in:
2026-10-20 05:27:18 -04:00
parent 3cee89d023
commit 4080114085
10 changed files with 324 additions and 72 deletions
+4 -7
View File
@@ -2,7 +2,7 @@ import { useMemo, useState } from 'react';
import { ArrowLeftRight, Check, Clipboard, LoaderCircle, RefreshCw, Send } from 'lucide-react';
import { CONSENSUS, NETWORK, isNftTrade, nftTradeMessage, type NftTrade, type TestnetDeck, type TestnetSnapshot } from '../shared/testnet.ts';
import { tradeOfferSchema, type TradeOffer } from '../shared/trade-offer.ts';
import type { Wallet } from './wallet.ts';
import { sign, type Wallet } from './wallet.ts';
import './trade.css';
import { StateClient } from '../shared/rpc-client.ts';
const stateClient = new StateClient();
@@ -148,9 +148,7 @@ export function TradeDesk({ wallet, snapshot, rpcAvailable, onNotice }: Props) {
});
const terms = { network: NETWORK, chainId: fresh.chainId, proposer: wallet.address,
counterparty: remoteDeck.address, offered: offeredLegs, requested: requestedLegs };
const proposerSignature = await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, wallet.privateKey,
new TextEncoder().encode(nftTradeMessage(terms)));
const signatureText = btoa(String.fromCharCode(...new Uint8Array(proposerSignature)));
const signatureText = await sign(wallet,nftTradeMessage(terms));
const offer = { format: 'wikideck-trade-offer' as const, version: 1 as const, ...terms,
proposerPublicKey: wallet.publicKey, proposerSignature: signatureText };
setOfferText(JSON.stringify(offer, null, 2));
@@ -180,13 +178,12 @@ export function TradeDesk({ wallet, snapshot, rpcAvailable, onNotice }: Props) {
if (!block || !ownsFrom(fresh, leg.cardHash, wallet.address) || ownershipSequence(fresh, leg.cardHash) !== leg.sequence ||
fresh.blocks.length + 1 - block.height < CONSENSUS.coinbaseMaturity) throw new Error('One of your requested cards is no longer owned, current, or mature.');
}
const counterpartySignature = await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, wallet.privateKey,
new TextEncoder().encode(nftTradeMessage(offer)));
const counterpartySignature = await sign(wallet,nftTradeMessage(offer));
const accepted: NftTrade = { type: 'trade', network: offer.network, chainId: offer.chainId,
proposer: offer.proposer, counterparty: offer.counterparty, offered: offer.offered, requested: offer.requested,
proposerPublicKey: offer.proposerPublicKey, proposerSignature: offer.proposerSignature,
counterpartyPublicKey: wallet.publicKey,
counterpartySignature: btoa(String.fromCharCode(...new Uint8Array(counterpartySignature))) };
counterpartySignature };
const result = await api<{ status: string; message: string }>('trades', accepted);
onNotice(result.message);
setOfferText(''); setRemoteDeck(undefined); setOffered(new Set()); setRequested(new Set());