Skip to content

Commit

Permalink
Merge pull request #4331 from WalletConnect/feat/auth
Browse files Browse the repository at this point in the history
feat: auth aka sign 2.5
  • Loading branch information
ganchoradkov committed Apr 2, 2024
2 parents f902b14 + ed537a7 commit 73f76ff
Show file tree
Hide file tree
Showing 39 changed files with 3,426 additions and 108 deletions.
21 changes: 4 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions packages/core/src/controllers/pairing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class Pairing implements IPairing {
this.registeredMethods = [...new Set([...this.registeredMethods, ...methods])];
};

public create: IPairing["create"] = async () => {
public create: IPairing["create"] = async (params) => {
this.isInitialized();
const symKey = generateRandomBytes32();
const topic = await this.core.crypto.setSymKey(symKey);
Expand All @@ -100,6 +100,7 @@ export class Pairing implements IPairing {
symKey,
relay,
expiryTimestamp: expiry,
methods: params?.methods,
});
await this.pairings.set(topic, pairing);
await this.core.relayer.subscribe(topic);
Expand All @@ -111,7 +112,7 @@ export class Pairing implements IPairing {
public pair: IPairing["pair"] = async (params) => {
this.isInitialized();
this.isValidPair(params);
const { topic, symKey, relay, expiryTimestamp } = parseUri(params.uri);
const { topic, symKey, relay, expiryTimestamp, methods } = parseUri(params.uri);
let existingPairing;
if (this.pairings.keys.includes(topic)) {
existingPairing = this.pairings.get(topic);
Expand All @@ -123,7 +124,7 @@ export class Pairing implements IPairing {
}

const expiry = expiryTimestamp || calcExpiry(FIVE_MINUTES);
const pairing = { topic, relay, expiry, active: false };
const pairing = { topic, relay, expiry, active: false, methods };
await this.pairings.set(topic, pairing);
this.core.expirer.set(topic, expiry);

Expand Down
3 changes: 1 addition & 2 deletions packages/sign-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@aws-sdk/client-cloudwatch": "3.450.0",
"@walletconnect/jsonrpc-provider": "^1.0.13",
"@walletconnect/jsonrpc-ws-connection": "1.0.14",
"@walletconnect/relay-api": "^1.0.9",
"lokijs": "^1.5.12"
"@walletconnect/relay-api": "^1.0.9"
}
}
41 changes: 40 additions & 1 deletion packages/sign-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SignClientTypes, ISignClient, ISignClientEvents, EngineTypes } from "@w
import { getAppMetadata } from "@walletconnect/utils";
import { EventEmitter } from "events";
import { SIGN_CLIENT_DEFAULT, SIGN_CLIENT_PROTOCOL, SIGN_CLIENT_VERSION } from "./constants";
import { Engine, PendingRequest, Proposal, Session } from "./controllers";
import { AuthStore, Engine, PendingRequest, Proposal, Session } from "./controllers";

export class SignClient extends ISignClient {
public readonly protocol = SIGN_CLIENT_PROTOCOL;
Expand All @@ -24,6 +24,7 @@ export class SignClient extends ISignClient {
public session: ISignClient["session"];
public proposal: ISignClient["proposal"];
public pendingRequest: ISignClient["pendingRequest"];
public auth: ISignClient["auth"];

static async init(opts?: SignClientTypes.Options) {
const client = new SignClient(opts);
Expand All @@ -49,6 +50,7 @@ export class SignClient extends ISignClient {
this.proposal = new Proposal(this.core, this.logger);
this.pendingRequest = new PendingRequest(this.core, this.logger);
this.engine = new Engine(this);
this.auth = new AuthStore(this.core, this.logger);
}

get context() {
Expand Down Expand Up @@ -200,6 +202,42 @@ export class SignClient extends ISignClient {
}
};

public authenticate: ISignClient["authenticate"] = async (params) => {
try {
return await this.engine.authenticate(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};

public formatAuthMessage: ISignClient["formatAuthMessage"] = (params) => {
try {
return this.engine.formatAuthMessage(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};

public approveSessionAuthenticate: ISignClient["approveSessionAuthenticate"] = async (params) => {
try {
return await this.engine.approveSessionAuthenticate(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};

public rejectSessionAuthenticate: ISignClient["rejectSessionAuthenticate"] = async (params) => {
try {
return await this.engine.rejectSessionAuthenticate(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};

// ---------- Private ----------------------------------------------- //

private async initialize() {
Expand All @@ -210,6 +248,7 @@ export class SignClient extends ISignClient {
await this.proposal.init();
await this.pendingRequest.init();
await this.engine.init();
await this.auth.init();
this.core.verify.init({ verifyUrl: this.metadata.verifyUrl });
this.logger.info(`SignClient Initialization Success`);
} catch (error: any) {
Expand Down
9 changes: 9 additions & 0 deletions packages/sign-client/src/constants/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const AUTH_PROTOCOL = "wc";
export const AUTH_VERSION = 1.5;
export const AUTH_CONTEXT = "auth";
export const AUTH_KEYS_CONTEXT = "authKeys";
export const AUTH_PAIRING_TOPIC_CONTEXT = "pairingTopics";
export const AUTH_REQUEST_CONTEXT = "requests";

export const AUTH_STORAGE_PREFIX = `${AUTH_PROTOCOL}@${AUTH_VERSION}:${AUTH_CONTEXT}:`;
export const AUTH_PUBLIC_KEY_NAME = `${AUTH_STORAGE_PREFIX}:PUB_KEY`;
1 change: 1 addition & 0 deletions packages/sign-client/src/constants/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SIGN_CLIENT_EVENTS: Record<SignClientTypes.Event, SignClientTypes.E
session_request_sent: "session_request_sent",
session_event: "session_event",
proposal_expire: "proposal_expire",
session_authenticate: "session_authenticate",
session_request_expire: "session_request_expire",
};

Expand Down
14 changes: 13 additions & 1 deletion packages/sign-client/src/constants/engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FIVE_MINUTES, ONE_DAY, SEVEN_DAYS } from "@walletconnect/time";
import { FIVE_MINUTES, ONE_DAY, ONE_HOUR, SEVEN_DAYS } from "@walletconnect/time";
import { EngineTypes } from "@walletconnect/types";

export const ENGINE_CONTEXT = "engine";
Expand Down Expand Up @@ -101,6 +101,18 @@ export const ENGINE_RPC_OPTS: EngineTypes.RpcOptsMap = {
tag: 1115,
},
},
wc_sessionAuthenticate: {
req: {
ttl: ONE_HOUR,
prompt: true,
tag: 1116,
},
res: {
ttl: ONE_HOUR,
prompt: false,
tag: 1117,
},
},
};

export const SESSION_REQUEST_EXPIRY_BOUNDARIES = {
Expand Down
1 change: 1 addition & 0 deletions packages/sign-client/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./session";
export * from "./engine";
export * from "./pendingRequest";
export * from "./verify";
export * from "./auth";
11 changes: 11 additions & 0 deletions packages/sign-client/src/controllers/authKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Store } from "@walletconnect/core";
import { Logger } from "@walletconnect/logger";
import { ICore } from "@walletconnect/types";

import { AUTH_KEYS_CONTEXT, AUTH_STORAGE_PREFIX, AUTH_PUBLIC_KEY_NAME } from "../constants";

export class AuthKey extends Store<string, { responseTopic: string; publicKey: string }> {
constructor(public core: ICore, public logger: Logger) {
super(core, logger, AUTH_KEYS_CONTEXT, AUTH_STORAGE_PREFIX, () => AUTH_PUBLIC_KEY_NAME);
}
}
11 changes: 11 additions & 0 deletions packages/sign-client/src/controllers/authPairingTopic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Store } from "@walletconnect/core";
import { Logger } from "@walletconnect/logger";
import { ICore } from "@walletconnect/types";

import { AUTH_PAIRING_TOPIC_CONTEXT, AUTH_STORAGE_PREFIX } from "../constants";

export class AuthPairingTopic extends Store<string, { topic: string; pairingTopic: string }> {
constructor(public core: ICore, public logger: Logger) {
super(core, logger, AUTH_PAIRING_TOPIC_CONTEXT, AUTH_STORAGE_PREFIX);
}
}
17 changes: 17 additions & 0 deletions packages/sign-client/src/controllers/authRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Store } from "@walletconnect/core";
import { Logger } from "@walletconnect/logger";
import { AuthTypes, ICore } from "@walletconnect/types";

import { AUTH_STORAGE_PREFIX, AUTH_REQUEST_CONTEXT } from "../constants";

export class AuthRequest extends Store<number, AuthTypes.PendingRequest> {
constructor(public core: ICore, public logger: Logger) {
super(
core,
logger,
AUTH_REQUEST_CONTEXT,
AUTH_STORAGE_PREFIX,
(val: AuthTypes.PendingRequest) => val.id,
);
}
}
23 changes: 23 additions & 0 deletions packages/sign-client/src/controllers/authStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Logger } from "@walletconnect/logger";
import { IAuth, ICore } from "@walletconnect/types";
import { AuthPairingTopic } from "./authPairingTopic";
import { AuthRequest } from "./authRequest";
import { AuthKey } from "./authKey";

export class AuthStore {
public authKeys: IAuth["authKeys"];
public pairingTopics: IAuth["pairingTopics"];
public requests: IAuth["requests"];

constructor(public core: ICore, public logger: Logger) {
this.authKeys = new AuthKey(this.core, this.logger);
this.pairingTopics = new AuthPairingTopic(this.core, this.logger);
this.requests = new AuthRequest(this.core, this.logger);
}

public async init() {
await this.authKeys.init();
await this.pairingTopics.init();
await this.requests.init();
}
}
Loading

0 comments on commit 73f76ff

Please sign in to comment.