aboutsummaryrefslogtreecommitdiffstats
path: root/db/sessions/e43362425e142ce46f6b127c2cb9655b
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-10 11:31:04 -0800
committerAdam Soltys <[email protected]>2026-02-10 11:31:04 -0800
commitf12cdb56c0b0c759c18ec789496394cd648e7cc1 (patch)
tree0d1d55a70b83b88a1c92d0fbcecd784637711fb3 /db/sessions/e43362425e142ce46f6b127c2cb9655b
parent0472ded5b52807f93ce5761ad884928008410e53 (diff)
downloadcoinos-server-f12cdb56c0b0c759c18ec789496394cd648e7cc1.tar.gz
coinos-server-f12cdb56c0b0c759c18ec789496394cd648e7cc1.zip
filter null managers
Diffstat (limited to 'db/sessions/e43362425e142ce46f6b127c2cb9655b')
0 files changed, 0 insertions, 0 deletions
[email protected]> 2024-01-09 23:10:35 -0800 committer Adam Soltys <[email protected]> 2024-01-09 23:10:39 -0800 remove users from archive once migrated' href='/coinos-server.git/commit/lib/utils.js?h=regtest-data&id=7a84d2555ffabbe29b23cf6e5035b6a4be8b9eb6'>7a84d255 ^
e3266dde ^
7a84d255 ^

d45fb3d4 ^

7a84d255 ^
d45fb3d4 ^


7a84d255 ^
3161dd7e ^
7a84d255 ^
2346e482 ^
7a84d255 ^


fee415dd ^
86cca842 ^
7a84d255 ^

e6cd93b5 ^
ef367905 ^
7a84d255 ^
057707f3 ^

02a373b8 ^
057707f3 ^


c28a1b61 ^
eb641b0e ^
057707f3 ^


ef367905 ^
057707f3 ^
ef367905 ^
2eb199b8 ^
7a84d255 ^
2eb199b8 ^















b67ca07b ^
2eb199b8 ^
0559a382 ^
7a84d255 ^
712560b8 ^
5269deaa ^
372c6357 ^
5269deaa ^
e4444d46 ^

5269deaa ^


0559a382 ^
2be8d681 ^





63aac564 ^

80470a2c ^





63aac564 ^








92e7cf4c ^














63aac564 ^
92e7cf4c ^





















63aac564 ^
92e7cf4c ^

63aac564 ^




















92e7cf4c ^
63aac564 ^
8c8c71d5 ^
80470a2c ^
0a2734df ^





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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
                                         
                                                              
                                   
                               
                             
                                     
 




                            
 

                                                               

                                                        
                              


             
                                                        
 
                                                        
 


                                                          
                                           
 

                                                                           
                                                                            
 
                                                              

                                        
                                                                      


                                  
                                                       
                                                                    


                                     
 
                                                   
  
 
                     















               
           
  
 
                                          
                                                     
                                     
                                        
                                           

                                             


              
  





                                           

  





                                                         








                                                














                                                                  
 





















                                                                         
                          

                                                   




















                                                                                
                
 
 
                                                          





                                                                        
import locales from "$lib/locales/index";
import { randomBytes, bytesToHex } from "@noble/hashes/utils";
import migrate from "$lib/migrate";
import { g, s } from "$lib/db";
import config from "$config";
import { getPublicKey } from "nostr";

export let fail = (msg) => {
  throw new Error(msg);
};

export let nada = () => {};

export let sleep = (n) => new Promise((r) => setTimeout(r, n));
export let wait = async (f, s = 300, n = 50) => {
  let i = 0;
  while (!(await f()) && i < s) (await sleep(n)) && i++;
  if (i >= s) fail("timeout");
  return f();
};

export let prod = process.env.NODE_ENV === "production";

export let bail = (res, msg) => res.code(500).send(msg);

export let SATS = 100000000;
export let sats = (n) => Math.round(n * SATS);
export let btc = (n) => parseFloat((n / SATS).toFixed(8));
export let fiat = (n, r) => (n * r) / SATS;

export let uniq = (a, k) => [...new Map(a.map((x) => [k(x), x])).values()];
export let pick = (O, K) =>
  K.reduce((o, k) => (typeof O[k] !== "undefined" && (o[k] = O[k]), o), {});

