Skip to content

Commit

Permalink
Merge branch 'master' into bolt12a
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl authored Dec 22, 2024
2 parents 0e56bc8 + 0ca3703 commit cc993ff
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 46 deletions.
5 changes: 3 additions & 2 deletions api/resolvers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -1381,10 +1381,11 @@ export const updateItem = async (parent, { sub: subName, forward, hash, hmac, ..

const user = await models.user.findUnique({ where: { id: meId } })

// edits are only allowed for own items within 10 minutes but forever if it's their bio or a job
// edits are only allowed for own items within 10 minutes
// but forever if an admin is editing an "admin item", it's their bio or a job
const myBio = user.bioId === old.id
const timer = Date.now() < datePivot(new Date(old.invoicePaidAt ?? old.createdAt), { seconds: ITEM_EDIT_SECONDS })
const canEdit = (timer && ownerEdit) || myBio || isJob(item)
const canEdit = (timer && ownerEdit) || adminEdit || myBio || isJob(item)
if (!canEdit) {
throw new GqlInputError('item can no longer be edited')
}
Expand Down
18 changes: 9 additions & 9 deletions api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ async function upsertWallet (
}
}

const { id, enabled, priority, ...walletData } = data
const { id, enabled, priority, ...recvConfig } = data

const txs = []

Expand All @@ -812,13 +812,13 @@ async function upsertWallet (
data: {
enabled,
priority,
// client only wallets has no walletData
...(Object.keys(walletData).length > 0
// client only wallets have no receive config and thus don't have their own table
...(Object.keys(recvConfig).length > 0
? {
[wallet.field]: {
update: {
where: { walletId: Number(id) },
data: walletData
upsert: {
create: recvConfig,
update: recvConfig
}
}
}
Expand Down Expand Up @@ -857,8 +857,8 @@ async function upsertWallet (
priority,
userId: me.id,
type: wallet.type,
// client only wallets has no walletData
...(Object.keys(walletData).length > 0 ? { [wallet.field]: { create: walletData } } : {}),
// client only wallets have no receive config and thus don't have their own table
...(Object.keys(recvConfig).length > 0 ? { [wallet.field]: { create: recvConfig } } : {}),
...(vaultEntries
? {
vaultEntries: {
Expand All @@ -882,7 +882,7 @@ async function upsertWallet (
)
}

if (canReceive({ def: walletDef, config: walletData })) {
if (canReceive({ def: walletDef, config: recvConfig })) {
txs.push(
models.walletLog.createMany({
data: {
Expand Down
2 changes: 1 addition & 1 deletion components/boost-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Boost ({ item, className, ...props }) {
item={item} As={oprops =>
<div className='upvoteParent'>
<div
className={styles.upvoteWrapper}
className={classNames(styles.upvoteWrapper, item.deletedAt && styles.noSelfTips)}
>
<BoostIcon
{...props}
Expand Down
7 changes: 5 additions & 2 deletions components/nostr-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
error: false,
loading: true
})

const nostr = new Nostr()
try {
const { data, error } = await createAuth()
if (error) throw error
Expand All @@ -125,7 +127,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
if (!k1) throw new Error('Error generating challenge') // should never happen

const useExtension = !nip46token
const signer = Nostr.getSigner({ nip46token, supportNip07: useExtension })
const signer = nostr.getSigner({ nip46token, supportNip07: useExtension })
if (!signer && useExtension) throw new Error('No extension found')

if (signer instanceof NDKNip46Signer) {
Expand All @@ -142,7 +144,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
loading: true
})

const signedEvent = await Nostr.sign({
const signedEvent = await nostr.sign({
kind: 27235,
created_at: Math.floor(Date.now() / 1000),
tags: [
Expand All @@ -161,6 +163,7 @@ export function NostrAuth ({ text, callbackUrl, multiAuth }) {
} catch (e) {
setError(e)
} finally {
nostr.close()
clearSuggestionTimer()
}
}, [])
Expand Down
2 changes: 1 addition & 1 deletion components/upvote.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}

.noSelfTips {
transform: scaleX(-1);
visibility: hidden;
}

.upvoteWrapper:not(.noSelfTips):hover {
Expand Down
5 changes: 4 additions & 1 deletion components/use-crossposter.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ export default function useCrossposter () {
if (!event) return { allSuccessful, noteId }

do {
const nostr = new Nostr()
try {
const result = await Nostr.crosspost(event, { relays: failedRelays || relays })
const result = await nostr.crosspost(event, { relays: failedRelays || relays })

if (result.error) {
failedRelays = []
Expand Down Expand Up @@ -231,6 +232,8 @@ export default function useCrossposter () {
// wait 2 seconds to show error then break
await new Promise(resolve => setTimeout(resolve, 2000))
return { allSuccessful, noteId }
} finally {
nostr.close()
}
} while (failedRelays.length > 0)

Expand Down
15 changes: 13 additions & 2 deletions components/vault/use-vault-configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function useVaultConfigurator ({ onVaultKeySet, beforeDisconnectVault } =
beforeDisconnectVault?.()
await remove('key')
keyReactiveVar(null)
}, [remove, keyReactiveVar])
}, [remove, keyReactiveVar, beforeDisconnectVault])

useEffect(() => {
if (!me) return
Expand Down Expand Up @@ -94,6 +94,17 @@ export function useVaultConfigurator ({ onVaultKeySet, beforeDisconnectVault } =

await updateVaultKey({
variables: { entries, hash: vaultKey.hash },
update: (cache, { data }) => {
cache.modify({
id: `User:${me.id}`,
fields: {
privates: (existing) => ({
...existing,
vaultKeyHash: vaultKey.hash
})
}
})
},
onError: (error) => {
const errorCode = error.graphQLErrors[0]?.extensions?.code
if (errorCode === E_VAULT_KEY_EXISTS) {
Expand All @@ -110,7 +121,7 @@ export function useVaultConfigurator ({ onVaultKeySet, beforeDisconnectVault } =
console.error('error setting vault key', e)
toaster.danger(e.message)
}
}, [getVaultEntries, updateVaultKey, set, get, remove, onVaultKeySet, keyReactiveVar])
}, [getVaultEntries, updateVaultKey, set, get, remove, onVaultKeySet, keyReactiveVar, me?.id])

return { key, setVaultKey, clearVault, disconnectVault }
}
Expand Down
25 changes: 23 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ services:
CLI: "bitcoin-cli"
CLI_ARGS: "-chain=regtest -rpcport=${RPC_PORT} -rpcuser=${RPC_USER} -rpcpassword=${RPC_PASS}"
ofelia.enabled: "true"
ofelia.group: "payments"
ofelia.job-exec.minecron.schedule: "@every 1m"
ofelia.job-exec.minecron.command: >
bash -c '
Expand Down Expand Up @@ -363,6 +364,7 @@ services:
CLI: "lncli"
CLI_USER: "lnd"
ofelia.enabled: "true"
ofelia.group: "payments"
ofelia.job-exec.sn_channel_cron.schedule: "@every 1m"
ofelia.job-exec.sn_channel_cron.command: >
su lnd -c bash -c "
Expand Down Expand Up @@ -462,6 +464,7 @@ services:
CLI: "lncli"
CLI_USER: "lnd"
ofelia.enabled: "true"
ofelia.group: "payments"
ofelia.job-exec.lnd_channel_cron.schedule: "@every 1m"
ofelia.job-exec.lnd_channel_cron.command: >
su lnd -c bash -c "
Expand Down Expand Up @@ -551,13 +554,14 @@ services:
CLI_USER: "clightning"
CLI_ARGS: "--regtest"
ofelia.enabled: "true"
ofelia.group: "wallets"
ofelia.job-exec.cln_channel_cron.schedule: "@every 1m"
ofelia.job-exec.cln_channel_cron.command: >
su clightning -c bash -c "
lightning-cli --regtest connect $ROUTER_LND_PUBKEY@router_lnd:9735
if [ $$(lightning-cli --regtest getinfo | jq '.num_active_channels + .num_pending_channels') -ge 3 ]; then
exit 0
else
lightning-cli --regtest connect $ROUTER_LND_PUBKEY@router_lnd:9735
lightning-cli --regtest fundchannel id=$ROUTER_LND_PUBKEY feerate=1000perkb \\
amount=1000000000 push_msat=500000000000 minconf=0
fi
Expand Down Expand Up @@ -610,6 +614,7 @@ services:
CLI_USER: "root"
CLI_ARGS: "-p pass"
ofelia.enabled: "true"
ofelia.group: "wallets"
ofelia.job-exec.eclair_channel_cron.schedule: "@every 1m"
ofelia.job-exec.eclair_channel_cron.command: >
bash -c "
Expand Down Expand Up @@ -670,6 +675,7 @@ services:
labels:
CLI: "lncli"
CLI_USER: "lnd"
ofelia.group: "payments"
cpu_shares: "${CPU_SHARES_MODERATE}"
channdler:
image: mcuadros/ofelia:latest
Expand All @@ -680,8 +686,23 @@ services:
- bitcoin
- sn_lnd
- lnd
- router_lnd
restart: unless-stopped
command: daemon --docker -f label=com.docker.compose.project=${COMPOSE_PROJECT_NAME} -f label=ofelia.group=payments
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
cpu_shares: "${CPU_SHARES_LOW}"
walletscron:
image: mcuadros/ofelia:latest
container_name: walletscron
profiles:
- wallets
depends_on:
- router_lnd
- eclair
- cln
restart: unless-stopped
command: daemon --docker -f label=com.docker.compose.project=${COMPOSE_PROJECT_NAME}
command: daemon --docker -f label=com.docker.compose.project=${COMPOSE_PROJECT_NAME} -f label=ofelia.group=wallets
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
cpu_shares: "${CPU_SHARES_LOW}"
Expand Down
4 changes: 2 additions & 2 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,5 @@ export const EXTRA_LONG_POLL_INTERVAL = Number(process.env.NEXT_PUBLIC_EXTRA_LON

export const ZAP_UNDO_DELAY_MS = 5_000

export const WALLET_SEND_PAYMENT_TIMEOUT_MS = 15_000
export const WALLET_CREATE_INVOICE_TIMEOUT_MS = 15_000
export const WALLET_SEND_PAYMENT_TIMEOUT_MS = 150_000
export const WALLET_CREATE_INVOICE_TIMEOUT_MS = 45_000
29 changes: 22 additions & 7 deletions lib/nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export const RELAYS_BLACKLIST = []
* @property {function(Object, {privKey: string, signer: NDKSigner}): Promise<NDKEvent>} sign
* @property {function(Object, {relays: Array<string>, privKey: string, signer: NDKSigner}): Promise<NDKEvent>} publish
*/
export class Nostr {
export default class Nostr {
/**
* @type {NDK}
*/
_ndk = null

static globalInstance = null
constructor ({ privKey, defaultSigner, relays, nip46token, supportNip07 = false, ...ndkOptions } = {}) {
this._ndk = new NDK({
explicitRelayUrls: relays,
Expand All @@ -47,6 +47,16 @@ export class Nostr {
})
}

/**
* @type {NDK}
*/
static get () {
if (!Nostr.globalInstance) {
Nostr.globalInstance = new Nostr()
}
return Nostr.globalInstance
}

/**
* @type {NDK}
*/
Expand Down Expand Up @@ -151,12 +161,17 @@ export class Nostr {
return { error }
}
}
}

/**
* @type {Nostr}
*/
export default new Nostr()
/**
* Close all relay connections
*/
close () {
const pool = this.ndk.pool
for (const relay of pool.urls()) {
pool.removeRelay(relay)
}
}
}

export function hexToBech32 (hex, prefix = 'npub') {
return bech32.encode(prefix, bech32.toWords(Buffer.from(hex, 'hex')))
Expand Down
Loading

0 comments on commit cc993ff

Please sign in to comment.