@shocknet/clink-sdk
Version:
sdk client for clink
65 lines • 2.63 kB
JavaScript
import { nip44, getPublicKey } from "nostr-tools";
import { sendRequest } from "./sender.js";
const { getConversationKey, decrypt, encrypt } = nip44;
/* export const SendNdebitRequest = async (pool: AbstractSimplePool, privateKey: Uint8Array, relays: string[], pubKey: string, data: NdebitData, timeoutSeconds?: number): Promise<NdebitResponse> => {
const publicKey = getPublicKey(privateKey)
const content = encrypt(JSON.stringify(data), getConversationKey(privateKey, pubKey))
const event = newNdebitEvent(content, publicKey, pubKey)
const signed = finalizeEvent(event, privateKey)
await Promise.all(pool.publish(relays, signed))
return new Promise<NdebitResponse>((res, rej) => {
let closer: SubCloser = { close: () => { } }
let timer: NodeJS.Timeout | null = null
if (timeoutSeconds) {
timer = setTimeout(() => {
closer.close(); rej('failed to get ndebit response in time')
}, timeoutSeconds * 1000)
}
closer = pool.subscribeMany(relays, [newNdebitFilter(publicKey, signed.id)], {
onevent: async (e) => {
if (timer) clearTimeout(timer)
const content = decrypt(e.content, getConversationKey(privateKey, pubKey))
res(JSON.parse(content))
}
})
})
} */
export const SendNdebitRequest = async (pool, privateKey, relays, toPubKey, data, timeoutSeconds) => {
const publicKey = getPublicKey(privateKey);
const content = encrypt(JSON.stringify(data), getConversationKey(privateKey, toPubKey));
const event = newNdebitEvent(content, publicKey, toPubKey);
return sendRequest(pool, { privateKey, publicKey }, relays, toPubKey, event, 21002, timeoutSeconds);
};
export const newNdebitFullAccessRequest = (pointer) => {
return {
pointer: pointer
};
};
export const newNdebitPaymentRequest = (invoice, amount, pointer) => {
return {
bolt11: invoice,
amount_sats: amount,
pointer: pointer
};
};
export const newNdebitBudgetRequest = (frequency, amount, pointer) => {
return {
amount_sats: amount,
frequency: frequency,
pointer: pointer
};
};
export const newNdebitEvent = (content, fromPub, toPub) => ({
content,
created_at: Math.floor(Date.now() / 1000),
kind: 21002,
pubkey: fromPub,
tags: [['p', toPub], ['clink_version', '1']]
});
export const newNdebitFilter = (publicKey, eventId) => ({
since: Math.floor(Date.now() / 1000) - 1,
kinds: [21002],
'#p': [publicKey],
'#e': [eventId]
});
//# sourceMappingURL=ndebit.js.map