Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sessionToken parameter support to initOnRamp #306

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/onramp/generateOnRampURL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,33 @@ describe('generateOnrampURL', () => {
const url = new URL(
generateOnRampURL({
sessionToken: 'test',
destinationWallets: [],
}),
);
expect(url.origin).toEqual('https://pay.coinbase.com');
expect(url.pathname).toEqual('/buy/select-asset');
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({
Expand Down
16 changes: 12 additions & 4 deletions src/onramp/generateOnRampURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
7 changes: 7 additions & 0 deletions src/types/onramp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ type BaseOnRampAppParams = {
* `{ "0x1": ["base"] }`
*/
addresses?: Record<string, string[]>;
/**
* 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.
Expand Down
11 changes: 11 additions & 0 deletions src/utils/CoinbasePixel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down