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

Adding access keys to nearlib #918

Merged
merged 2 commits into from
Apr 30, 2019
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
81 changes: 57 additions & 24 deletions nearlib/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,52 @@
- [createAccount](#createaccount)
- [Parameters](#parameters-3)
- [Examples](#examples-4)
- [createAccountWithRandomKey](#createaccountwithrandomkey)
- [addAccessKey](#addaccesskey)
- [Parameters](#parameters-4)
- [Examples](#examples-5)
- [viewAccount](#viewaccount)
- [createAccountWithRandomKey](#createaccountwithrandomkey)
- [Parameters](#parameters-5)
- [Examples](#examples-6)
- [viewAccount](#viewaccount)
- [Parameters](#parameters-6)
- [Examples](#examples-7)
- [Near](#near)
- [Parameters](#parameters-6)
- [Parameters](#parameters-7)
- [callViewFunction](#callviewfunction)
- [Parameters](#parameters-7)
- [Examples](#examples-7)
- [scheduleFunctionCall](#schedulefunctioncall)
- [Parameters](#parameters-8)
- [Examples](#examples-8)
- [deployContract](#deploycontract)
- [scheduleFunctionCall](#schedulefunctioncall)
- [Parameters](#parameters-9)
- [Examples](#examples-9)
- [getTransactionStatus](#gettransactionstatus)
- [deployContract](#deploycontract)
- [Parameters](#parameters-10)
- [Examples](#examples-10)
- [waitForTransactionResult](#waitfortransactionresult)
- [getTransactionStatus](#gettransactionstatus)
- [Parameters](#parameters-11)
- [Examples](#examples-11)
- [loadContract](#loadcontract)
- [waitForTransactionResult](#waitfortransactionresult)
- [Parameters](#parameters-12)
- [Examples](#examples-12)
- [createDefaultConfig](#createdefaultconfig)
- [loadContract](#loadcontract)
- [Parameters](#parameters-13)
- [Examples](#examples-13)
- [createDefaultConfig](#createdefaultconfig)
- [Parameters](#parameters-14)
- [Examples](#examples-14)
- [WalletAccount](#walletaccount)
- [Parameters](#parameters-14)
- [Examples](#examples-14)
- [Parameters](#parameters-15)
- [Examples](#examples-15)
- [isSignedIn](#issignedin)
- [Examples](#examples-15)
- [getAccountId](#getaccountid)
- [Examples](#examples-16)
- [requestSignIn](#requestsignin)
- [Parameters](#parameters-15)
- [getAccountId](#getaccountid)
- [Examples](#examples-17)
- [signOut](#signout)
- [Examples](#examples-18)
- [signTransactionBody](#signtransactionbody)
- [requestSignIn](#requestsignin)
- [Parameters](#parameters-16)
- [Examples](#examples-18)
- [signOut](#signout)
- [Examples](#examples-19)
- [signBuffer](#signbuffer)
- [Parameters](#parameters-17)

## require

Expand Down Expand Up @@ -163,6 +166,36 @@ const createAccountResponse = await account.createAccount(
aliceAccountName);
```

### addAccessKey

Adds a new access key to the owners account for an some app to use.

#### Parameters

- `ownersAccountId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** id of the owner's account.
- `newPublicKey` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** public key for the access key.
- `contractId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** if the given contractId is not empty, then this access key will only be able to call
the given contractId.
- `methodName` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** If the given method name is not empty, then this access key will only be able to call
the given method name.
- `fundingOwner` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** account id to own the funding of this access key. If empty then account owner is used by default.
fundingOwner should be used if this access key would be sponsored by the app. In this case the app would
prefer to own funding of this access key, to get it back when the key is removed.
- `fundingAmount` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** amount of funding to withdraw from the owner's account and put to this access key.
Make sure you that you don't fund the access key when the fundingOwner is different from the account's owner.

#### Examples

```javascript
const addAccessKeyResponse = await account.addAccessKey(
accountId,
keyWithRandomSeed.getPublicKey(),
contractId,
"",
"",
10);
```

### createAccountWithRandomKey

Creates a new account with a new random key pair. Returns the key pair to the caller. It's the caller's responsibility to
Expand Down Expand Up @@ -420,12 +453,12 @@ Sign out from the current account
walletAccount.signOut();
```

### signTransactionBody
### signBuffer

Sign a transaction body. If the key for senderAccountId is not present,
Sign a buffer. If the key for originator is not present,
this operation will fail.

#### Parameters

- `body` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `senderAccountId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `buffer` **[Uint8Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)**
- `originator` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
75 changes: 69 additions & 6 deletions nearlib/account.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const bs58 = require('bs58');

const { CreateAccountTransaction, SignedTransaction } = require('./protos');
const { google, AccessKey, AddKeyTransaction, CreateAccountTransaction, SignedTransaction } = require('./protos');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add the new classes to index.js and browser.js?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, since we don't return them to the browser or index

const KeyPair = require('./signing/key_pair');

/**
Expand Down Expand Up @@ -30,28 +30,91 @@ class Account {
const nonce = await this.nearClient.getNonce(originator);
publicKey = bs58.decode(publicKey);
const createAccount = CreateAccountTransaction.create({
nonce,
originator,
newAccountId,
publicKey,
});
// Integers with value of 0 must be omitted
// https://github.com/dcodeIO/protobuf.js/issues/1138
if (nonce !== 0) {
createAccount.nonce = nonce;
}
if (amount !== 0) {
createAccount.amount = amount;
}

const buffer = CreateAccountTransaction.encode(createAccount).finish();
const signature = await this.nearClient.signer.signTransactionBody(
const signatureAndPublicKey = await this.nearClient.signer.signBuffer(
buffer,
originator,
);

const signedTransaction = SignedTransaction.create({
createAccount,
signature,
signature: signatureAndPublicKey.signature,
publicKey: signatureAndPublicKey.publicKey,
});
return this.nearClient.submitTransaction(signedTransaction);
}

/**
* Adds a new access key to the owners account for an some app to use.
* @param {string} ownersAccountId id of the owner's account.
* @param {string} newPublicKey public key for the access key.
* @param {string} contractId if the given contractId is not empty, then this access key will only be able to call
* the given contractId.
* @param {string} methodName If the given method name is not empty, then this access key will only be able to call
* the given method name.
* @param {string} fundingOwner account id to own the funding of this access key. If empty then account owner is used by default.
* fundingOwner should be used if this access key would be sponsored by the app. In this case the app would
* prefer to own funding of this access key, to get it back when the key is removed.
* @param {number} fundingAmount amount of funding to withdraw from the owner's account and put to this access key.
* Make sure you that you don't fund the access key when the fundingOwner is different from the account's owner.
* @example
* const addAccessKeyResponse = await account.addAccessKey(
* accountId,
* keyWithRandomSeed.getPublicKey(),
* contractId,
* "",
* "",
* 10);
*/
async addAccessKey(ownersAccountId, newPublicKey, contractId, methodName, fundingOwner, fundingAmount) {
const nonce = await this.nearClient.getNonce(ownersAccountId);
newPublicKey = bs58.decode(newPublicKey);
const accessKey = AccessKey.create({});
if (contractId) {
accessKey.contractId = google.protobuf.StringValue.create({
value: contractId,
});
}
if (methodName) {
accessKey.methodName = google.protobuf.BytesValue.create({
value: new Uint8Array(Buffer.from(methodName)),
});
}
if (fundingOwner) {
accessKey.balanceOwner = google.protobuf.StringValue.create({
value: fundingOwner,
});
}
if (fundingAmount > 0) {
accessKey.amount = fundingAmount;
}
const addKey = AddKeyTransaction.create({
nonce,
originator: ownersAccountId,
newKey: newPublicKey,
accessKey,
});
const buffer = AddKeyTransaction.encode(addKey).finish();
const signatureAndPublicKey = await this.nearClient.signer.signBuffer(
buffer,
ownersAccountId,
);

const signedTransaction = SignedTransaction.create({
addKey,
signature: signatureAndPublicKey.signature,
publicKey: signatureAndPublicKey.publicKey,
});
return this.nearClient.submitTransaction(signedTransaction);
}
Expand Down
Loading