export let bip21 = (address, { amount, memo, tip, type }) => {
  if (!(amount || memo)) return address;

  let network = { liquid: "liquidnetwork", bitcoin: "bitcoin" }[type];
  let url = new URLSearchParams();

  if (amount) {
    url.append("amount", btc(amount + tip).toFixed(8));
    if (type === "liquid") url.append("assetid", config.liquid.btc);
  }

  if (memo) url.append("memo", memo);

  return `${network}:${address}?${url.toString()}`;
};

export let fields = [
  "cipher",
  "pubkey",
  "password",
  "username",
  "salt",
  "currency",
  "currencies",
  "fiat",
  "otpsecret",
  "balance",
  "ip",
  "pin",
  "display",
  "haspin",
  "profile",
  "prompt",
  "banner",
];

export let getUser = async (username) => {
  if (username === "undefined") fail("invalid user");
  let user = await migrate(username);
  if (user && !user.anon && !user.nwc) {
    user.nwc = bytesToHex(randomBytes(32));
    await s(getPublicKey(user.nwc), user.id);
    await s(`user:${user.id}`, user);
  }

  return user;
};

export let getInvoice = async (hash) => {
  let iid = await g(`invoice:${hash}`);
  if (iid && iid.id) iid = iid.id;
  else if (iid && iid.hash) iid = iid.hash;
  return await g(`invoice:${iid}`);
};

export let getPayment = async (id) => {
  let p = await g(`payment:${id}`);
  if (typeof p === "string") p = await g(`payment:${p}`);
  return p;
};

export let f = (s, currency) =>
  new Intl.NumberFormat("en-US", {
    style: "currency",
    currency,
  })
    .format(s)
    .replace("CA", "");

export function formatReceipt(items, currency) {
  function wrapText(text, maxWidth) {
    const words = text.split(" ");
    const lines = [];
    let currentLine = "";
    words.forEach((word) => {
      if (currentLine.length + word.length + 1 > maxWidth) {
        lines.push(currentLine);
        currentLine = word;
      } else {
        currentLine += (currentLine.length > 0 ? " " : "") + word;
      }
    });
    if (currentLine) lines.push(currentLine);
    return lines;
  }

  function calculateColumnWidths(items) {
    let maxQuantityLength = 0;
    let maxPriceLength = 0;

    items.forEach((item) => {
      const quantityLength = String(item.quantity).length;
      if (quantityLength > maxQuantityLength) {
        maxQuantityLength = quantityLength;
      }

      const priceLength = f(item.price * item.quantity, currency).length;
      if (priceLength > maxPriceLength) {
        maxPriceLength = priceLength;
      }
    });

    // Add padding to the widths for aesthetic spacing
    return {
      quantityColumnWidth: maxQuantityLength + 1, // Space after quantity
      priceColumnWidth: maxPriceLength + 1, // Space before price
    };
  }
  const maxLineWidth = 32;
  const { quantityColumnWidth, priceColumnWidth } =
    calculateColumnWidths(items);
  const nameColumnWidth = maxLineWidth - quantityColumnWidth - priceColumnWidth;

  return items
    .map((item) => {
      const quantityStr = String(item.quantity).padEnd(quantityColumnWidth);
      const priceStr = f(item.price * item.quantity, currency).padStart(
        priceColumnWidth,
      );
      const nameLines = wrapText(item.name, nameColumnWidth);

      // Construct the full line(s) with the first line including the price
      let fullLines = [
        `${quantityStr}${nameLines[0].padEnd(nameColumnWidth)}${priceStr}`,
      ];
      // Add any additional name lines, properly indented
      for (let i = 1; i < nameLines.length; i++) {
        fullLines.push(" ".repeat(quantityColumnWidth) + nameLines[i]);
      }

      return fullLines.join("\n");
    })
    .join("\n");
}

export let t = ({ language = "en" }) => locales[language];

export let chunk = (array, chunkSize) => array.reduce((all, one, i) => {
  const chunkIndex = Math.floor(i / chunkSize);
  (all[chunkIndex] = all[chunkIndex] || []).push(one);
  return all;
}, []);