Skip to content

Commit

Permalink
Merge pull request #189 from nearprotocol/find-access-key
Browse files Browse the repository at this point in the history
Change the way access key is fetched by Account
  • Loading branch information
vgrichina authored Jan 22, 2020
2 parents f26d1cb + 17bf1b9 commit 1ddc03a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/account.d.ts

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

25 changes: 13 additions & 12 deletions lib/account.js

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

28 changes: 15 additions & 13 deletions src.ts/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export class Account {
readonly connection: Connection;
readonly accountId: string;
private _state: AccountState;
private _accessKey: AccessKey;

private _ready: Promise<void>;
protected get ready(): Promise<void> {
Expand All @@ -53,16 +52,7 @@ export class Account {
}

async fetchState(): Promise<void> {
this._accessKey = null;
this._state = await this.connection.provider.query(`account/${this.accountId}`, '');
const publicKey = await this.connection.signer.getPublicKey(this.accountId, this.connection.networkId);
if (!publicKey) {
return;
}
this._accessKey = await this.connection.provider.query(`access_key/${this.accountId}/${publicKey.toString()}`, '');
if (!this._accessKey) {
throw new Error(`Failed to fetch access key for '${this.accountId}' with public key ${publicKey.toString()}`);
}
}

async state(): Promise<AccountState> {
Expand Down Expand Up @@ -94,14 +84,17 @@ export class Account {

private async signAndSendTransaction(receiverId: string, actions: Action[]): Promise<FinalExecutionOutcome> {
await this.ready;
if (!this._accessKey) {
throw new TypedError(`Can not sign transactions, no matching key pair found in Signer.`, 'KeyNotFound');

// TODO: Find matching access key based on transaction
const accessKey = await this.findAccessKey();
if (!accessKey) {
throw new TypedError(`Can not sign transactions for account ${this.accountId}, no matching key pair found in Signer.`, 'KeyNotFound');
}

const status = await this.connection.provider.status();

const [txHash, signedTx] = await signTransaction(
receiverId, ++this._accessKey.nonce, actions, base_decode(status.sync_info.latest_block_hash), this.connection.signer, this.accountId, this.connection.networkId
receiverId, ++accessKey.nonce, actions, base_decode(status.sync_info.latest_block_hash), this.connection.signer, this.accountId, this.connection.networkId
);

let result;
Expand Down Expand Up @@ -133,6 +126,15 @@ export class Account {
return result;
}

private async findAccessKey(): Promise<AccessKey> {
const publicKey = await this.connection.signer.getPublicKey(this.accountId, this.connection.networkId);
if (!publicKey) {
return null;
}
// TODO: Cache keys and handle nonce errors automatically
return await this.connection.provider.query(`access_key/${this.accountId}/${publicKey.toString()}`, '');
}

async createAndDeployContract(contractId: string, publicKey: string | PublicKey, data: Uint8Array, amount: BN): Promise<Account> {
const accessKey = fullAccessKey();
await this.signAndSendTransaction(contractId, [createAccount(), transfer(amount), addKey(PublicKey.from(publicKey), accessKey), deployContract(data)]);
Expand Down
4 changes: 2 additions & 2 deletions test/account.access_key.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ test('remove access key no longer works', async() => {
await contract.setValue({value: 'test'});
fail('should throw an error');
} catch (e) {
expect(e.message).toMatch(new RegExp(`.*?Signer "${workingAccount.accountId}" doesn't have access key with the given public_key ${publicKey}`));
expect(e.type).toMatch(/InvalidTxError::InvalidAccessKey::AccessKeyNotFound|UntypedError/);
expect(e.message).toEqual(`Can not sign transactions for account ${workingAccount.accountId}, no matching key pair found in Signer.`);
expect(e.type).toEqual('KeyNotFound');
}
});

Expand Down

0 comments on commit 1ddc03a

Please sign in to comment.