Skip to content

Commit d644987

Browse files
committed
fix codec and stuff
1 parent 8b88c7a commit d644987

Some content is hidden

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

41 files changed

+369
-121
lines changed

examples/dapp/near/vanilla/src/providers/NearDAppProvider.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const NearDAppProvider: React.FC<NearDAppProviderProps> = ({ children })
4545
{
4646
receiverId: "guest-book.testnet",
4747
methodNames: ["addMessage"],
48+
allowance: "0"
4849
},
4950
);
5051

examples/wallet/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@one-click-connect/core": "workspace:*",
1313
"@one-click-connect/wallet": "workspace:*",
14+
"@one-click-connect/sdk": "workspace:*",
1415
"bignumber.js": "^9.2.1",
1516
"buffer": "^6.0.3",
1617
"near-api-js": "^4.0.4",

examples/wallet/web/src/hooks/useNearOCCRequest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function useNearOCCRequest(): UseNearOCCRequestResult {
3232
return useMemo(() => {
3333
try {
3434
const { type, params } = client.parseDAppRequest(window.location.href);
35+
console.log("type", type, "params", params);
3536
switch (type) {
3637
case "add-lak":
3738
return {
@@ -56,7 +57,8 @@ export function useNearOCCRequest(): UseNearOCCRequestResult {
5657
throw Error("unknown request type");
5758
}
5859
// eslint-disable-next-line @typescript-eslint/no-unused-vars
59-
} catch (_: unknown) {
60+
} catch (e: unknown) {
61+
console.log(e);
6062
return {
6163
isInitialTxRequest: false,
6264
isFullAccessKeyRequest: false,

examples/wallet/web/src/hooks/useWallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function useWallet() {
3535
// Use stored key if available, otherwise use default
3636
const privateKey = storedKey || defaultPrivateKey;
3737
setPrivateKey(privateKey);
38-
});
38+
}, []);
3939

4040

4141
return {

examples/wallet/web/src/pages/Sign.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ const Sign: React.FC = () => {
1414
const [transaction, setTransaction] = useState<Transaction | null>(null);
1515
const { isInitialTxRequest, isFullAccessKeyRequest, data } = useNearOCCRequest();
1616
const { wallet, loading } = useWallet();
17-
18-
console.log(data);
19-
17+
console.log(isFullAccessKeyRequest);
2018
useEffect(() => {
2119
if (isInitialTxRequest) {
2220
setPermissions(data?.permissions);
@@ -36,9 +34,14 @@ const Sign: React.FC = () => {
3634
const connection = await near.connect({ networkId: "testnet", nodeUrl: "https://rpc.testnet.near.org", keyStore });
3735
const account = new Account(connection.connection, wallet.accountId);
3836

39-
console.log(PublicKey.fromString(publicKey!), permissions!.receiverId, permissions!.methodNames);
40-
const res = await account.addKey(PublicKey.fromString(publicKey!), permissions!.receiverId, permissions!.methodNames);
41-
console.log(res);
37+
await account.addKey(PublicKey.fromString(publicKey!), permissions!.receiverId, permissions!.methodNames);
38+
window.location.assign(redirectURL!);
39+
} else {
40+
const keyStore = new near.keyStores.InMemoryKeyStore();
41+
await keyStore.setKey("testnet", wallet.accountId, wallet.keyPair!);
42+
const connection = await near.connect({ networkId: "testnet", nodeUrl: "https://rpc.testnet.near.org", keyStore });
43+
const account = new Account(connection.connection, wallet.accountId);
44+
await account.signAndSendTransaction(transaction!);
4245
window.location.assign(redirectURL!);
4346
}
4447
}

examples/wallet/web/src/providers/NearWalletProvider.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, { createContext, useContext, useMemo } from "react";
22
import { WalletClient } from "@one-click-connect/wallet";
3+
import { Transaction, TransactionCodec } from "@one-click-connect/sdk";
34

45
interface NearDAppContextType {
5-
client: WalletClient;
6+
client: WalletClient<Transaction>;
67
}
78

89
const NearDAppContext = createContext<NearDAppContextType | null>(null);
@@ -23,7 +24,7 @@ export const NearWalletProvider: React.FC<NearWalletProviderProps> = ({ children
2324
const client = useMemo(() => {
2425
return new WalletClient({
2526
signingURL: window.location.origin + "/sign",
26-
});
27+
}, new TransactionCodec());
2728
}, []);
2829

2930
const value = useMemo(

packages/dapp/core/src/clients/base/base.client.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import { MsgAddLAK, MsgSignWithFAK, MsgSignIn, Permissions, MsgSignInQueryParams } from "@one-click-connect/core";
1+
import { MsgAddLAK, MsgSignWithFAK, MsgSignIn, Permissions, MsgSignInQueryParams, Codec } from "@one-click-connect/core";
22
import { BaseDAppClientConfig } from "./base.client.config";
33
import { Account } from "../../common";
4-
import { AccountStore, Callbacks, PendingTransactionStore, Wallet } from "../../common";
4+
import { AccountStore, Callbacks, PendingTransactionStore, Provider } from "../../common";
55

66
export abstract class BaseDAppClient<Transaction, TransactionResult, Config extends BaseDAppClientConfig = BaseDAppClientConfig> {
77
protected account?: Account;
88

99
protected constructor(
1010
protected config: Config,
11+
protected codec: Codec<Transaction, string>,
1112
protected permissions: Permissions,
1213
protected callbacks: Callbacks,
13-
protected wallet: Wallet<Transaction, TransactionResult>,
14+
protected provider: Provider<Transaction, TransactionResult>,
1415
protected accountStore: AccountStore,
1516
protected pendingTransactionStore?: PendingTransactionStore<Transaction>,
1617
) {}
@@ -32,7 +33,7 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
3233
get publicKey() {
3334
if (!this.connected) throw new Error("Not connected");
3435
if (!this.accessKey) throw new Error("Access key undefined");
35-
return this.wallet.derivePublicKey(this.accessKey);
36+
return this.provider.derivePublicKey(this.accessKey);
3637
}
3738

3839
/**
@@ -68,21 +69,27 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
6869
*/
6970
async signAndSendTransaction(transaction: Transaction): Promise<TransactionResult | void> {
7071
if (!this.connected) throw new Error("Not connected");
71-
if (!this.account!.accessKey) {
72-
this.account!.accessKey = await this.wallet.generateAccessKey();
73-
await this.accountStore.set(this.account!);
74-
if (this.pendingTransactionStore) await this.pendingTransactionStore.push(this.account!.accountId, transaction);
75-
const publicKey = await this.wallet.derivePublicKey(this.account!.accessKey);
72+
const hasAccessKey = this.account!.accessKey !== undefined;
73+
const canExecute = await this.provider.canExecute(this.permissions, transaction);
74+
const hasPermissions =
75+
hasAccessKey && (await this.provider.canAccessKeyExecute(this.account!.accountId, this.account!.accessKey!, transaction));
7676

77-
const msgAddLAK = new MsgAddLAK(this.config.redirectURL, this.permissions, publicKey);
78-
return await this.callbacks.onRequestAddLAK(msgAddLAK.toURL(this.account!.signingURL));
77+
if (!canExecute) {
78+
// Can't execute transaction with the established permissions
79+
const msgSignWithFAK = new MsgSignWithFAK(this.codec, [transaction], this.config.redirectURL);
80+
return await this.callbacks.onRequestSignWithFAK(msgSignWithFAK.toURL(this.account!.signingURL));
7981
} else {
80-
const hasPermissions = await this.wallet.canAccessKeyExecute(this.account!.accountId, this.account!.accessKey, transaction);
81-
if (hasPermissions) {
82-
return await this.wallet.signAndSendTransaction(this.account!.accountId, this.account!.accessKey, transaction);
82+
if (!hasAccessKey || (hasAccessKey && !hasPermissions)) {
83+
// Access key is not stored or access key is stored but doesn't have permissions (removed or badly created)
84+
this.account!.accessKey = await this.provider.generateAccessKey();
85+
await this.accountStore.set(this.account!);
86+
if (this.pendingTransactionStore) await this.pendingTransactionStore.push(this.account!.accountId, transaction);
87+
const publicKey = this.provider.derivePublicKey(this.account!.accessKey);
88+
const msgAddLAK = new MsgAddLAK(this.config.redirectURL, this.permissions, publicKey);
89+
return await this.callbacks.onRequestAddLAK(msgAddLAK.toURL(this.account!.signingURL));
8390
} else {
84-
const msgSignWithFAK = new MsgSignWithFAK([transaction], this.config.redirectURL);
85-
return await this.callbacks.onRequestSignWithFAK(msgSignWithFAK.toURL(this.account!.signingURL));
91+
// Access key is stored and has permissions
92+
return await this.provider.signAndSendTransaction(this.account!.accountId, this.account!.accessKey!, transaction);
8693
}
8794
}
8895
}
@@ -95,7 +102,7 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
95102
async signMessage(message: string): Promise<string> {
96103
if (!this.connected) throw new Error("Not connected");
97104
if (!this.accessKey) throw new Error("Access key undefined");
98-
return this.wallet.signMessage(this.accessKey, message);
105+
return this.provider.signMessage(this.accessKey, message);
99106
}
100107

101108
/**
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Permissions } from "@one-click-connect/core";
1+
import { Codec, Permissions } from "@one-click-connect/core";
22
import { BaseDAppClientConfig } from "../base";
3-
import { Codec, PendingTransactionStore, Wallet } from "../../common";
3+
import { PendingTransactionStore, Provider } from "../../common";
44
import { BaseDAppClient } from "../base";
5-
import { BrowserAccountStore, BrowserPendingTransactionStore } from "../../store/browser";
5+
import { BrowserAccountStore, BrowserPendingTransactionStore } from "../../store";
66
import { BrowserCallbacks } from "./browser.callbacks";
77

88
export class BrowserDAppClient<
@@ -12,13 +12,13 @@ export class BrowserDAppClient<
1212
> extends BaseDAppClient<Transaction, TransactionResult, Config> {
1313
constructor(
1414
protected config: Config,
15+
protected codec: Codec<Transaction, string>,
1516
protected permissions: Permissions,
16-
protected codec: Codec<Transaction[], string>,
17-
protected wallet: Wallet<Transaction, TransactionResult>,
17+
protected provider: Provider<Transaction, TransactionResult>,
1818
) {
1919
const accountStore = new BrowserAccountStore();
2020
const pendingTransactionStore: PendingTransactionStore<Transaction> = new BrowserPendingTransactionStore(codec);
2121
const callbacks = new BrowserCallbacks();
22-
super(config, permissions, callbacks, wallet, accountStore, pendingTransactionStore);
22+
super(config, codec, permissions, callbacks, provider, accountStore, pendingTransactionStore);
2323
}
2424
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export * from "./account";
22
export * from "./client";
3-
export * from "./codec";
43
export * from "./errors";
54
export * from "./config";
5+
export * from "./provider";
66
export * from "./store";
7-
export * from "./wallet";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./provider";

0 commit comments

Comments
 (0)