-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.ts
More file actions
84 lines (78 loc) · 3.4 KB
/
Copy pathindex.ts
File metadata and controls
84 lines (78 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* OpenWallet Standard (OWS) integration for filecoin-pin
*
* Resolves an OWS-managed wallet into a viem `Account` that the Synapse SDK
* can sign with directly. Private keys never leave the OWS core; this module
* just hands Synapse a signing surface (signMessage / signTransaction /
* signTypedData) backed by the OWS adapter.
*
* The adapter is loaded via dynamic `import()` because
* `@open-wallet-standard/core` is a napi-rs native binding without prebuilt
* artifacts for Windows or musl. A static import would crash CLI startup on
* those platforms even when the user never asks for OWS auth.
*
* @module core/ows
*/
import type { Account, Chain } from 'viem'
export interface OwsAccountOptions {
/** Wallet name or ID registered with the `ows` CLI / OWS core */
walletId: string
/** Target Filecoin chain (used to derive CAIP-2 chain ID) */
chain: Chain
/** Optional passphrase for keystore-encrypted wallets */
passphrase?: string
/** Optional account index within the wallet (defaults to 0) */
index?: number
/** Optional override for OWS vault path */
vaultPath?: string
}
interface OwsViemAdapter {
owsToViemAccount: (
walletNameOrId: string,
options?: { chain?: string; passphrase?: string; index?: number; vaultPath?: string }
) => Account
}
async function loadAdapter(): Promise<OwsViemAdapter> {
try {
return (await import('@open-wallet-standard/adapters/viem')) as unknown as OwsViemAdapter
} catch (err) {
const reason = err instanceof Error ? err.message : String(err)
throw new Error(
'OpenWallet Standard is not available on this platform. ' +
'@open-wallet-standard/core ships napi-rs prebuilt binaries for linux-x64-gnu, ' +
'linux-arm64-gnu, darwin-x64, and darwin-arm64 only (no Windows or musl/Alpine artifact today). ' +
'Use --private-key / PRIVATE_KEY instead, or run on a supported platform.\n' +
`Underlying load error: ${reason}`
)
}
}
const EVM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/
/**
* Build a viem `Account` backed by an OWS wallet.
*
* filecoin-pin signs via FEVM (Filecoin EVM), so we always request an
* `eip155:*` account from OWS. OWS wallets typically also expose a native
* Filecoin (`fil:*`, f1/f3 address) account derived from the same seed —
* that one is not used here, since synapse-sdk targets FEVM via viem.
*
* The returned account is a `LocalAccount` from viem's perspective; signing
* calls are delegated to the OWS native core, so the private key never
* materializes in the Node process.
*/
export async function getOwsAccount(options: OwsAccountOptions): Promise<Account> {
const { owsToViemAccount } = await loadAdapter()
const chainId = `eip155:${options.chain.id}`
const adapterOptions: Parameters<typeof owsToViemAccount>[1] = { chain: chainId }
if (options.passphrase != null) adapterOptions.passphrase = options.passphrase
if (options.index != null) adapterOptions.index = options.index
if (options.vaultPath != null) adapterOptions.vaultPath = options.vaultPath
const account = owsToViemAccount(options.walletId, adapterOptions)
if (!EVM_ADDRESS_REGEX.test(account.address)) {
throw new Error(
`OWS returned a non-EVM address (${account.address}) for wallet "${options.walletId}". ` +
'filecoin-pin signs via FEVM and requires an eip155 account. ' +
'Check that the wallet has an eip155:* entry in `ows wallet list`.'
)
}
return account
}