remove amber

This commit is contained in:
Jon Staab 2024-06-17 12:33:13 -07:00
parent a94fabdffb
commit c15453b996
8 changed files with 6 additions and 199 deletions

View File

@ -14,7 +14,7 @@ import {
REACTION,
} from "@welshman/util"
import {Tracker} from "@welshman/net"
import type {Feed} from "@welshman/feeds"
import type {Feed, RequestItem} from "@welshman/feeds"
import {walkFeed, FeedLoader as CoreFeedLoader} from "@welshman/feeds"
import {noteKinds, isLike, reactionKinds, repostKinds} from "src/util/nostr"
import {withGetter} from "src/util/misc"
@ -59,7 +59,7 @@ const prepFilters = (filters, opts: FeedOpts) => {
return filters
}
function* getRequestItems({relays, filters}, opts: FeedOpts) {
function* getRequestItems({relays, filters}: RequestItem, opts: FeedOpts) {
filters = prepFilters(filters, opts)
// Use relays specified in feeds
@ -135,7 +135,7 @@ export const createFeed = (opts: FeedOpts) => {
skipCache: true,
onEvent: prependEvent,
signal: controller.signal,
filters: filters.map(assoc('since', now())),
filters: filters.map(assoc("since", now())),
forcePlatform: opts.forcePlatform && (relays?.length || 0) === 0,
})
}

View File

