@@ -3,57 +3,28 @@ import { describeAccountSnapshot } from "openclaw/plugin-sdk/account-helpers";
33import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts" ;
44import {
55 createDelegatedSetupWizardProxy ,
6- createStandardChannelSetupStatus ,
76 DEFAULT_ACCOUNT_ID ,
8- createSetupTranslator ,
97} from "openclaw/plugin-sdk/setup-runtime" ;
108import { buildChannelConfigSchema , type ChannelPlugin } from "./channel-api.js" ;
119import { NostrConfigSchema } from "./config-schema.js" ;
1210import { DEFAULT_RELAYS } from "./default-relays.js" ;
13- import { createNostrSetupAdapter , createNostrSetupContract } from "./setup-adapter.js" ;
14-
15- const t = createSetupTranslator ( ) ;
11+ import {
12+ createNostrSetupAdapter ,
13+ createNostrSetupContract ,
14+ createNostrSetupStatus ,
15+ } from "./setup-adapter.js" ;
16+ import type { ResolvedNostrAccount } from "./types.js" ;
1617
1718const channel = "nostr" as const ;
1819
19- type NostrAccountConfig = {
20- enabled ?: boolean ;
21- name ?: string ;
22- defaultAccount ?: string ;
23- privateKey ?: unknown ;
24- relays ?: string [ ] ;
25- dmPolicy ?: "pairing" | "allowlist" | "open" | "disabled" ;
26- allowFrom ?: Array < string | number > ;
27- profile ?: unknown ;
28- } ;
29-
30- type ResolvedNostrSetupAccount = {
31- accountId : string ;
32- name ?: string ;
33- enabled : boolean ;
34- configured : boolean ;
35- privateKey : string ;
36- publicKey : string ;
37- relays : string [ ] ;
38- profile ?: unknown ;
39- config : NostrAccountConfig ;
40- } ;
20+ type NostrAccountConfig = ResolvedNostrAccount [ "config" ] ;
4121
4222function getNostrConfig ( cfg : OpenClawConfig ) : NostrAccountConfig | undefined {
4323 return ( cfg . channels as Record < string , unknown > | undefined ) ?. nostr as
4424 | NostrAccountConfig
4525 | undefined ;
4626}
4727
48- function listSetupNostrAccountIds ( cfg : OpenClawConfig ) : string [ ] {
49- const nostrCfg = getNostrConfig ( cfg ) ;
50- const privateKey = typeof nostrCfg ?. privateKey === "string" ? nostrCfg . privateKey . trim ( ) : "" ;
51- if ( ! privateKey ) {
52- return [ ] ;
53- }
54- return [ resolveDefaultSetupNostrAccountId ( cfg ) ] ;
55- }
56-
5728function resolveDefaultSetupNostrAccountId ( cfg : OpenClawConfig ) : string {
5829 const configured = getNostrConfig ( cfg ) ?. defaultAccount ;
5930 return typeof configured === "string" && configured . trim ( )
@@ -64,7 +35,7 @@ function resolveDefaultSetupNostrAccountId(cfg: OpenClawConfig): string {
6435function resolveSetupNostrAccount ( params : {
6536 cfg : OpenClawConfig ;
6637 accountId ?: string | null ;
67- } ) : ResolvedNostrSetupAccount {
38+ } ) : ResolvedNostrAccount {
6839 const nostrCfg = getNostrConfig ( params . cfg ) ;
6940 const accountId = params . accountId ?. trim ( ) || resolveDefaultSetupNostrAccountId ( params . cfg ) ;
7041 const privateKey = typeof nostrCfg ?. privateKey === "string" ? nostrCfg . privateKey . trim ( ) : "" ;
@@ -90,47 +61,16 @@ function resolveSetupNostrAccount(params: {
9061 } ;
9162}
9263
93- function looksLikeNostrPrivateKey ( privateKey : string ) : boolean {
94- return (
95- privateKey . startsWith ( "nsec1" ) ||
96- privateKey . startsWith ( "NSEC1" ) ||
97- / ^ [ 0 - 9 a - f A - F ] { 64 } $ / . test ( privateKey )
98- ) ;
99- }
100-
101- const nostrSetupAdapter = createNostrSetupAdapter ( {
102- resolveAccountId : ( cfg , accountId ) => accountId ?. trim ( ) || resolveDefaultSetupNostrAccountId ( cfg ) ,
103- validatePrivateKey : looksLikeNostrPrivateKey ,
104- } ) ;
105- const nostrSetupContract = createNostrSetupContract ( nostrSetupAdapter ) ;
106-
10764const nostrSetupWizard = createDelegatedSetupWizardProxy ( {
10865 channel,
10966 loadWizard : async ( ) => ( await import ( "./setup-surface.js" ) ) . nostrSetupWizard ,
110- status : {
111- ...createStandardChannelSetupStatus ( {
112- channelLabel : "Nostr" ,
113- configuredLabel : t ( "wizard.channels.statusConfigured" ) ,
114- unconfiguredLabel : t ( "wizard.channels.statusNeedsPrivateKey" ) ,
115- configuredHint : t ( "wizard.channels.statusConfigured" ) ,
116- unconfiguredHint : t ( "wizard.channels.statusNeedsPrivateKey" ) ,
117- configuredScore : 1 ,
118- unconfiguredScore : 0 ,
119- includeStatusLine : true ,
120- resolveConfigured : ( { cfg, accountId } ) =>
121- resolveSetupNostrAccount ( { cfg, accountId } ) . configured ,
122- resolveExtraStatusLines : ( { cfg } ) => {
123- const account = resolveSetupNostrAccount ( { cfg } ) ;
124- return [ `Relays: ${ account . relays . length || DEFAULT_RELAYS . length } ` ] ;
125- } ,
126- } ) ,
127- } ,
67+ status : createNostrSetupStatus ( resolveSetupNostrAccount ) ,
12868 resolveShouldPromptAccountIds : ( ) => false ,
12969 delegatePrepare : true ,
13070 delegateFinalize : true ,
13171} ) ;
13272
133- export const nostrSetupPlugin : ChannelPlugin < ResolvedNostrSetupAccount > = {
73+ export const nostrSetupPlugin : ChannelPlugin < ResolvedNostrAccount > = {
13474 id : channel ,
13575 meta : {
13676 id : channel ,
@@ -147,10 +87,17 @@ export const nostrSetupPlugin: ChannelPlugin<ResolvedNostrSetupAccount> = {
14787 } ,
14888 reload : { configPrefixes : [ "channels.nostr" ] } ,
14989 configSchema : buildChannelConfigSchema ( NostrConfigSchema ) ,
150- setupContract : nostrSetupContract ,
90+ setupContract : createNostrSetupContract (
91+ createNostrSetupAdapter ( {
92+ resolveAccountId : ( cfg , accountId ) =>
93+ accountId ?. trim ( ) || resolveDefaultSetupNostrAccountId ( cfg ) ,
94+ validatePrivateKey : ( privateKey ) => / ^ (?: n s e c 1 | N S E C 1 ) | ^ [ 0 - 9 a - f A - F ] { 64 } $ / u. test ( privateKey ) ,
95+ } ) ,
96+ ) ,
15197 setupWizard : nostrSetupWizard ,
15298 config : {
153- listAccountIds : listSetupNostrAccountIds ,
99+ listAccountIds : ( cfg ) =>
100+ resolveSetupNostrAccount ( { cfg } ) . configured ? [ resolveDefaultSetupNostrAccountId ( cfg ) ] : [ ] ,
154101 resolveAccount : ( cfg , accountId ) => resolveSetupNostrAccount ( { cfg, accountId } ) ,
155102 defaultAccountId : resolveDefaultSetupNostrAccountId ,
156103 isConfigured : ( account ) => account . configured ,
0 commit comments