Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support FastAuth Wallets #200

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ How to verify the signature: https://docs.near.org/build/web3-apps/backend/#3-ve
- Add get/set methods for `window.location.hash`. `Set` method only accepts an empty string as a value.

- Allow the wallet selector to connect to an extra contract besides `config.contractName`. To enable this feature, pass the allowExternalContract flag to the config parameter of initNear, e.g. `initNear({networkId, selector, config: { allowExternalContract: 'hello.near-examples.near' }} )`

- Add support for FastAuth wallets by using `signAndSendDelegateAction()` when provided by wallet instead of `signAndSendTransaction()` when committing data to the Social DB contract.


## 2.6.1
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions src/lib/components/Commit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from "react";
import React, { useEffect, useState, useRef } from "react";
import {
asyncCommit,
prepareCommit,
Expand Down Expand Up @@ -37,7 +37,7 @@ export const CommitModal = (props) => {
const accountId = useAccountId(props.networkId);
const cache = useCache();

const [asyncCommitStarted, setAsyncAsyncCommitStarted] = useState(false);
const asyncCommitStarted = useRef(false);
const [extraStorage, setExtraStorage] = useState(0);
const [commitInProgress, setCommitInProgress] = useState(false);

Expand Down Expand Up @@ -143,7 +143,7 @@ export const CommitModal = (props) => {
if (
!commitInProgress &&
!cantCommit &&
!asyncCommitStarted &&
!asyncCommitStarted.current &&
commit &&
showIntent &&
writePermission &&
Expand All @@ -156,8 +156,8 @@ export const CommitModal = (props) => {
computeWritePermission(writePermission, commit.data[accountId])
) === JSON.stringify(writePermission)
) {
setAsyncAsyncCommitStarted(true);
onCommit().then(() => setAsyncAsyncCommitStarted(false));
asyncCommitStarted.current = true;
onCommit().then(() => (asyncCommitStarted.current = false));
}
}
}
Expand All @@ -176,7 +176,10 @@ export const CommitModal = (props) => {
const shouldBypassModal = !cantCommit && matchesModalBypassConfig;

const isReadyToCommit =
!!commit && showIntent && !asyncCommitStarted && writePermission !== null;
!!commit &&
showIntent &&
!asyncCommitStarted.current &&
writePermission !== null;
const show = isReadyToCommit && !shouldBypassModal;

useEffect(() => {
Expand Down
59 changes: 43 additions & 16 deletions src/lib/data/commitData.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,30 @@ export const prepareCommit = async (

export const asyncCommit = async (near, data, deposit) => {
console.log("Committing data", data);
return await conditionalCommit(near, data, deposit);

return await near.contract.set(
{
data,
},
TGas.mul(100).toFixed(0),
deposit.toFixed(0)
);
// return await near.contract.set(
// {
// data,
// },
// TGas.mul(100).toFixed(0),
// deposit.toFixed(0)
// );
};

export const asyncCommitData = async (near, originalData, forceRewrite) => {
const { data, deposit } = await prepareCommit(
near,
originalData,
forceRewrite
);
return asyncCommit(near, data, deposit);
};
// NOTE: The following method is commented out since it's not being used anywhere...
// export const asyncCommitData = async (near, originalData, forceRewrite) => {
// const { data, deposit } = await prepareCommit(
// near,
// originalData,
// forceRewrite
// );
// return asyncCommit(near, data, deposit);
// };

export const requestPermissionAndCommit = async (near, data, deposit) => {
const wallet = await (await near.selector).wallet();
const actions = [];

if (near.publicKey) {
actions.push(
functionCallCreator(
Expand All @@ -115,6 +117,14 @@ export const requestPermissionAndCommit = async (near, data, deposit) => {
);
deposit = Big(0);
}

return await conditionalCommit(near, data, deposit, actions);
};

const conditionalCommit = async (near, data, deposit, otherActions = []) => {
const wallet = await (await near.selector).wallet();
const actions = [...otherActions];

actions.push(
functionCallCreator(
"set",
Expand All @@ -123,6 +133,23 @@ export const requestPermissionAndCommit = async (near, data, deposit) => {
deposit.gt(0) ? deposit.toFixed(0) : "1"
)
);

if (wallet.signAndSendDelegateAction) {
/*
If the user is signed in with FastAuth, use the delegate action to pass through the relayer.
The relayer automatically deposits a certain amount of storage for each FastAuth account, which
is enough for most actions on Social DB. Passing a non-zero amount of deposit to the relayer
currently throws an error due to the SDK not using a full access key.
*/

actions.forEach((action) => (action.params.deposit = "0"));

return await wallet.signAndSendDelegateAction({
receiverId: near.config.contractName,
actions,
});
}

return await wallet.signAndSendTransaction({
receiverId: near.config.contractName,
actions,
Expand Down
Loading