Skip to content

Commit

Permalink
create server module
Browse files Browse the repository at this point in the history
  • Loading branch information
acharb committed Jan 29, 2024
1 parent 49b3a1b commit 2b7cfbb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 62 deletions.
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ export {
export { Anchor } from "./walletSdk/Anchor";
export { Sep24 } from "./walletSdk/Anchor/Sep24";
export { IssuedAssetId, NativeAssetId, FiatAssetId } from "./walletSdk/Asset";
export {
Sep10,
WalletSigner,
DefaultSigner,
signChallengeTransaction,
} from "./walletSdk/Auth";
export { Sep10, WalletSigner, DefaultSigner } from "./walletSdk/Auth";
export {
AccountKeypair,
PublicKeypair,
Expand All @@ -47,6 +42,12 @@ export { Utils };
import * as Exceptions from "./walletSdk/Exceptions";
export { Exceptions };

/**
* Server
*/
import * as Server from "./walletSdk/Server";
export { Server };

import * as walletSdk from "./walletSdk";
import { Keypair } from "@stellar/stellar-sdk";
// TODO - figure out why Keypair used in parent codebase throws error
Expand Down
54 changes: 1 addition & 53 deletions src/walletSdk/Auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { AxiosInstance } from "axios";
import StellarSdk, {
Transaction,
TransactionBuilder,
Keypair,
StellarToml,
} from "@stellar/stellar-sdk";
import StellarSdk, { Transaction } from "@stellar/stellar-sdk";
import { decode } from "jws";

import { Config } from "walletSdk";
Expand All @@ -15,19 +10,14 @@ import {
InvalidTokenError,
MissingTokenError,
ExpiredTokenError,
ChallengeTxnIncorrectSequenceError,
ChallengeTxnInvalidSignatureError,
} from "../Exceptions";
import {
AuthenticateParams,
AuthToken,
ChallengeParams,
ChallengeResponse,
SignParams,
SignChallengeTxnParams,
SignChallengeTxnResponse,
} from "../Types";
import { parseToml } from "../Utils";

export { WalletSigner, DefaultSigner } from "./WalletSigner";

Expand Down Expand Up @@ -175,45 +165,3 @@ const validateToken = (token: string) => {
throw new ExpiredTokenError(parsedToken.expiresAt);
}
};

/**
* Helper method for signing a SEP-10 challenge transaction if valid.
* @param {SignChallengeTxnParams} params - The Authentication params.
* @param {AccountKeypair} params.accountKp - Keypair for the Stellar account signing the transaction.
* @param {string} [params.challengeTx] - The challenge transaction given by an anchor for authentication.
* @param {string} [params.networkPassphrase] - The network passphrase for the network authenticating on.
* @param {string} [params.anchorDomain] - Domain hosting stellar.toml file containing `SIGNING_KEY`.
* @returns {Promise<SignChallengeTxnResponse>} The signed transaction.
*/
export const signChallengeTransaction = async ({
accountKp,
challengeTx,
networkPassphrase,
anchorDomain,
}: SignChallengeTxnParams): Promise<SignChallengeTxnResponse> => {
const tx = TransactionBuilder.fromXDR(
challengeTx,
networkPassphrase,
) as Transaction;

if (parseInt(tx.sequence) !== 0) {
throw new ChallengeTxnIncorrectSequenceError();
}

const tomlResp = await StellarToml.Resolver.resolve(anchorDomain);
const parsedToml = parseToml(tomlResp);
const anchorKp = Keypair.fromPublicKey(parsedToml.signingKey);

const isValid =
tx.signatures.length &&
anchorKp.verify(tx.hash(), tx.signatures[0].signature());
if (!isValid) {
throw new ChallengeTxnInvalidSignatureError();
}

accountKp.sign(tx);
return {
transaction: tx.toXDR(),
networkPassphrase,
};
};
60 changes: 60 additions & 0 deletions src/walletSdk/Server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Code in the Server module is written to be used by server side
* applications.
*/

import {
Transaction,
TransactionBuilder,
Keypair,
StellarToml,
} from "@stellar/stellar-sdk";

import { parseToml } from "../Utils";
import { SignChallengeTxnParams, SignChallengeTxnResponse } from "../Types";
import {
ChallengeTxnIncorrectSequenceError,
ChallengeTxnInvalidSignatureError,
} from "../Exceptions";

/**
* Helper method for signing a SEP-10 challenge transaction if valid.
* @param {SignChallengeTxnParams} params - The Authentication params.
* @param {AccountKeypair} params.accountKp - Keypair for the Stellar account signing the transaction.
* @param {string} [params.challengeTx] - The challenge transaction given by an anchor for authentication.
* @param {string} [params.networkPassphrase] - The network passphrase for the network authenticating on.
* @param {string} [params.anchorDomain] - Domain hosting stellar.toml file containing `SIGNING_KEY`.
* @returns {Promise<SignChallengeTxnResponse>} The signed transaction.
*/
export const signChallengeTransaction = async ({
accountKp,
challengeTx,
networkPassphrase,
anchorDomain,
}: SignChallengeTxnParams): Promise<SignChallengeTxnResponse> => {
const tx = TransactionBuilder.fromXDR(
challengeTx,
networkPassphrase,
) as Transaction;

if (parseInt(tx.sequence) !== 0) {
throw new ChallengeTxnIncorrectSequenceError();
}

const tomlResp = await StellarToml.Resolver.resolve(anchorDomain);
const parsedToml = parseToml(tomlResp);
const anchorKp = Keypair.fromPublicKey(parsedToml.signingKey);

const isValid =
tx.signatures.length &&
anchorKp.verify(tx.hash(), tx.signatures[0].signature());
if (!isValid) {
throw new ChallengeTxnInvalidSignatureError();
}

accountKp.sign(tx);
return {
transaction: tx.toXDR(),
networkPassphrase,
};
};
6 changes: 3 additions & 3 deletions test/auth.test.ts → test/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransactionBuilder } from "@stellar/stellar-sdk";
import { Wallet, signChallengeTransaction } from "../src";
import { Wallet, Server } from "../src";

let wallet;
let account;
Expand All @@ -18,7 +18,7 @@ describe("SEP-10 helpers", () => {

let isValid;
try {
const signedResp = await signChallengeTransaction({
const signedResp = await Server.signChallengeTransaction({
accountKp,
challengeTx: validChallengeTx,
networkPassphrase,
Expand All @@ -44,7 +44,7 @@ describe("SEP-10 helpers", () => {

let isValid;
try {
await signChallengeTransaction({
await Server.signChallengeTransaction({
accountKp,
challengeTx: invalidChallengeTx,
networkPassphrase,
Expand Down

0 comments on commit 2b7cfbb

Please sign in to comment.