@shocknet/clink-sdk
Version:
sdk client for clink
37 lines • 1.54 kB
JavaScript
import { nip44, getPublicKey, finalizeEvent } from "nostr-tools";
const { getConversationKey, decrypt, encrypt } = nip44;
export const SendNofferRequest = async (pool, privateKey, relays, toPubKey, data, timeoutSeconds = 30) => {
const publicKey = getPublicKey(privateKey);
const content = encrypt(JSON.stringify(data), getConversationKey(privateKey, toPubKey));
const event = newNofferEvent(content, publicKey, toPubKey);
const signed = finalizeEvent(event, privateKey);
await Promise.all(pool.publish(relays, signed));
return new Promise((res, rej) => {
let closer = { close: () => { } };
const timeout = setTimeout(() => {
closer.close();
rej("failed to get noffer response in time");
}, timeoutSeconds * 1000);
closer = pool.subscribeMany(relays, [newNofferFilter(publicKey, signed.id)], {
onevent: async (e) => {
clearTimeout(timeout);
const content = decrypt(e.content, getConversationKey(privateKey, toPubKey));
res(JSON.parse(content));
}
});
});
};
export const newNofferEvent = (content, fromPub, toPub) => ({
content,
created_at: Math.floor(Date.now() / 1000),
kind: 21001,
pubkey: fromPub,
tags: [['p', toPub], ['clink_version', '1']]
});
export const newNofferFilter = (publicKey, eventId) => ({
since: Math.floor(Date.now() / 1000) - 1,
kinds: [21001],
'#p': [publicKey],
'#e': [eventId]
});
//# sourceMappingURL=noffer.js.map