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 SEP6 deposit and withdrawal #77

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
96 changes: 89 additions & 7 deletions src/walletSdk/Anchor/Sep6.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { AxiosInstance } from "axios";
import queryString from "query-string";

import { Anchor } from "../Anchor";
import { ServerRequestFailedError } from "../Exceptions";
import { Sep6Info } from "../Types";

type Sep6Params = {
anchor: Anchor;
httpClient: AxiosInstance;
};
import {
ServerRequestFailedError,
Sep12MissingInfoError,
Sep6DepositDeniedError,
} from "../Exceptions";
import {
Sep6Info,
Sep6Params,
Sep6DepositParams,
Sep6WithdrawParams,
Sep6DepositResponse,
Sep6WithdrawResponse,
} from "../Types";

/**
* Flow for creating deposits and withdrawals with an anchor using SEP-6.
Expand Down Expand Up @@ -53,4 +60,79 @@ export class Sep6 {
throw new ServerRequestFailedError(e);
}
}

/**
* Deposits funds using the SEP-6 protocol. Next steps by
* the anchor are given in the response.
*
* @param {object} options - The options for the deposit.
* @param {string} options.authToken - The authentication token.
* @param {Sep6DepositParams} options.params - The parameters for the deposit request.
*
* @returns {Promise<Sep6DepositResponse>} Sep6 deposit response, containing next steps if needed
* to complete the deposit.
*
* @throws {Sep12MissingInfoError} If Sep-12 KYC info is missing for the user.
* @throws {Sep6DepositDeniedError} If the deposit is still being processed or not accepted.
* @throws {Error} If an unexpected error occurs during the deposit operation.
*/
async deposit({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about exchange endoints, are they out of scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

authToken,
params,
}: {
authToken: string;
params: Sep6DepositParams;
}): Promise<Sep6DepositResponse> {
const { transferServer } = await this.anchor.sep1();

try {
const resp = await this.httpClient.get(
`${transferServer}/deposit?${queryString.stringify(params)}`,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
},
);
return resp.data;
} catch (e) {
if (e.response?.data?.type === "non_interactive_customer_info_needed") {
throw new Sep12MissingInfoError(e.response?.data?.fields);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's an error. It's expected behavior in the flow. I think we should have different types of the Sep6Response which wallets must handle accordingly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, how about this? fb25d34

also I realized those resposnes can be for deposit or withdrawal so i added a helper flow function similar to sep24

} else if (e.response?.data?.type === "customer_info_status") {
throw new Sep6DepositDeniedError(e.response?.data);
}
throw e;
}
}

/**
* Initiates a withdrawal using SEP-6.
*
* @param {object} options - The options for the withdrawal operation.
* @param {string} options.authToken - The authentication token.
* @param {Sep6WithdrawParams} options.params - The parameters for the withdrawal request.
*
* @returns {Promise<Sep6WithdrawResponse>} Sep6 withdraw response.
*/
async withdraw({
authToken,
params,
}: {
authToken: string;
params: Sep6WithdrawParams;
}): Promise<Sep6WithdrawResponse> {
const { transferServer } = await this.anchor.sep1();

const resp = await this.httpClient.get(
`${transferServer}/withdraw?${queryString.stringify(params)}`,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
},
);
return resp.data;
}
}
18 changes: 18 additions & 0 deletions src/walletSdk/Exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,21 @@ export class NoAccountAndNoSponsorError extends Error {
Object.setPrototypeOf(this, NoAccountAndNoSponsorError.prototype);
}
}

export class Sep12MissingInfoError extends Error {
constructor(missingFields) {
super(
`SEP-12 KYC info missing. Submit to anchor missing fields: ${missingFields}`,
);
Object.setPrototypeOf(this, Sep12MissingInfoError.prototype);
}
}

