Skip to content

Commit

Permalink
feat: allow custom anchor service urls on mainnet (#3240)
Browse files Browse the repository at this point in the history
* feat: allow custom anchor service urls on mainnet

* chore: update log for custom cas url

Co-authored-by: Spencer T Brody <[email protected]>

* chore: add gitcoin cas api to approved mainnet urls

* chore: run prettier

---------

Co-authored-by: Spencer T Brody <[email protected]>
  • Loading branch information
smrz2001 and stbrody authored Jun 13, 2024
1 parent 14c603b commit 8d7306a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
19 changes: 6 additions & 13 deletions packages/core/src/initialization/__tests__/anchoring.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, test } from '@jest/globals'
import {
CustomMainnetCasError,
DEFAULT_ANCHOR_SERVICE_URLS,
makeAnchorService,
makeAnchorServiceUrl,
Expand All @@ -14,29 +13,23 @@ import {

describe('makeAnchorServiceUrl', () => {
const CUSTOM_URL = 'https://cas.com'
const logger = new LoggerProvider().getDiagnosticsLogger()

test('pass from config', () => {
expect(makeAnchorServiceUrl('hello', Networks.INMEMORY)).toEqual('hello')
expect(makeAnchorServiceUrl('hello', Networks.INMEMORY, logger)).toEqual('hello')
})
test('drop trailing slashes', () => {
const trailingSlash = `${CUSTOM_URL}/`
const doubleTrailingSlash = `${CUSTOM_URL}//`
expect(makeAnchorServiceUrl(CUSTOM_URL, Networks.INMEMORY)).toEqual(CUSTOM_URL)
expect(makeAnchorServiceUrl(trailingSlash, Networks.INMEMORY)).toEqual(CUSTOM_URL)
expect(makeAnchorServiceUrl(doubleTrailingSlash, Networks.INMEMORY)).toEqual(CUSTOM_URL)
expect(makeAnchorServiceUrl(CUSTOM_URL, Networks.INMEMORY, logger)).toEqual(CUSTOM_URL)
expect(makeAnchorServiceUrl(trailingSlash, Networks.INMEMORY, logger)).toEqual(CUSTOM_URL)
expect(makeAnchorServiceUrl(doubleTrailingSlash, Networks.INMEMORY, logger)).toEqual(CUSTOM_URL)
})
test('pass default by network', () => {
expect(makeAnchorServiceUrl(undefined, Networks.MAINNET)).toEqual(
expect(makeAnchorServiceUrl(undefined, Networks.MAINNET, logger)).toEqual(
DEFAULT_ANCHOR_SERVICE_URLS[Networks.MAINNET]
)
})
test('throw on custom CAS on mainnet', () => {
expect(() => makeAnchorServiceUrl(CUSTOM_URL, Networks.MAINNET)).toThrow(CustomMainnetCasError)
const casInternal = 'https://cas-internal.3boxlabs.com'
expect(makeAnchorServiceUrl(casInternal, Networks.MAINNET)).toEqual(casInternal)
const casDirect = 'https://cas-direct.3boxlabs.com'
expect(makeAnchorServiceUrl(casDirect, Networks.MAINNET)).toEqual(casDirect)
})
})

describe('makeAnchorService', () => {
Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/initialization/anchoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,24 @@ export class UnusableAnchorChainsError extends Error {
}
}

export class CustomMainnetCasError extends Error {
constructor() {
super('Cannot use custom anchor service on Ceramic mainnet')
}
}

const TRAILING_SLASH = new RegExp(/\/+$/g) // slash at the end of the string
const MAINNET_CAS_URLS = [
'https://cas-internal.3boxlabs.com',
'https://cas-direct.3boxlabs.com',
'https://ceramic-prod-cas-api-gitcoin.3boxlabs.com',
DEFAULT_ANCHOR_SERVICE_URLS[Networks.MAINNET],
]
export function makeAnchorServiceUrl(fromConfig: string | undefined, network: Networks): string {
export function makeAnchorServiceUrl(
fromConfig: string | undefined,
network: Networks,
logger: DiagnosticsLogger
): string {
const casUrl = fromConfig?.replace(TRAILING_SLASH, '') || DEFAULT_ANCHOR_SERVICE_URLS[network]
// Log a warning when using a custom anchor service URL on mainnet
if (isMainnet(network) && !MAINNET_CAS_URLS.includes(casUrl)) {
throw new CustomMainnetCasError()
logger.warn(
`${casUrl} is not a standard Anchor Service URL for Ceramic mainnet. Use a custom anchor service URL at your own risk. Note than an unreliable Ceramic Anchor Service can lead to data loss.`
)
}
return casUrl
}
Expand Down Expand Up @@ -124,7 +126,7 @@ export function makeAnchorService(
if (network === Networks.INMEMORY) {
return new InMemoryAnchorService(config as any, logger)
}
const anchorServiceUrl = makeAnchorServiceUrl(config.anchorServiceUrl, network)
const anchorServiceUrl = makeAnchorServiceUrl(config.anchorServiceUrl, network, logger)
if (!config.readOnly) {
const anchorServiceAuth = makeAnchorServiceAuth(
config.anchorServiceAuthMethod,
Expand Down

0 comments on commit 8d7306a

Please sign in to comment.