@ -2,7 +2,6 @@
import cx from "classnames"
import {onMount} from "svelte"
import {last, prop, objOf} from "ramda"
import {tryCatch} from "@welshman/lib"
import {HANDLER_INFORMATION, NOSTR_CONNECT} from "@welshman/util"
import {tryJson} from "src/util/misc"
import {showWarning} from "src/partials/Toast.svelte"
@ -15,19 +14,15 @@
import {
load,
hints,
Amber,
loadHandle,
getExtension,
withExtension,
loginWithAmber,
loginWithExtension,
loginWithNostrConnect,
} from "src/engine"
import {router} from "src/app/util/router"
import {boot} from "src/app/state"
const amber = Amber.get()
const signUp = () => router.at("signup").replaceModal()
const useBunker = () => router.at("login/bunker").replaceModal()
@ -42,15 +37,6 @@
}
})
const useAmber = async () => {
const pubkey = await tryCatch(amber.getPubkey, e => showWarning(e.toString()))
if (pubkey) {
loginWithAmber(pubkey)
boot()
}
}
const usePrivateKey = () => router.at("login/privkey").replaceModal()
const usePublicKey = () => router.at("login/pubkey").replaceModal()
@ -196,14 +182,6 @@
<span>Extension</span>
</Tile>
{/if}
{#if amber.isEnabled()}
<Tile class="cursor-pointer bg-tinted-800" on:click={useAmber}>
<div>
<i class="fa fa-gem fa-xl" />
</div>
<span>Amber</span>
</Tile>
{/if}
<Tile class="cursor-pointer bg-tinted-800" on:click={usePrivateKey}>
<div>
<i class="fa fa-key fa-xl" />

View File

@ -919,8 +919,6 @@ export const loginWithPublicKey = pubkey => addSession({method: "pubkey", pubkey
export const loginWithExtension = pubkey => addSession({method: "extension", pubkey})
export const loginWithAmber = pubkey => addSession({method: "amber", pubkey})
export const loginWithNsecBunker = async (pubkey, connectToken, connectRelay) => {
const connectKey = generatePrivateKey()
const connectHandler = {relays: [connectRelay]}

View File

@ -1,160 +0,0 @@
import {defer} from "hurdak"
import {sleep, tryCatch, Worker} from "@welshman/lib"
import type {HashedEvent, SignedEvent} from "@welshman/util"
import {hasValidSignature} from "@welshman/util"
import {parsePubkey} from "src/util/nostr"
const createGetPublicKeyIntent = () =>
`intent:#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=signature;S.type=get_public_key;end`
const createSignEventIntent = (draft: HashedEvent) =>
`intent:${encodeURIComponent(
JSON.stringify(draft),
)}#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=signature;S.type=sign_event;end`
const createNip04EncryptIntent = (pubkey: string, plainText: string) =>
`intent:${encodeURIComponent(
plainText,
)}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip04_encrypt;end`
const createNip04DecryptIntent = (pubkey: string, data: string) =>
`intent:${encodeURIComponent(
data,
)}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip04_decrypt;end`
const createNip44EncryptIntent = (pubkey: string, plainText: string) =>
`intent:${encodeURIComponent(
plainText,
)}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip44_encrypt;end`
const createNip44DecryptIntent = (pubkey: string, data: string) =>
`intent:${encodeURIComponent(
data,
)}#Intent;scheme=nostrsigner;S.pubKey=${pubkey};S.compressionType=none;S.returnType=signature;S.type=nip44_decrypt;end`
class Request {
result = defer<{result?: string; error?: string}>()
constructor(readonly intent: string) {}
fulfill = async () => {
// Clear out the clipboard if we can
await tryCatch(() => navigator.clipboard.writeText(""))
// Send the intent to amber
const other = window.open(this.intent, "_blank")
// Wait a moment to avoid the visibilitychange listener firing before navigation
await sleep(500)
const cleanup = () => {
document.removeEventListener("visibilitychange", onVisibilityChange)
clearTimeout(timeout)
other.close()
}
const onResult = result => {
this.result.resolve({result})
cleanup()
}
const onError = error => {
this.result.resolve({error})
cleanup()
}
const timeout = setTimeout(() => onError("No data received."), 15000)
const onVisibilityChange = async () => {
await sleep(500)
if (document.visibilityState !== "visible") return
try {
const result = await navigator.clipboard.readText()
if (result) {
onResult(result)
}
} catch (e) {
// Pass, document isn't focused
}
}
document.addEventListener("visibilitychange", onVisibilityChange)
return this.result
}
}
let singleton: Amber
export class Amber {
worker = new Worker<Request>()
constructor() {
this.worker.addGlobalHandler(request => request.fulfill())
}
static get() {
if (!singleton) {
singleton = new Amber()
}
return singleton
}
_request = async (intent: string) => {
const request = new Request(intent)
this.worker.push(request)
return request.result.then(({result, error}) => {
if (error) {
throw error
}
return result
})
}
isEnabled = () => navigator.userAgent.includes("Android") && navigator.clipboard?.readText
getPubkey = async () => {
const result = await this._request(createGetPublicKeyIntent())
const pubkey = await parsePubkey(result)
if (!pubkey) {
throw new Error("Expected clipboard to have pubkey")
}
return pubkey
}
signEvent = async (draft: HashedEvent): Promise<SignedEvent> => {
const sig = await this._request(createSignEventIntent(draft))
if (!sig.match(/^[a-f0-9]+$/)) throw new Error("Expected hex signature")
const event: SignedEvent = {...draft, sig}
if (!hasValidSignature(event)) throw new Error("Invalid signature")
return event
}
nip04Encrypt = (pubkey: string, plaintext: string): Promise<string> =>
this._request(createNip04EncryptIntent(pubkey, plaintext))
nip04Decrypt = (pubkey: string, data: string): Promise<string> =>
this._request(createNip04DecryptIntent(pubkey, data))
nip44Encrypt = (pubkey: string, plaintext: string): Promise<string> =>
this._request(createNip44EncryptIntent(pubkey, plaintext))
nip44Decrypt = (pubkey: string, data: string): Promise<string> =>
this._request(createNip44DecryptIntent(pubkey, data))
}

View File

@ -5,7 +5,6 @@ import {Nip59} from "./nip59"
import {Signer} from "./signer"
import {Connect} from "./connect"
export * from "./amber"
export * from "./nip04"
export * from "./nip07"
export * from "./nip44"

View File

@ -3,7 +3,6 @@ import {switcherFn, tryFunc} from "hurdak"
import type {Session} from "src/engine/model"
import type {Connect} from "./connect"
import {withExtension} from "./nip07"
import {Amber} from "./amber"
export class Nip04 {
constructor(
@ -12,7 +11,7 @@ export class Nip04 {
) {}
isEnabled() {
return ["amber", "privkey", "connect", "extension"].includes(this.session?.method)
return ["privkey", "connect", "extension"].includes(this.session?.method)
}
async encrypt(message: string, pk: string, sk: string) {
@ -27,7 +26,6 @@ export class Nip04 {
const {method, privkey} = this.session
return switcherFn(method, {
amber: () => Amber.get().nip04Encrypt(pk, message),
privkey: () => this.encrypt(message, pk, privkey),
extension: () => withExtension(ext => ext.nip04.encrypt(pk, message)),
connect: () => this.connect.broker.nip04Encrypt(pk, message),
@ -38,7 +36,6 @@ export class Nip04 {
const {method, privkey} = this.session
return switcherFn(method, {
amber: () => Amber.get().nip04Decrypt(pk, message),
privkey: () => this.decrypt(message, pk, privkey),
extension: () => withExtension(ext => ext.nip04.decrypt(pk, message)),
connect: () => this.connect.broker.nip04Decrypt(pk, message),

View File

@ -6,7 +6,6 @@ import {fromHex} from "src/util/nostr"
import type {Session} from "src/engine/model"
import type {Connect} from "./connect"
import {withExtension} from "./nip07"
import {Amber} from "./amber"
// Deriving shared secret is an expensive computation, cache it
export const getSharedSecret = cached({
@ -22,7 +21,7 @@ export class Nip44 {
) {}
isEnabled() {
if (["amber", "privkey", "connect"].includes(this.session?.method)) {
if (["privkey", "connect"].includes(this.session?.method)) {
return true
}
@ -45,7 +44,6 @@ export class Nip44 {
const {method, privkey} = this.session
return switcherFn(method, {
amber: () => Amber.get().nip44Encrypt(pk, message),
privkey: () => this.encrypt(message, pk, privkey),
extension: () => withExtension(ext => ext.nip44.encrypt(pk, message)),
connect: () => this.connect.broker.nip44Encrypt(pk, message),
@ -56,7 +54,6 @@ export class Nip44 {
const {method, privkey} = this.session
return switcherFn(method, {
amber: () => Amber.get().nip44Decrypt(pk, message),
privkey: () => this.decrypt(message, pk, privkey),
extension: () => withExtension(ext => ext.nip44.decrypt(pk, message)),
connect: () => this.connect.broker.nip44Decrypt(pk, message),

View File

@ -5,7 +5,6 @@ import {getPublicKey, getSignature} from "src/util/nostr"
import type {Session} from "src/engine/model"
import type {Connect} from "./connect"
import {withExtension} from "./nip07"
import {Amber} from "./amber"
export class Signer {
constructor(
@ -14,7 +13,7 @@ export class Signer {
) {}
isEnabled() {
return ["amber", "connect", "privkey", "extension"].includes(this.session?.method)
return ["connect", "privkey", "extension"].includes(this.session?.method)
}
prepWithKey(event: EventTemplate, sk: string) {
@ -48,7 +47,6 @@ export class Signer {
const event = this.prepAsUser(template)
return switcherFn(method, {
amber: () => Amber.get().signEvent(event),
privkey: () => ({...event, sig: getSignature(event, privkey)}),
extension: () => withExtension(ext => ext.signEvent(event)),
connect: () => this.connect.broker.signEvent(template),