export class Sep6DepositDeniedError extends Error {
constructor(data) {
super(
`Sep-6 information is either still being processed or not accepted. ${data}`,
);
Object.setPrototypeOf(this, Sep6DepositDeniedError.prototype);
}
}
69 changes: 69 additions & 0 deletions src/walletSdk/Types/sep6.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { AxiosInstance } from "axios";
import { Anchor } from "../Anchor";

export interface Sep6EndpointInfo {
enabled: boolean;
authentication_required?: boolean;
Expand Down Expand Up @@ -56,3 +59,69 @@ export interface Sep6Info {
claimable_balances: boolean;
};
}

export type Sep6Params = {
anchor: Anchor;
httpClient: AxiosInstance;
};

export interface Sep6DepositParams {
asset_code: string;
account: string;
memo_type?: string;
memo?: string;
email_address?: string;
type?: string;
lang?: string;
on_change_callback?: string;
amount?: string;
country_code?: string;
claimable_balance_supported?: string;
customer_id?: string;
}

export interface Sep6WithdrawParams {
asset_code: string;
type: string;
dest?: string;
dest_extra?: string;
account?: string;
memo?: string;
lang?: string;
on_change_callback?: string;
amount?: string;
country_code?: string;
refund_memo?: string;
refund_memo_type?: string;
customer_id?: string;
}

export interface Sep6DepositResponse {
how?: string;
instructions?: {
[key: string]: {
value: string;
description: string;
};
};
id?: string;
eta?: number;
min_amoun?: number;
max_amount?: number;
fee_fixed?: number;
fee_percent?: number;
extra_info?: { message?: string };
}

export interface Sep6WithdrawResponse {
account_id?: string;
memo_type?: string;
memo?: string;
id?: string;
eta?: number;
min_amount?: number;
max_amount?: number;
fee_fixed?: number;
fee_percent?: number;
extra_info?: { message?: string };
}
73 changes: 70 additions & 3 deletions test/sep6.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { Wallet } from "../src";
import axios from "axios";

let wallet;
let anchor;
let sep6;
let accountKp;

describe("SEP-6", () => {
beforeAll(async () => {
wallet = Wallet.TestNet();
anchor = wallet.anchor({ homeDomain: "testanchor.stellar.org" });
sep6 = anchor.sep6();

accountKp = wallet.stellar().account().createKeypair();
await axios.get(
"https://friendbot.stellar.org/?addr=" + accountKp.publicKey,
);
}, 10000);
it("should get anchor info", async () => {
const wallet = Wallet.TestNet();
const anchor = wallet.anchor({ homeDomain: "testanchor.stellar.org" });
const sep6 = anchor.sep6();
const resp = await sep6.info();
expect(resp.deposit).toBeTruthy();
expect(resp.withdraw).toBeTruthy();
Expand All @@ -13,4 +26,58 @@ describe("SEP-6", () => {
expect(refreshed.deposit).toBeTruthy();
expect(refreshed.withdraw).toBeTruthy();
});
it("should deposit", async () => {
const auth = await anchor.sep10();
const authToken = await auth.authenticate({ accountKp });

const sep12 = await anchor.sep12(authToken);

await sep12.add({
sep9Info: {
first_name: "john",
last_name: "smith",
email_address: "[email protected]",
bank_number: "12345",
bank_account_number: "12345",
},
});

const resp = await sep6.deposit({
authToken,
params: {
asset_code: "SRT",
account: accountKp.publicKey,
type: "bank_account",
},
});
expect(resp.id).toBeTruthy();
});
it("should withdraw", async () => {
const auth = await anchor.sep10();
const authToken = await auth.authenticate({ accountKp });

const sep12 = await anchor.sep12(authToken);

await sep12.add({
sep9Info: {
first_name: "john",
last_name: "smith",
email_address: "[email protected]",
bank_number: "12345",
bank_account_number: "12345",
},
});

const resp = await sep6.withdraw({
authToken,
params: {
asset_code: "SRT",
account: accountKp.publicKey,
type: "bank_account",
dest: "123",
dest_extra: "12345",
},
});
expect(resp.id).toBeTruthy();
});
});
Loading