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

[TS] Implement support for SEP-7 #141

Merged
merged 9 commits into from
Jun 27, 2024
Merged
9 changes: 9 additions & 0 deletions @stellar/typescript-wallet-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export {
SponsoringBuilder,
} from "./walletSdk/Horizon";
export { Recovery } from "./walletSdk/Recovery";
export {
Sep7Base,
Sep7Pay,
Sep7Tx,
isValidSep7Uri,
parseSep7Uri,
sep7ReplacementsFromString,
sep7ReplacementsToString,
} from "./walletSdk/Uri";
export { Watcher } from "./walletSdk/Watcher";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
HttpHeaders,
} from "../Types";
import { DefaultClient } from "../";
import { DefaultSignerDomainAccountError } from "../Exceptions";

/**
* A Wallet Signer for signing Stellar transactions.
Expand Down Expand Up @@ -51,9 +52,8 @@ export const DefaultSigner: WalletSigner = {
},
// eslint-disable-next-line @typescript-eslint/require-await
signWithDomainAccount: async () => {
throw new Error(
"The DefaultSigner can't sign transactions with domain account",
);
// The DefaultSigner can't sign transactions with domain account
throw new DefaultSignerDomainAccountError();
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
* @class
*/
export class Sep12 {
private authToken;
private baseUrl;
private httpClient;
private headers;
private authToken: AuthToken;
private baseUrl: string;
private httpClient: AxiosInstance;
private headers: { [key: string]: string };

/**
* Creates a new instance of the Sep12 class.
Expand Down
39 changes: 39 additions & 0 deletions @stellar/typescript-wallet-sdk/src/walletSdk/Exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
FLOW_TYPE,
AxiosErrorData,
GetCustomerParams,
WEB_STELLAR_TX_SCHEME,
WEB_STELLAR_PAY_SCHEME,
} from "../Types";
import { extractAxiosErrorData } from "../Utils";

Expand Down Expand Up @@ -306,6 +308,20 @@ export class InvalidJsonError extends Error {
}
}

export class SigningKeypairMissingSecretError extends Error {
constructor() {
super("This keypair doesn't have a secret key and can't sign");
Object.setPrototypeOf(this, SigningKeypairMissingSecretError.prototype);
}
}

export class DefaultSignerDomainAccountError extends Error {
constructor() {
super("The DefaultSigner can't sign transactions with domain account");
Object.setPrototypeOf(this, DefaultSignerDomainAccountError.prototype);
}
}

export class AuthHeaderSigningKeypairRequiredError extends Error {
constructor() {
super("Must be SigningKeypair to sign auth header");
Expand All @@ -324,3 +340,26 @@ export class AuthHeaderClientDomainRequiredError extends Error {
Object.setPrototypeOf(this, AuthHeaderClientDomainRequiredError.prototype);
}
}

export class Sep7InvalidUriError extends Error {
constructor() {
super(
`Invalid Stellar Sep-7 URI: it must either start with '${WEB_STELLAR_TX_SCHEME}' and have a 'xdr' param or start with '${WEB_STELLAR_PAY_SCHEME}' and have a 'destination' param`,
);
Object.setPrototypeOf(this, Sep7InvalidUriError.prototype);
}
}

export class Sep7LongMsgError extends Error {
constructor(msgMaxLength: number) {
super(`'msg' should be no longer than ${msgMaxLength} characters.`);
Object.setPrototypeOf(this, Sep7LongMsgError.prototype);
}
}

export class Sep7UriTypeNotSupportedError extends Error {
constructor(type: string) {
super(`Stellar Sep-7 URI type ${type} is not currently supported.`);
Object.setPrototypeOf(this, Sep7UriTypeNotSupportedError.prototype);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Keypair, Transaction, FeeBumpTransaction } from "@stellar/stellar-sdk";
import { SigningKeypairMissingSecretError } from "../Exceptions";

export class AccountKeypair {
keypair: Keypair;
Expand Down Expand Up @@ -28,7 +29,7 @@ export class PublicKeypair extends AccountKeypair {
export class SigningKeypair extends AccountKeypair {
constructor(keypair: Keypair) {
if (!keypair.canSign()) {
throw new Error("This keypair doesn't have a secret key and can't sign");
throw new SigningKeypairMissingSecretError();
}
super(keypair);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export * from "./auth";
export * from "./horizon";
export * from "./recovery";
export * from "./sep6";
export * from "./sep7";
export * from "./sep12";
export * from "./sep24";
export * from "./sep38";
Expand Down
16 changes: 16 additions & 0 deletions @stellar/typescript-wallet-sdk/src/walletSdk/Types/sep7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum Sep7OperationType {
tx = "tx",
pay = "pay",
}

export const WEB_STELLAR_SCHEME = "web+stellar:";
export const WEB_STELLAR_TX_SCHEME = `${WEB_STELLAR_SCHEME}${Sep7OperationType.tx}`;
export const WEB_STELLAR_PAY_SCHEME = `${WEB_STELLAR_SCHEME}${Sep7OperationType.pay}`;

export const URI_MSG_MAX_LENGTH = 300;

export type Sep7Replacement = {
id: string;
path: string;
hint: string;
};
Loading
Loading