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

ConseilJS 5.0.2-beta.3 #303

Merged
merged 7 commits into from
Jul 6, 2020
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ npm i conseiljs-softsigner
import fetch from 'node-fetch';
import * as log from 'loglevel';

import { registerFetch, registerLogger } from 'conseiljs';
import { registerFetch, registerLogger, Signer, TezosMessageUtils } from 'conseiljs';
import { KeyStoreUtils, SoftSigner } from 'conseiljs-softsigner';

const logger = log.getLogger('conseiljs');
Expand All @@ -40,8 +40,8 @@ registerLogger(logger);
registerFetch(fetch);

let signer: Signer;
const keyStore = await KeyStoreUtils.restoreIdentityFromSecretKey ('edskRgu8wHxjwayvnmpLDDijzD3VZDoAH7ZLqJWuG4zg7LbxmSWZWhtkSyM5Uby41rGfsBGk4iPKWHSDniFyCRv3j7YFCknyHH');
signer = new SoftSigner(TezosMessageUtils.writeKeyWithHint(keyStore.secretKey, 'edsk'));
const keyStore = await KeyStoreUtils.restoreIdentityFromSecretKey('edskRgu8wHxjwayvnmpLDDijzD3VZDoAH7ZLqJWuG4zg7LbxmSWZWhtkSyM5Uby41rGfsBGk4iPKWHSDniFyCRv3j7YFCknyHH');
signer = await SoftSigner.createSigner(TezosMessageUtils.writeKeyWithHint(keyStore.secretKey, 'edsk'), -1);
```

## Use with React
Expand All @@ -56,7 +56,7 @@ TBD

```html
<script src="https://cdn.jsdelivr.net/gh/cryptonomic/conseiljs/dist-web/conseiljs.min.js"
integrity="sha384-zf1JnFgeBkUovmAPQjLAJl81QYeEwY3HcMv626FdnOq1cS9pnxSbuBi4tZPfhGjd"
integrity="sha384-CDpCW4rM4qw7oEyOr5wSQNqnPmQw9vtCoqWyKSDIys50jRONU+j+uxk/aEy0QzRJ"
crossorigin="anonymous"></script>
```

Expand Down
2 changes: 1 addition & 1 deletion dist-web/conseiljs.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ We have a complete [React application example](https://github.com/Cryptonomic/Co

```html
<script src="https://cdn.jsdelivr.net/gh/cryptonomic/conseiljs/dist-web/conseiljs.min.js"
integrity="sha384-zf1JnFgeBkUovmAPQjLAJl81QYeEwY3HcMv626FdnOq1cS9pnxSbuBi4tZPfhGjd"
integrity="sha384-CDpCW4rM4qw7oEyOr5wSQNqnPmQw9vtCoqWyKSDIys50jRONU+j+uxk/aEy0QzRJ"
crossorigin="anonymous"></script>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('TezosContractIntrospector integration test suite', () => {
contractQuery = ConseilQueryBuilder.addPredicate(contractQuery, 'account_id', ConseilOperator.STARTSWITH, ['KT1']);
contractQuery = ConseilQueryBuilder.addPredicate(contractQuery, 'script', ConseilOperator.ISNULL, [], true);
contractQuery = ConseilQueryBuilder.addAggregationFunction(contractQuery, 'account_id', ConseilFunction.count);
contractQuery = ConseilQueryBuilder.addOrdering(contractQuery, 'count_account_id', ConseilSortDirection.DESC)
contractQuery = ConseilQueryBuilder.addOrdering(contractQuery, 'count_account_id', ConseilSortDirection.DESC);
contractQuery = ConseilQueryBuilder.setLimit(contractQuery, cap);

const contractList = await ConseilDataClient.executeEntityQuery(conseilServer, 'tezos', conseilServer.network, 'accounts', contractQuery);
Expand Down
23 changes: 15 additions & 8 deletions package-lock.json

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

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "conseiljs",
"version": "5.0.2-beta.2",
"version": "5.0.2-beta.3",
"description": "Client-side library for dApp development.",
"browser": "dist/index-web.js",
"main": "dist/index.js",
Expand Down Expand Up @@ -58,7 +58,10 @@
"require": [
"ts-node/register"
],
"reporter": [ "text", "text-lcov" ],
"reporter": [
"text",
"text-lcov"
],
"sourceMap": true,
"instrument": true
},
Expand All @@ -82,7 +85,7 @@
"awesome-typescript-loader": "5.2.1",
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"conseiljs-softsigner": "5.0.2-beta.1",
"conseiljs-softsigner": "5.0.2-beta.3",
"copyfiles": "2.3.0",
"coveralls": "3.1.0",
"eslint": "7.2.0",
Expand Down
3 changes: 3 additions & 0 deletions src/chain/tezos/TezosMessageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ export namespace TezosMessageUtils {

/**
* Computes a BLAKE2b message hash of the requested length.
*
* @param {Buffer} payload Buffer to hash
* @param {number} length Length of hash to produce
*/
export function simpleHash(payload: Buffer, length: number) : Buffer {
return Buffer.from(blakejs.blake2b(payload, null, length)); // Same as libsodium.crypto_generichash
Expand Down
50 changes: 44 additions & 6 deletions src/reporting/tezos/TezosConseilClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ export namespace TezosConseilClient {
/**
* Returns big_map data for a given contract if any is available.
*
* @param serverInfo Conseil server connection definition.
* @param accountID Account hash to query for.
* @param serverInfo Conseil server connection definition.
* @param contract Contract address to query for.
*/
export async function getBigMapData(serverInfo: ConseilServerInfo, accountID: string): Promise<ContractMapDetails | undefined> {
if (!accountID.startsWith('KT1')) { throw new Error('Invalid address'); }
export async function getBigMapData(serverInfo: ConseilServerInfo, contract: string): Promise<ContractMapDetails | undefined> {
if (!contract.startsWith('KT1')) { throw new Error('Invalid address'); }

const ownerQuery = ConseilQueryBuilder.setLimit(ConseilQueryBuilder.addFields(ConseilQueryBuilder.addPredicate(ConseilQueryBuilder.blankQuery(), 'account_id', ConseilOperator.EQ, [accountID], false), 'big_map_id'), 100);
const ownerQuery = ConseilQueryBuilder.setLimit(ConseilQueryBuilder.addFields(ConseilQueryBuilder.addPredicate(ConseilQueryBuilder.blankQuery(), 'account_id', ConseilOperator.EQ, [contract], false), 'big_map_id'), 100);
const ownerResult = await getTezosEntityData(serverInfo, serverInfo.network, 'originated_account_maps', ownerQuery);

if (ownerResult.length < 1) { return undefined; }
Expand All @@ -296,7 +296,45 @@ export namespace TezosConseilClient {
maps.push({definition, content});
}

return { contract: accountID, maps };
return { contract, maps };
}

/**
* Returns a value for a given plain-text key. The big map id is either provided as a paramter or the smallest of the possibly multiple big maps associated with the given contract address is used. Note that sometimes key values must be wrapped in quotes for key types that are non-numeric and not byte type.
*
* Under normal circumstances these keys are hashed and TezosNodeReader.getValueForBigMapKey() expects such an encoded key. However, with the Conseil indexer it's possible to query for plain-text keys.
*
* @param serverInfo Conseil server connection definition.
* @param key Key to query for.
Copy link
Contributor

Choose a reason for hiding this comment

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

We should specify whether this is encoded or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

docs were updated

* @param contract Optional contract address to be used to identify an associated big map.
* @param mapIndex Optional big map index to query, but one of contract or mapIndex must be provided.
*/
export async function getBigMapValueForKey(serverInfo: ConseilServerInfo, key: string, contract: string = '', mapIndex: number = -1): Promise<string> {
if (!contract.startsWith('KT1')) { throw new Error('Invalid address'); }
if (key.length < 1) { throw new Error('Invalid key'); }
if (mapIndex < 0 && contract.length === 0) { throw new Error('One of contract or mapIndex must be specified'); }

if (mapIndex < 0 && contract.length > 0) {
let ownerQuery = ConseilQueryBuilder.setLimit(ConseilQueryBuilder.blankQuery(), 1);
ownerQuery = ConseilQueryBuilder.addFields(ownerQuery, 'big_map_id');
ownerQuery = ConseilQueryBuilder.addPredicate(ownerQuery, 'account_id', ConseilOperator.EQ, [contract], false);
ownerQuery = ConseilQueryBuilder.addOrdering(ownerQuery, 'big_map_id', ConseilSortDirection.DESC);

const ownerResult = await getTezosEntityData(serverInfo, serverInfo.network, 'originated_account_maps', ownerQuery);

if (ownerResult.length < 1) { throw new Error(`Could not find any maps for ${contract}`); }
mapIndex = ownerResult[0];
}

let contentQuery = ConseilQueryBuilder.setLimit(ConseilQueryBuilder.blankQuery(), 1);
contentQuery = ConseilQueryBuilder.addFields(contentQuery, 'value');
contentQuery = ConseilQueryBuilder.addPredicate(contentQuery, 'key', ConseilOperator.EQ, [key], false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it not possible to automatically encode the key? I forget the details.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not at this time, contract parser will need updates.

contentQuery = ConseilQueryBuilder.addPredicate(contentQuery, 'big_map_id', ConseilOperator.EQ, [mapIndex], false);
const contentResult = await getTezosEntityData(serverInfo, serverInfo.network, 'big_map_contents', contentQuery);

if (contentResult.length < 1) { throw new Error(`Could not a value for key ${key} in map ${mapIndex}`); }

return contentResult[0];
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/types/ExternalInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Signer {
signOperation: (bytes: Buffer) => Promise<Buffer>;
signText: (message: string) => Promise<string>;
signTextHash: (message: string) => Promise<string>;
}

export interface KeyStore {
Expand Down
4 changes: 2 additions & 2 deletions test/chain/tezos/TezosNodeWriter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ describe('TezosNodeWriter tests', () => {
registerFetch(fetch);

keyStore = await KeyStoreUtils.restoreIdentityFromSecretKey(accounts[0].secretKey);
signer = new SoftSigner(TezosMessageUtils.writeKeyWithHint(keyStore.secretKey, 'edsk'));
signer = await SoftSigner.createSigner(TezosMessageUtils.writeKeyWithHint(keyStore.secretKey, 'edsk'), 0);

faucetKeyStore = await KeyStoreUtils.restoreIdentityFromFundraiser(drips[0].mnemonic.join(' '), drips[0].email, drips[0].password, drips[0].pkh);
faucetSigner = new SoftSigner(TezosMessageUtils.writeKeyWithHint(faucetKeyStore.secretKey, 'edsk'));
faucetSigner = await SoftSigner.createSigner(TezosMessageUtils.writeKeyWithHint(faucetKeyStore.secretKey, 'edsk'), 10);
}));

it('forgeOperationsRemotely test', mochaAsync(async () => {
Expand Down