support --auth/--sec/--prompt-sec on req.

This commit is contained in:
fiatjaf
2023-12-09 16:32:04 -03:00
parent ed3156ae10
commit b7b61c0723
7 changed files with 78 additions and 65 deletions

View File

@@ -222,27 +222,22 @@ example:
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
defer cancel()
status, err := relay.Publish(ctx, evt)
if err == nil {
// published fine probably
log("%s.\n", status)
if err := relay.Publish(ctx, evt); err == nil {
// published fine
log("success.\n")
continue nextline
}
// error publishing
if strings.HasPrefix(err.Error(), "msg: auth-required:") && sec != "" && doAuth {
// if the relay is requesting auth and we can auth, let's do it
log("performing auth... ")
st, err := relay.Auth(c.Context, func(evt *nostr.Event) error { return evt.Sign(sec) })
if st == nostr.PublishStatusSucceeded {
pk, _ := nostr.GetPublicKey(sec)
log("performing auth as %s... ", pk)
if err := relay.Auth(c.Context, func(evt *nostr.Event) error { return evt.Sign(sec) }); err == nil {
// try to publish again, but this time don't try to auth again
doAuth = false
goto publish
} else {
// auth error
if err == nil {
err = fmt.Errorf("no response from relay")
}
log("auth error: %s. ", err)
}
}