Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web-v2/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default defineConfig([
},
},
settings: {
'import/core-modules': ['virtual:simplicity-sources'],
'import/resolver': {
typescript: {},
node: {
Expand Down
36 changes: 36 additions & 0 deletions web-v2/plugins/lwkWasmPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'node:fs'
import path from 'node:path'

import type { Plugin } from 'vite'

interface LwkWasmPluginOptions {
wasmPath: string
}

export function lwkWasmPlugin(options: LwkWasmPluginOptions): Plugin {
return {
name: 'lwk-wasm',

configureServer(server) {
server.middlewares.use((req, res, next) => {
if (!req.url) {
return next()
}

if (!req.url.endsWith('lwk_wasm_bg.wasm')) {
return next()
}

const wasmFile = path.resolve(options.wasmPath)

if (!fs.existsSync(wasmFile)) {
return next()
}

res.setHeader('Content-Type', 'application/wasm')

fs.createReadStream(wasmFile).pipe(res)
})
},
}
}
72 changes: 72 additions & 0 deletions web-v2/plugins/simplicitySourcesPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import fs from 'node:fs'
import path from 'node:path'

import type { Plugin, ResolvedConfig } from 'vite'

// Internal Vite virtual module id.
// We dynamically generate this module from raw .simf covenant sources
// so the frontend can import them like a normal ES module.
const SIMPLICITY_SOURCES_VIRTUAL = '\0virtual:simplicity-sources'

interface CovenantConfig {
covenants: Array<{
id: string
path: string
}>
}

interface SimplicitySourcesPluginOptions {
configPath: string
}

export function simplicitySourcesPlugin(options: SimplicitySourcesPluginOptions): Plugin {
let viteConfig: ResolvedConfig

return {
name: 'simplicity-sources',

configResolved(config) {
viteConfig = config
},

resolveId(id) {
if (id === 'virtual:simplicity-sources') {
return SIMPLICITY_SOURCES_VIRTUAL
}

return null
},

load(id) {
if (id !== SIMPLICITY_SOURCES_VIRTUAL) {
return null
}

const resolvedConfigPath = path.resolve(viteConfig.root, options.configPath)

if (!fs.existsSync(resolvedConfigPath)) {
throw new Error(`Simplicity config not found: ${resolvedConfigPath}`)
}

const rawConfig = fs.readFileSync(resolvedConfigPath, 'utf-8')

const config = JSON.parse(rawConfig) as CovenantConfig

const sources: Record<string, string> = {}

for (const covenant of config.covenants) {
const covenantPath = path.resolve(viteConfig.root, covenant.path)

if (!fs.existsSync(covenantPath)) {
throw new Error(`Simplicity covenant file not found: ${covenantPath}`)
}

sources[covenant.id] = fs.readFileSync(covenantPath, 'utf-8').trim()
}

return `
export const sources = ${JSON.stringify(sources)}
`
},
}
}
7 changes: 7 additions & 0 deletions web-v2/simplicity-covenants.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"covenants": [
{ "id": "lending", "path": "../crates/contracts/simf/lending.simf" },
{ "id": "asset_auth", "path": "../crates/contracts/simf/asset_auth.simf" },
{ "id": "script_auth", "path": "../crates/contracts/simf/script_auth.simf" }
]
}
27 changes: 27 additions & 0 deletions web-v2/src/lib/pset-builder/buildExplicitRecipientPset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Address, type Network, type OutPoint, type Pset, TxBuilder, type Wollet } from 'lwk_web'

interface BuildExplicitRecipientPsetParams {
wollet: Wollet
network: Network
recipientAddress: string
outpoints: OutPoint[]
satoshi: bigint
feeRate?: number
}

export function buildExplicitRecipientPset({
wollet,
network,
recipientAddress,
outpoints,
satoshi,
feeRate = 1,
}: BuildExplicitRecipientPsetParams): Pset {
const address = new Address(recipientAddress)

return new TxBuilder(network)
.feeRate(feeRate)
.setWalletUtxos(outpoints)
.addExplicitRecipient(address, satoshi, network.policyAsset())
.finish(wollet)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Address,
type ExternalUtxo,
type Network,
type OutPoint,
type Pset,
TxBuilder,
type Wollet,
} from 'lwk_web'

interface BuildExternalExplicitRecipientPsetParams {
wollet: Wollet
network: Network
recipientAddress: string
outpoints: OutPoint[]
externalUtxos: ExternalUtxo[]
satoshi: bigint
feeRate?: number
}

export function buildExternalExplicitRecipientPset({
wollet,
network,
recipientAddress,
outpoints,
externalUtxos,
satoshi,
feeRate = 1,
}: BuildExternalExplicitRecipientPsetParams): Pset {
const address = new Address(recipientAddress)
void externalUtxos

return (
new TxBuilder(network)
.feeRate(feeRate)
.setWalletUtxos(outpoints)
// .addExternalUtxos(externalUtxos) // TODO: expose TxBuilder.addExternalUtxos() in lwk_web.
.addExplicitRecipient(address, satoshi, network.policyAsset())
.finish(wollet)
)
}
2 changes: 2 additions & 0 deletions web-v2/src/lib/pset-builder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { buildExplicitRecipientPset } from './buildExplicitRecipientPset'
export { buildExternalExplicitRecipientPset } from './buildExternalExplicitRecipientPset'
20 changes: 11 additions & 9 deletions web-v2/src/lib/wallet-core/connector/jade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { Jade, Network, Pset, Wollet, WolletDescriptor } from 'lwk_web'

import type { Lwk } from '@/lwk'
import {
Jade,
type Network,
type Pset,
Singlesig,
type Wollet,
type WolletDescriptor,
} from 'lwk_web'

import type { ConnectionStatus, JadeVersionInfo, WalletType } from '../types'
import type { WalletConnector } from './types'
Expand All @@ -17,17 +22,14 @@ export class JadeConnector implements WalletConnector {
private busy = false
private _id: string | null = null

constructor(
private readonly lwk: Lwk,
private readonly lwkNetwork: Network,
) {}
constructor(private readonly lwkNetwork: Network) {}

async connect(): Promise<void> {
if (this.jade !== null) return
// HACK: The TS bindings declare this as a sync constructor, but wasm-bindgen
// generates an async constructor under the hood that returns a Promise.
// `await new this.lwk.Jade(...)` is intentional — not a mistake.
this.jade = await new this.lwk.Jade(this.lwkNetwork, true)
this.jade = await new Jade(this.lwkNetwork, true)
}

disconnect(): void {
Expand Down Expand Up @@ -91,7 +93,7 @@ export class JadeConnector implements WalletConnector {
const addrResult = wollet.address()
const index = addrResult.index()
const path = wollet.addressFullPath(index)
const singlesig = this.lwk.Singlesig.from(variant)
const singlesig = Singlesig.from(variant)
return await this.jade.getReceiveAddressSingle(singlesig, path)
}

Expand Down
21 changes: 13 additions & 8 deletions web-v2/src/lib/wallet-core/connector/seed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { Network, Pset, Signer, WolletDescriptor } from 'lwk_web'

import type { Lwk } from '@/lwk'
import {
Mnemonic,
type Network,
type Pset,
Signer,
simplicityDeriveXonlyPubkey,
type WolletDescriptor,
XOnlyPublicKey,
} from 'lwk_web'

import type { ConnectionStatus, WalletType } from '../types'
import type { WalletConnector } from './types'
Expand All @@ -19,7 +25,6 @@ export class SeedConnector implements WalletConnector {
private _id: string | null = null

constructor(
private readonly lwk: Lwk,
private readonly lwkNetwork: Network,
private readonly mnemonicStr: string,
) {
Expand All @@ -28,8 +33,8 @@ export class SeedConnector implements WalletConnector {

async connect(): Promise<void> {
if (this.signer !== null) return
const mnemonic = new this.lwk.Mnemonic(this.mnemonicStr)
this.signer = new this.lwk.Signer(mnemonic, this.lwkNetwork)
const mnemonic = new Mnemonic(this.mnemonicStr)
this.signer = new Signer(mnemonic, this.lwkNetwork)
this._id = crypto.randomUUID()
}

Expand Down Expand Up @@ -61,11 +66,11 @@ export class SeedConnector implements WalletConnector {
return this.signer.sign(pset)
}

async getXOnlyPublicKey(): Promise<string> {
async getXOnlyPublicKey(): Promise<XOnlyPublicKey> {
if (!this.signer) throw new Error('SeedConnector: not connected')
//github.com/BlockstreamResearch/smplx/blob/1945d11b47fff8838c3e99c210133519a9522324/crates/sdk/src/signer/core.rs#L621C1-L628C2
const path = this.lwkNetwork.isMainnet() ? 'm/84h/1776h/0h/0/1' : 'm/84h/1h/0h/0/1'
return this.lwk.simplicityDeriveXonlyPubkey(this.signer, path).toString()
return simplicityDeriveXonlyPubkey(this.signer, path)
}

async getConnectionStatus(): Promise<ConnectionStatus> {
Expand Down
4 changes: 2 additions & 2 deletions web-v2/src/lib/wallet-core/connector/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Pset, Wollet, WolletDescriptor } from 'lwk_web'
import type { Pset, Wollet, WolletDescriptor, XOnlyPublicKey } from 'lwk_web'

import type { ConnectionStatus, WalletType } from '../types'

Expand All @@ -10,6 +10,6 @@ export interface WalletConnector {
signPset(pset: Pset): Promise<Pset>
isConnected: boolean
getConnectionStatus(): Promise<ConnectionStatus>
getXOnlyPublicKey?(): Promise<string>
getXOnlyPublicKey?(): Promise<XOnlyPublicKey>
getVerifiedReceiveAddress?(variant: WalletType, wollet: Wollet): Promise<string>
}
34 changes: 7 additions & 27 deletions web-v2/src/lwk/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type {
EsploraClient,
Network,
SimplicityArguments,
Transaction,
XOnlyPublicKey,
} from 'lwk_web'
import { EsploraClient, Network, type Transaction } from 'lwk_web'

import { env, type NetworkName } from '@/constants/env'

Expand All @@ -20,41 +14,27 @@ export async function getLwk(): Promise<Lwk> {
return lwk
}

export function createLwkNetwork(network: NetworkName, lwk: Lwk): Network {
export function createLwkNetwork(network: NetworkName): Network {
switch (network) {
case 'liquid':
return lwk.Network.mainnet()
return Network.mainnet()
case 'liquidtestnet':
return lwk.Network.testnet()
return Network.testnet()
case 'regtest':
return lwk.Network.regtestDefault()
return Network.regtestDefault()
}
}

export interface PsetWithExtractTx {
extractTx(): Transaction
}

export interface CreateP2trAddressParams {
source: string
args: SimplicityArguments
internalKey: XOnlyPublicKey
network: NetworkName
}

export function createP2trAddress(lwk: Lwk, params: CreateP2trAddressParams): string {
const program = lwk.SimplicityProgram.load(params.source, params.args)
const net = createLwkNetwork(params.network, lwk)
const address = program.createP2trAddress(params.internalKey, net)
return address.toString()
}

/**
* Creates an EsploraClient configured for waterfalls + utxoOnly scanning.
* Waterfalls provides fast indexed encrypted UTXO discovery vs slow sequential HD scan.
*/
export function createEsploraClient(lwk: Lwk, lwkNetwork: Network): EsploraClient {
const client = new lwk.EsploraClient(
export function createEsploraClient(lwkNetwork: Network): EsploraClient {
const client = new EsploraClient(
lwkNetwork,
`${env.VITE_WATERFALLS_URL}/api`,
true, // waterfalls
Expand Down
Loading