diff --git a/src/onramp/generateOnRampURL.test.ts b/src/onramp/generateOnRampURL.test.ts index 2cea051..dc05875 100644 --- a/src/onramp/generateOnRampURL.test.ts +++ b/src/onramp/generateOnRampURL.test.ts @@ -19,7 +19,6 @@ describe('generateOnrampURL', () => { const url = new URL( generateOnRampURL({ sessionToken: 'test', - destinationWallets: [], }), ); expect(url.origin).toEqual('https://pay.coinbase.com'); @@ -27,6 +26,26 @@ describe('generateOnrampURL', () => { expect(url.searchParams.get('sessionToken')).toEqual('test'); }); + it('should throw when both sessionToken and destinationWallets or addresses are provided', () => { + expect(() => + generateOnRampURL({ + sessionToken: 'test', + destinationWallets: [], + }), + ).toThrowError( + 'When sessionToken is provided, destinationWallets and addresses should not be specified', + ); + + expect(() => + generateOnRampURL({ + sessionToken: 'test', + addresses: {}, + }), + ).toThrowError( + 'When sessionToken is provided, destinationWallets and addresses should not be specified', + ); + }); + it('generates URL with empty destination wallets', () => { const url = new URL( generateOnRampURL({ diff --git a/src/onramp/generateOnRampURL.ts b/src/onramp/generateOnRampURL.ts index f607fdf..0210fe5 100644 --- a/src/onramp/generateOnRampURL.ts +++ b/src/onramp/generateOnRampURL.ts @@ -18,10 +18,18 @@ export const generateOnRampURL = ({ const url = new URL(host); url.pathname = '/buy/select-asset'; - if (props.destinationWallets && props.addresses) { - throw new Error('Only one of destinationWallets or addresses can be provided'); - } else if (!props.destinationWallets && !props.addresses) { - throw new Error('One of destinationWallets or addresses must be provided'); + if (props.sessionToken) { + if (props.destinationWallets || props.addresses) { + throw new Error( + 'When sessionToken is provided, destinationWallets and addresses should not be specified', + ); + } + } else { + if (props.destinationWallets && props.addresses) { + throw new Error('Only one of destinationWallets or addresses can be provided'); + } else if (!props.destinationWallets && !props.addresses) { + throw new Error('One of destinationWallets or addresses must be provided'); + } } (Object.keys(props) as (keyof typeof props)[]).forEach((key) => { diff --git a/src/types/onramp.ts b/src/types/onramp.ts index 59f5308..f534ba8 100644 --- a/src/types/onramp.ts +++ b/src/types/onramp.ts @@ -60,6 +60,13 @@ type BaseOnRampAppParams = { * `{ "0x1": ["base"] }` */ addresses?: Record; + /** + * The session token used to initialize the Coinbase Onramp experience. + * This token expires after a short period of time and can only be used once. + * A new token must be obtained for every new session. + * @see https://docs.cdp.coinbase.com/onramp/docs/api-initializing#getting-an-onramp-session-token + */ + sessionToken?: string; /** * This optional parameter will restrict the assets available for the user to buy/send. It acts as a filter on the * networks specified in the {addresses} param. diff --git a/src/utils/CoinbasePixel.test.ts b/src/utils/CoinbasePixel.test.ts index 7592f51..9182c26 100644 --- a/src/utils/CoinbasePixel.test.ts +++ b/src/utils/CoinbasePixel.test.ts @@ -50,6 +50,17 @@ describe('CoinbasePixel', () => { expect(instance.appParams).toEqual(defaultArgs.appParams); }); + it('should initialize with sessionToken', () => { + const sessionToken = 'test-session-token'; + const instance = createUntypedPixel({ + appId: 'test', + appParams: { ...defaultAppParams, sessionToken }, + }); + + expect(instance.appId).toEqual('test'); + expect(instance.appParams.sessionToken).toEqual(sessionToken); + }); + it('should handle opening the embedded experience when logged out', () => { const instance = createUntypedPixel(defaultArgs);