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

feat!: make appKeyPrefix required #1014

Merged
merged 2 commits into from
Jan 5, 2023
Merged
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
19 changes: 19 additions & 0 deletions .changeset/gentle-points-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"near-api-js": major
---

Make `appKeyPrefix` a required arg to `WalletConnection` constructor

Users that were doing

```typescript
new WalletConnection(near);
```

will now have to do

```typescript
new WalletConnection(near, "undefined");
```

If they want to access the keys they had potentially accumulated
6 changes: 4 additions & 2 deletions packages/near-api-js/src/wallet-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ export class WalletConnection {
/** @hidden */
_completeSignInPromise: Promise<void>;

constructor(near: Near, appKeyPrefix: string | null) {
constructor(near: Near, appKeyPrefix: string) {
if(typeof(appKeyPrefix) != 'string') {
throw new Error('Please define a clear appKeyPrefix for this WalletConnection instance as the second argument to the constructor');
}
if(typeof window === 'undefined') {
return new Proxy(this, {
get(target, property) {
Expand All @@ -108,7 +111,6 @@ export class WalletConnection {
const authData = JSON.parse(window.localStorage.getItem(authDataKey));
this._networkId = near.config.networkId;
this._walletBaseUrl = near.config.walletUrl;
appKeyPrefix = appKeyPrefix || near.config.contractName || 'default';
this._keyStore = (near.connection.signer as InMemorySigner).keyStore;
this._authData = authData || { allKeys: [] };
this._authDataKey = authDataKey;
Expand Down
26 changes: 20 additions & 6 deletions packages/near-api-js/test/wallet-account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ beforeEach(() => {
replaceState: (state, title, url) => history.push([state, title, url])
}
});
walletConnection = new nearApi.WalletConnection(nearFake);
walletConnection = new nearApi.WalletConnection(nearFake, '');
});

it('not signed in by default', () => {
expect(walletConnection.isSignedIn()).not.toBeTruthy();
});

it('throws if non string appKeyPrefix', () => {
expect(() => new nearApi.WalletConnection(nearFake)).toThrow(/appKeyPrefix/);
expect(() => new nearApi.WalletConnection(nearFake, 1)).toThrow(/appKeyPrefix/);
expect(() => new nearApi.WalletConnection(nearFake, null)).toThrow(/appKeyPrefix/);
expect(() => new nearApi.WalletConnection(nearFake, undefined)).toThrow(/appKeyPrefix/);
});

describe('fails gracefully on the server side (without window)', () => {
const windowValueBefore = global.window;

Expand All @@ -70,26 +77,33 @@ describe('fails gracefully on the server side (without window)', () => {
});

it('does not throw on instantiation', () => {
expect(() => new nearApi.WalletConnection(nearFake)).not.toThrowError();
expect(() => new nearApi.WalletConnection(nearFake, '')).not.toThrowError();
});

it('throws if non string appKeyPrefix in server context', () => {
expect(() => new nearApi.WalletConnection(nearFake)).toThrow(/appKeyPrefix/);
expect(() => new nearApi.WalletConnection(nearFake, 1)).toThrow(/appKeyPrefix/);
expect(() => new nearApi.WalletConnection(nearFake, null)).toThrow(/appKeyPrefix/);
expect(() => new nearApi.WalletConnection(nearFake, undefined)).toThrow(/appKeyPrefix/);
});

it('returns an empty string as accountId', () => {
const serverWalletConnection = new nearApi.WalletConnection(nearFake);
const serverWalletConnection = new nearApi.WalletConnection(nearFake, '');
expect(serverWalletConnection.getAccountId()).toEqual('');
});

it('returns false as isSignedIn', () => {
const serverWalletConnection = new nearApi.WalletConnection(nearFake);
const serverWalletConnection = new nearApi.WalletConnection(nearFake, '');
expect(serverWalletConnection.isSignedIn()).toEqual(false);
});

it('throws explicit error when calling other methods on the instance', () => {
const serverWalletConnection = new nearApi.WalletConnection(nearFake);
const serverWalletConnection = new nearApi.WalletConnection(nearFake, '');
expect(() => serverWalletConnection.requestSignIn('signInContract', 'signInTitle', 'http://example.com/success', 'http://example.com/fail')).toThrow(/please ensure you are using WalletConnection on the browser/);
});

it('can access other props on the instance', () => {
const serverWalletConnection = new nearApi.WalletConnection(nearFake);
const serverWalletConnection = new nearApi.WalletConnection(nearFake, '');
expect(serverWalletConnection['randomValue']).toEqual(undefined);
});
});
Expand Down