Skip to content

Commit

Permalink
support base64 identifier in macaroon
Browse files Browse the repository at this point in the history
  • Loading branch information
bucko13 committed Feb 22, 2022
1 parent 8c77b73 commit 4b1d846
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
19 changes: 18 additions & 1 deletion src/identifier.ts
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert')
const bufio = require('bufio')
import crypto from 'crypto'
import uuidv4 from 'uuid/v4'
import * as Macaroon from 'macaroon'

import { IdentifierOptions } from './types'

Expand Down Expand Up @@ -88,7 +89,11 @@ export class Identifier extends bufio.Struct {
}

static fromString(str: string): Identifier {
return new this().fromHex(str)
try {
return new this().fromHex(str)
} catch (e) {
return new this().fromBase64(str)
}
}

/**
Expand Down Expand Up @@ -137,3 +142,15 @@ export class Identifier extends bufio.Struct {
}
}
}

export const decodeIdentifierFromMacaroon = (raw: string): string => {
const macaroon = Macaroon.importMacaroon(raw)
let identifier = macaroon._exportAsJSONObjectV2().i
if (identifier == undefined) {
identifier = macaroon._exportAsJSONObjectV2().i64
if (identifier == undefined) {
throw new Error(`Problem parsing macaroon identifier`)
}
}
return identifier
}
21 changes: 4 additions & 17 deletions src/lsat.ts
Expand Up @@ -4,7 +4,7 @@ const bufio = require('bufio')
import crypto from 'crypto'
import * as Macaroon from 'macaroon'

import { Caveat, Identifier } from '.'
import { Caveat, decodeIdentifierFromMacaroon, Identifier } from '.'
import { LsatOptions } from './types'
import { isHex, getIdFromRequest, decode } from './helpers'

Expand Down Expand Up @@ -306,14 +306,9 @@ export class Lsat extends bufio.Struct {
*/
static fromMacaroon(macaroon: string, invoice?: string): Lsat {
assert(typeof macaroon === 'string', 'Requires a raw macaroon string for macaroon to generate LSAT')
const identifier = Macaroon.importMacaroon(macaroon)._exportAsJSONObjectV2().i
let id: Identifier
let id: Identifier, identifier: string
try {
if (identifier == undefined) {
throw new Error(
`macaroon identifier undefined`
)
}
identifier = decodeIdentifierFromMacaroon(macaroon)
id = Identifier.fromString(identifier)
} catch (e:any) {
throw new Error(
Expand Down Expand Up @@ -410,15 +405,7 @@ export class Lsat extends bufio.Struct {
)

const paymentHash = getIdFromRequest(invoice)
let identifier
const mac = Macaroon.importMacaroon(macaroon)
identifier = mac._exportAsJSONObjectV2().i
if (identifier == undefined){
identifier = mac._exportAsJSONObjectV2().i64
if (identifier == undefined){
throw new Error(`Problem parsing macaroon identifier`)
}
}
const identifier = decodeIdentifierFromMacaroon(macaroon)

return new this({
id: identifier,
Expand Down
9 changes: 9 additions & 0 deletions tests/identifier.spec.ts
Expand Up @@ -6,7 +6,9 @@ import {
LATEST_VERSION,
TOKEN_ID_SIZE,
ErrUnknownVersion,
decodeIdentifierFromMacaroon,
} from '../src'
import { testChallenges } from './data'

describe('Macaroon Identifier', () => {
it('should properly serialize identifier of known version', () => {
Expand All @@ -33,4 +35,11 @@ describe('Macaroon Identifier', () => {
const encodeId = (): Identifier => new Identifier(options)
expect(encodeId).to.throw(ErrUnknownVersion, options.version.toString())
})

it('can decode from different macaroon types', () => {
for (const { macaroon } of testChallenges) {
const id = decodeIdentifierFromMacaroon(macaroon)
Identifier.fromString(id)
}
})
})
42 changes: 32 additions & 10 deletions tests/lsat.spec.ts
@@ -1,8 +1,18 @@
import { expect } from 'chai'

import * as Macaroon from "macaroon";
import { Caveat, Lsat, parseChallengePart } from '../src'
import { testChallengeParts, invoice, testChallenges, testChallengeErrors } from './data'
import * as Macaroon from 'macaroon'
import {
Caveat,
decodeIdentifierFromMacaroon,
Lsat,
parseChallengePart,
} from '../src'
import {
testChallengeParts,
invoice,
testChallenges,
testChallengeErrors,
} from './data'
import { getTestBuilder } from './utilities'
import { decode } from '../src/helpers'

Expand Down Expand Up @@ -41,9 +51,15 @@ describe('LSAT Token', () => {
})

it('should be able to decode lsat challenges', () => {
for (const {name, challenge, macaroon, paymentHash, expiration} of testChallenges) {
for (const {
name,
challenge,
macaroon,
paymentHash,
expiration,
} of testChallenges) {
const fromChallenge = (): Lsat => Lsat.fromChallenge(challenge)

expect(fromChallenge, `${name} should not have thrown`).to.not.throw()
const lsat = fromChallenge()
expect(lsat.baseMacaroon).to.equal(
Expand All @@ -59,26 +75,25 @@ describe('LSAT Token', () => {
expiration,
`expiration from ${name} LSAT did not match`
)
else
expect(lsat.validUntil).to.equal(0)
else expect(lsat.validUntil).to.equal(0)
}
})

it('should be able to decode header challenges', () => {
for (const {name, challenge} of testChallenges) {
for (const { name, challenge } of testChallenges) {
const header = `LSAT ${challenge}`
const fromHeader = (): Lsat => Lsat.fromHeader(header)
expect(fromHeader, `${name} should not have thrown`).to.not.throw()
}
})

it('should fail on incorrectly encoded challenges', () => {
for (const {name, challenge, error} of testChallengeErrors) {
for (const { name, challenge, error } of testChallengeErrors) {
const fromChallenge = (): Lsat => Lsat.fromChallenge(challenge)
expect(fromChallenge, `${name} should not have thrown`).to.throw(error)
}
})

it('should be able to check expiration to see if expired', () => {
const lsat = Lsat.fromChallenge(challenge)
expect(lsat.isExpired()).to.be.false
Expand Down Expand Up @@ -228,4 +243,11 @@ describe('LSAT Token', () => {
const addInvalidInv = (): void => lsat.addInvoice('12345')
expect(addInvalidInv).to.throw()
})

it('test macaroon versions', () => {
for (const { macaroon, name } of testChallenges) {
const test = () => Lsat.fromMacaroon(macaroon)
expect(test, `${name} should not have thrown`).to.not.throw()
}
})
})

0 comments on commit 4b1d846

Please sign in to comment.