Skip to content

Commit

Permalink
Allow cancel of own invoices without hmac (#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis authored Jan 2, 2025
1 parent 0ca9596 commit 6a02ea8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
13 changes: 11 additions & 2 deletions api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ export async function getWithdrawl (parent, { id }, { me, models, lnd }) {
}

export function createHmac (hash) {
if (!hash) throw new GqlInputError('hash required to create hmac')
const key = Buffer.from(process.env.INVOICE_HMAC_KEY, 'hex')
return crypto.createHmac('sha256', key).update(Buffer.from(hash, 'hex')).digest('hex')
}

export function verifyHmac (hash, hmac) {
if (!hash || !hmac) throw new GqlInputError('hash or hmac missing')
const hmac2 = createHmac(hash)
if (!timingSafeEqual(Buffer.from(hmac), Buffer.from(hmac2))) {
throw new GqlAuthorizationError('bad hmac')
Expand Down Expand Up @@ -487,8 +489,15 @@ const resolvers = {
},
createWithdrawl: createWithdrawal,
sendToLnAddr,
cancelInvoice: async (parent, { hash, hmac }, { models, lnd, boss }) => {
verifyHmac(hash, hmac)
cancelInvoice: async (parent, { hash, hmac }, { me, models, lnd, boss }) => {
// stackers can cancel their own invoices without hmac
if (me && !hmac) {
const inv = await models.invoice.findUnique({ where: { hash } })
if (!inv) throw new GqlInputError('invoice not found')
if (inv.userId !== me.id) throw new GqlInputError('not ur invoice')
} else {
verifyHmac(hash, hmac)
}
await finalizeHodlInvoice({ data: { hash }, lnd, models, boss })
return await models.invoice.findFirst({ where: { hash } })
},
Expand Down
2 changes: 1 addition & 1 deletion api/typeDefs/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const typeDefs = `
createInvoice(amount: Int!): InvoiceOrDirect!
createWithdrawl(invoice: String!, maxFee: Int!): Withdrawl!
sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl!
cancelInvoice(hash: String!, hmac: String!): Invoice!
cancelInvoice(hash: String!, hmac: String): Invoice!
dropBolt11(hash: String!): Boolean
removeWallet(id: ID!): Boolean
deleteWalletLogs(wallet: String): Boolean
Expand Down
4 changes: 0 additions & 4 deletions components/use-invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ export default function useInvoice () {
}, [client])

const cancel = useCallback(async ({ hash, hmac }) => {
if (!hash || !hmac) {
throw new Error('missing hash or hmac')
}

console.log('canceling invoice:', hash)
const { data } = await cancelInvoice({ variables: { hash, hmac } })
return data.cancelInvoice
Expand Down
2 changes: 1 addition & 1 deletion fragments/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export const SET_WALLET_PRIORITY = gql`

export const CANCEL_INVOICE = gql`
${INVOICE_FIELDS}
mutation cancelInvoice($hash: String!, $hmac: String!) {
mutation cancelInvoice($hash: String!, $hmac: String) {
cancelInvoice(hash: $hash, hmac: $hmac) {
...InvoiceFields
}
Expand Down

0 comments on commit 6a02ea8

Please sign in to comment.