Skip to content

Commit dc846b0

Browse files
authored
Merge pull request #7 from Peersyst/dapp/feat/relayer-client
[TA-4556]: Implement near relayer dapp client calls
2 parents 592a1f3 + e45db07 commit dc846b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+863
-132
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# one-click-connect
1+
# one-click-connect
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./relayer.client";
2+
export * from "./relayer.client.config";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { NearDAppClientConfig } from "../base";
2+
import { RelayerAPI } from "@one-click-connect/core/relayer";
3+
4+
export type NearRelayerDAppClientConfig = {
5+
relayerAPI?: RelayerAPI;
6+
} & NearDAppClientConfig;
Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,95 @@
1-
import { INearRelayerDAppClient } from "@one-click-connect/core/relayer";
1+
import { INearRelayerDAppClient, RelayerAPI } from "@one-click-connect/core/relayer";
22
import { SignInitialTxRequest, SignWithFakRequest } from "@one-click-connect/core/common";
3+
import { NearRelayerDAppClientConfig } from "./relayer.client.config";
4+
import { IAccountService } from "../../common/client/interfaces/i-account.service";
5+
import { DAppClient } from "../../common/client";
6+
import { Account } from "../../common/account";
7+
import { ClientError, ClientErrorCodes } from "../../common/client/errors";
8+
import { MsgFakSign, MsgSignInitialTx } from "@one-click-connect/core/base";
39

