Add setting for auto unwrapping
This commit is contained in:
+32
-23
@@ -1,14 +1,7 @@
|
|||||||
import {derived, writable} from "svelte/store"
|
import {derived, writable} from "svelte/store"
|
||||||
import {cached, randomId, append, omit, equals, assoc} from "@welshman/lib"
|
import {cached, randomId, append, omit, equals, assoc} from "@welshman/lib"
|
||||||
import {withGetter} from "@welshman/store"
|
import {withGetter} from "@welshman/store"
|
||||||
import {
|
import {Wallet, WRAP, getPubkeyTagValues, HashedEvent, SignedEvent} from "@welshman/util"
|
||||||
Wallet,
|
|
||||||
WRAP,
|
|
||||||
isHashedEvent,
|
|
||||||
getPubkeyTagValues,
|
|
||||||
HashedEvent,
|
|
||||||
SignedEvent,
|
|
||||||
} from "@welshman/util"
|
|
||||||
import {
|
import {
|
||||||
Nip59,
|
Nip59,
|
||||||
WrappedSigner,
|
WrappedSigner,
|
||||||
@@ -284,39 +277,55 @@ export const nip44EncryptToSelf = (payload: string) => {
|
|||||||
return $signer.nip44.encrypt($pubkey!, payload)
|
return $signer.nip44.encrypt($pubkey!, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gift wrap utilities
|
||||||
|
|
||||||
export const wrapManager = new WrapManager({relay, tracker})
|
export const wrapManager = new WrapManager({relay, tracker})
|
||||||
|
|
||||||
export const unwrapAndStore = async (wrap: SignedEvent) => {
|
export const shouldUnwrap = withGetter(writable(false))
|
||||||
if (wrap.kind !== WRAP) throw new Error("Tried to unwrap an invalid event")
|
|
||||||
|
|
||||||
// First, check index and repository
|
export const failedUnwraps = new Set<string>()
|
||||||
|
|
||||||
|
export const unwrapAndStore = async (wrap: SignedEvent) => {
|
||||||
|
if (wrap.kind !== WRAP) {
|
||||||
|
throw new Error("Tried to unwrap an invalid event")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!shouldUnwrap.get()) {
|
||||||
|
throw new Error("Discarding wrapped event because `shouldUnwrap` is not enabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check to see if we already tried to unwrap but failed
|
||||||
|
if (failedUnwraps.has(wrap.id)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check index and repository
|
||||||
const cached = wrapManager.getRumor(wrap.id)
|
const cached = wrapManager.getRumor(wrap.id)
|
||||||
|
|
||||||
if (cached) {
|
if (cached) {
|
||||||
return cached
|
return cached
|
||||||
}
|
}
|
||||||
|
|
||||||
let rumor: HashedEvent | undefined
|
let result: {rumor: HashedEvent; recipient: string} | undefined
|
||||||
let recipient: string | undefined
|
|
||||||
|
|
||||||
// Next, try to decrypt as the recipient
|
// Next, try to decrypt as the recipient
|
||||||
for (const pubkey of getPubkeyTagValues(wrap.tags)) {
|
for (const recipient of getPubkeyTagValues(wrap.tags)) {
|
||||||
const signer = getSignerFromPubkey(pubkey)
|
const signer = getSignerFromPubkey(recipient)
|
||||||
|
|
||||||
if (signer) {
|
if (signer) {
|
||||||
try {
|
try {
|
||||||
rumor = await Nip59.fromSigner(signer).unwrap(wrap)
|
const rumor = await Nip59.fromSigner(signer).unwrap(wrap)
|
||||||
recipient = pubkey
|
|
||||||
break
|
result = {rumor, recipient}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// pass
|
failedUnwraps.add(wrap.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rumor && recipient && isHashedEvent(rumor)) {
|
if (result) {
|
||||||
wrapManager.add({wrap, rumor, recipient})
|
wrapManager.add({wrap, ...result})
|
||||||
}
|
|
||||||
|
|
||||||
return rumor
|
return result.rumor
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export const wrap = async (
|
|||||||
return wrap
|
return wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
export const unwrap = async (signer: ISigner, wrap: SignedEvent) => {
|
export const unwrap = async (signer: ISigner, wrap: SignedEvent): Promise<HashedEvent> => {
|
||||||
// Avoid decrypting the same event multiple times
|
// Avoid decrypting the same event multiple times
|
||||||
if (seen.has(wrap.id)) {
|
if (seen.has(wrap.id)) {
|
||||||
const rumorOrError = seen.get(wrap.id)
|
const rumorOrError = seen.get(wrap.id)
|
||||||
@@ -57,7 +57,7 @@ export const unwrap = async (signer: ISigner, wrap: SignedEvent) => {
|
|||||||
if (rumorOrError instanceof Error) {
|
if (rumorOrError instanceof Error) {
|
||||||
throw rumorOrError
|
throw rumorOrError
|
||||||
} else {
|
} else {
|
||||||
return rumorOrError
|
return rumorOrError!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user