aboutsummaryrefslogtreecommitdiffstats
path: root/db/models/withdrawals.js
blob: fa2afb224d4d4426655d8c1a5cd83fceddcf1329 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import db from "$db/db";
import { DataTypes } from '@sequelize/core';

const attributes = {
  id: {
    type: DataTypes.INTEGER(11),
    allowNull: false,
    defaultValue: null,
    primaryKey: true,
    autoIncrement: true,
    comment: null,
    field: "id"
  },
  amount: {
    type: DataTypes.DOUBLE,
    allowNull: true,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "amount"
  },
  user_id: {
    type: DataTypes.INTEGER(11),
    allowNull: true,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "user_id"
  },
  createdAt: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "createdAt"
  },
  updatedAt: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "updatedAt"
  },
  transit: {
    type: DataTypes.STRING(255),
    allowNull: true,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "transit"
  },
  institution: {
    type: DataTypes.STRING(255),
    allowNull: true,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "institution"
  },
  account: {
    type: DataTypes.STRING(255),
    allowNull: true,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "account"
  },
  email: {
    type: DataTypes.STRING(255),
    allowNull: true,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "email"
  },
  notes: {
    type: DataTypes.TEXT,
    allowNull: true,
    defaultValue: null,
    primaryKey: false,
    autoIncrement: false,
    comment: null,
    field: "notes"
  },
};

const options = {
  tableName: "withdrawals",
  comment: "",
  indexes: []
};

db["Withdrawal"] = db.define("withdrawals_model", attributes, options);
lass="kd">const getPrivateKey = async (user: User): Promise<Uint8Array> => { let k; if (browser) { k = localStorage.getItem("nsec"); if (k) { return nip19.decode(k).data as Uint8Array; } } const { nsec } = user; if (nsec) { k = nip49decrypt(nsec, await getPassword()); } else { throw new Error("nsec not available"); } localStorage.setItem("nsec", nip19.nsecEncode(k)); return k; }; const decodeNsec = (nsec: string): Uint8Array => { const { type, data } = nip19.decode(nsec); if (type === "nsec" && data instanceof Uint8Array) return data; throw new Error("invalid nsec"); }; export const getNsec = async (user: User) => { return nip19.nsecEncode(await getPrivateKey(user)); }; export const setNsec = async (user: User, nsec: string) => { user.pubkey = getPublicKey(decodeNsec(nsec)); user.nsec = await encryptNsec(nsec); }; export const encryptNsec = async (nsec: string) => { const d = decodeNsec(nsec); return nip49encrypt(d, await getPassword()); }; export const sign = async (event, user) => { if (user?.nsec && !get($signer)?.ready) { const sk = await getPrivateKey(user); $signer.set({ method: "nsec", ready: true, params: { sk: bytesToHex(sk), pk: getPublicKey(sk) }, }); } eventToSign.set(event); let unsubscribe; const { method, params } = await new Promise((r) => { unsubscribe = $signer.subscribe((v) => v?.ready && r(v)); }); await unsubscribe(); const signedEvent = await signingMethods[method](event, params); eventToSign.set(signedEvent); setTimeout(() => eventToSign.set(null), 1000); return signedEvent; }; export const send = (event: EventTemplate) => { return post("/events", { event }); }; const getPassword = async (): Promise<string> => { if (!get(pw)) passwordPrompt.set(true); await wait(() => !!get(pw)); return get(pw) || ""; }; export const nostrConnectRelay = "wss://relay.nsec.app"; const signingMethods = { async connect(event, { sk, pk, pubkey }) { const id = crypto.randomUUID(); const signEvent = { id, method: "sign_event", params: [JSON.stringify(event)], }; const content = await nip04.encrypt(sk, pk, JSON.stringify(signEvent)); const signedSignEvent = finalizeEvent( { created_at: Math.round(Date.now() / 1000), kind: 24133, pubkey, content, tags: [["p", pk]], }, sk, ); const relay = await Relay.connect(nostrConnectRelay); relay.publish(signedSignEvent); return new Promise((resolve) => { relay.subscribe([{ kinds: [24133], "#p": [pubkey] }], { async onevent(event) { pk = event.pubkey; try { let response; try { response = JSON.parse(await nip04.decrypt(sk, pk, event.content)); } catch (e) { const ck = await getConversationKey(sk, pk); response = JSON.parse(await nip44decrypt(event.content, ck)); } if (response.id === id) resolve(JSON.parse(response.result)); } catch (e) { console.log("failed to parse nostr connect event", e); } }, }); }); }, async nsec(event, { sk }) { return finalizeEvent(event, sk); }, async extension(event) { return window.nostr.signEvent(event); }, async signer(event, { pubkey, sig }) { event.pubkey = pubkey; const signedEvent = { ...event, id: getEventHash(event), sig, }; return signedEvent; }, };