Skip to content

Commit

Permalink
[TS] Implement support for SEP-7 (#141)
Browse files Browse the repository at this point in the history
* Implement support for Sep-7

* Add a couple missing exceptions

* Assign a few missing types

* Add JSDocs for Sep7 classes

* Add JSDocs for sep7Parser file

* Make isValidSep7Uri also return a "reason" message for failed verifications

* Augment tests to account for error reasons strings

* Comment tweak
  • Loading branch information
CassioMG authored Jun 27, 2024
1 parent b768607 commit 8f9360f
Show file tree
Hide file tree
Showing 15 changed files with 1,814 additions and 11 deletions.
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: 38 additions & 1 deletion @stellar/typescript-wallet-sdk/src/walletSdk/Exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,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 @@ -319,8 +333,31 @@ export class AuthHeaderSigningKeypairRequiredError extends Error {
export class AuthHeaderClientDomainRequiredError extends Error {
constructor() {
super(
"This class should only be used for remote signing. For local signing use DefaultAuthHeaderSigner.",
"This class should only be used for remote signing. For local signing use DefaultAuthHeaderSigner",
);
Object.setPrototypeOf(this, AuthHeaderClientDomainRequiredError.prototype);
}
}

export class Sep7InvalidUriError extends Error {
constructor(reason: string) {
super(`Invalid Stellar Sep-7 URI, reason: ${reason}`);
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 operation 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
19 changes: 19 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,19 @@
export const WEB_STELLAR_SCHEME = "web+stellar:";

export enum Sep7OperationType {
tx = "tx",
pay = "pay",
}

export const URI_MSG_MAX_LENGTH = 300;

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

export type IsValidSep7UriResult = {
result: boolean;
reason?: string;
};
Loading

0 comments on commit 8f9360f

Please sign in to comment.