4-
export class NearRelayerDAppClient implements INearRelayerDAppClient {
5-
/**
6-
* @inheritdoc
7-
*/
8-
signIn(_accountID: string, _signingURL: string): boolean {
9-
throw new Error("Method not implemented.");
10+
export class NearRelayerDAppClient<C extends NearRelayerDAppClientConfig> extends DAppClient<C> implements INearRelayerDAppClient {
11+
constructor(config: C, accountService: IAccountService) {
12+
super(config, accountService);
1013
}
1114

1215
/**
1316
* @inheritdoc
1417
*/
15-
signOut(): void {
16-
throw new Error("Method not implemented.");
18+
signIn(accountID: string, signingURL: string, relayerAPI: RelayerAPI): boolean {
19+
if (this.getActiveAccount()?.accountID === accountID) {
20+
return true;
21+
}
22+
23+
const account = this.accountService.getAccount(accountID);
24+
if (!account) {
25+
return false;
26+
}
27+
28+
if (account.signingURL !== signingURL || (account.relayerAPI && account.relayerAPI !== relayerAPI)) {
29+
this.accountService.updateAccount(accountID, signingURL, relayerAPI);
30+
}
31+
32+
this.accountService.setActive(accountID);
33+
return true;
1734
}
1835

1936
/**
2037
* @inheritdoc
2138
*/
22-
signInitialTx(_request: SignInitialTxRequest): string {
23-
throw new Error("Method not implemented.");
39+
getActiveAccount(): Account | undefined {
40+
return this.accountService.getActive();
2441
}
2542

2643
/**
2744
* @inheritdoc
2845
*/
29-
signWithFullAccessKey(_request: SignWithFakRequest): string {
30-
throw new Error("Method not implemented.");
46+
signOut(): void {
47+
this.accountService.clearActiveAccount();
3148
}
3249

3350
/**
3451
* @inheritdoc
3552
*/
36-
accountId(): string {
37-
throw new Error("Method not implemented.");
53+
signInitialTx(request: SignInitialTxRequest): string {
54+
if (!this.config.redirectURL) {
55+
throw new ClientError(ClientErrorCodes.REDIRECT_URL_NOT_SET);
56+
}
57+
58+
if (!this.config.relayerAPI) {
59+
throw new ClientError(ClientErrorCodes.RELAYER_API_NOT_SET);
60+
}
61+
62+
const { accountID, signingURL, permissions } = request;
63+
64+
const account = this.accountService.getAccount(accountID);
65+
if (account) {
66+
throw new ClientError(ClientErrorCodes.ACCOUNT_ALREADY_EXISTS);
67+
}
68+
69+
const { keypair } = this.accountService.createAccount(accountID, signingURL, this.config.relayerAPI);
70+
71+
const msg = new MsgSignInitialTx(this.config.redirectURL, permissions, keypair.getPublicKey());
72+
return msg.toURL(signingURL);
3873
}
3974

4075
/**
4176
* @inheritdoc
4277
*/
43-
relayerAPI(): string {
44-
throw new Error("Method not implemented.");
78+
signWithFullAccessKey(request: SignWithFakRequest): string {
79+
if (!this.config.redirectURL) {
80+
throw new ClientError(ClientErrorCodes.REDIRECT_URL_NOT_SET);
81+
}
82+
83+
const { transaction, signingURL } = request;
84+
85+
const msg = new MsgFakSign(transaction, this.config.redirectURL);
86+
return msg.toURL(signingURL);
4587
}
4688

4789
/**
4890
* @inheritdoc
4991
*/
50-
signingURL(): string {
51-
throw new Error("Method not implemented.");
92+
getConfig(): C {
93+
return this.config;
5294
}
5395
}

packages/dapp/core/src/common/account/account.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { KeyPair } from "near-api-js/lib/utils";
22
import { IAccountRepository } from "./interfaces";
33
import { IAccountService } from "../client/interfaces/i-account.service";
44
import { Account } from "./account.types";
5+
import { RelayerAPI } from "@one-click-connect/core/relayer";
56

67
export class AccountService implements IAccountService {
78
constructor(private readonly accountRepository: IAccountRepository) {}
@@ -37,9 +38,9 @@ export class AccountService implements IAccountService {
3738
/**
3839
* @inheritdoc
3940
*/
40-
createAccount(accountId: string, signingURL: string, curve: string = "ed25519"): Account {
41-
const keypair = KeyPair.fromRandom(curve);
42-
const account = this.accountRepository.create(accountId, keypair, signingURL);
41+
createAccount(accountId: string, signingURL: string, relayerAPI?: RelayerAPI): Account {
42+
const keypair = KeyPair.fromRandom("ed25519");
43+
const account = this.accountRepository.create(accountId, keypair, signingURL, relayerAPI);
4344
this.accountRepository.setActive(accountId);
4445
return account;
4546
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { RelayerAPI } from "@one-click-connect/core/relayer";
12
import { KeyPair } from "near-api-js/lib/utils";
23

34
export type Account = {
45
accountID: string;
56
keypair: KeyPair;
67
signingURL: string;
8+
relayerAPI?: RelayerAPI;
79
};

packages/dapp/core/src/common/account/interfaces/i-account.repository.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { KeyPair } from "near-api-js/lib/utils";
22
import { Account } from "../account.types";
3+
import { RelayerAPI } from "@one-click-connect/core/relayer";
34

45
export interface IAccountRepository {
56
/**
@@ -18,13 +19,13 @@ export interface IAccountRepository {
1819
* @param accountId The account ID to create.
1920
* @param keypair The keypair to associate with the account.
2021
*/
21-
create(accountId: string, keypair: KeyPair, signingURL: string): Account;
22+
create(accountId: string, keypair: KeyPair, signingURL: string, relayerAPI?: RelayerAPI): Account;
2223
/**
2324
* Updates an existing account entry.
2425
* @param accountId The account ID to update.
2526
* @param keypair The new keypair to associate with the account.
2627
*/
27-
update(accountId: string, keypair?: KeyPair, signingURL?: string): void;
28+
update(accountId: string, keypair?: KeyPair, signingURL?: string, relayerAPI?: RelayerAPI): void;
2829
/**
2930
* Deletes an account entry.
3031
* @param accountId The account ID to delete.

packages/dapp/core/src/common/account/states/account-store.state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RelayerAPI } from "@one-click-connect/core/relayer";
12
import { KeyPair } from "near-api-js/lib/utils";
23

34
export type AccountStoreState = {
@@ -8,4 +9,5 @@ export type AccountStoreState = {
89
export type AccountStoreAccount = {
910
keypair: KeyPair;
1011
signingURL: string;
12+
relayerAPI?: RelayerAPI;
1113
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum ClientErrorCodes {
22
ACCOUNT_ALREADY_EXISTS = "ACCOUNT_ALREADY_EXISTS",
33
REDIRECT_URL_NOT_SET = "REDIRECT_URL_NOT_SET",
4+
RELAYER_API_NOT_SET = "RELAYER_API_NOT_SET",
45
}

packages/dapp/core/src/common/client/interfaces/i-account.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Account } from "../../account/account.types";
2+
import { RelayerAPI } from "@one-click-connect/core/relayer";
23

34
export interface IAccountService {
45
/**
@@ -24,9 +25,11 @@ export interface IAccountService {
2425
/**
2526
* Creates a new account.
2627
* @param accountId The ID of the account to create.
28+
* @param signingURL The signing URL of the account.
29+
* @param relayerAPI The relayer API of the account.
2730
* @returns The created account.
2831
*/
29-
createAccount(accountId: string, signingURL: string): Account;
32+
createAccount(accountId: string, signingURL: string, relayerAPI?: RelayerAPI): Account;
3033
/**
3134
* Deletes an account.
3235
* @param accountId The ID of the account to delete.
@@ -36,6 +39,7 @@ export interface IAccountService {
3639
* Updates an account.
3740
* @param accountId The ID of the account to update.
3841
* @param signingURL The signing URL of the account.
42+
* @param relayerAPI The relayer API of the account.
3943
*/
40-
updateAccount(accountId: string, signingURL: string): void;
44+
updateAccount(accountId: string, signingURL: string, relayerAPI?: RelayerAPI): void;
4145
}

0 commit comments

Comments
 (0)