Skip to content

Commit

Permalink
Add definition post method (#64)
Browse files Browse the repository at this point in the history
* Add definition post method

* Fix lint

* Update dependencies

* Revert jest version
  • Loading branch information
bonustrack authored Jul 18, 2019
1 parent 4f5d9c9 commit 82fc570
Show file tree
Hide file tree
Showing 7 changed files with 2,948 additions and 2,762 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ declare namespace Obyte {
assetAttestors(params: any): Promise<object>;
data(params: any): Promise<object>;
dataFeed(params: any): Promise<object>;
definition(params: any): Promise<string>;
definitionTemplate(params: any): Promise<string>;
poll(params: any): Promise<object>;
profile(params: any): Promise<object>;
Expand All @@ -282,6 +283,7 @@ declare namespace Obyte {
assetAttestors(params: any): Promise<object>;
data(params: any): Promise<string>;
dataFeed(params: any): Promise<string>;
definition(params: any): Promise<string>;
definitionTemplate(params: any): Promise<string>;
poll(params: any): Promise<string>;
profile(params: any): Promise<string>;
Expand Down
5,644 changes: 2,907 additions & 2,737 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obyte",
"version": "0.1.4",
"version": "0.1.5",
"description": "A pure and powerful JavaScript Obyte library",
"homepage": "https://obytejs.com",
"browser": "dist/obyte.min.js",
Expand All @@ -27,30 +27,30 @@
"dependencies": {
"babel-runtime": "^6.26.0",
"create-hash": "^1.2.0",
"secp256k1": "^3.6.2",
"secp256k1": "^3.7.1",
"thirty-two": "^1.0.2",
"wif": "^2.0.6",
"ws": "^6.2.0"
"ws": "^7.1.0"
},
"devDependencies": {
"@taskr/babel": "^1.1.0",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.1",
"babel-eslint": "^10.0.2",
"babel-loader": "^7.1.5",
"babel-plugin-add-module-exports": "^1.0.0",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"eslint": "^5.15.3",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-prettier": "^3.0.1",
"eslint": "^6.0.1",
"eslint-config-airbnb-base": "^13.2.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-prettier": "^3.1.0",
"jest": "^23.6.0",
"prettier": "^1.16.4",
"prettier": "^1.18.2",
"taskr": "^1.1.0",
"webpack": "^4.24.0",
"webpack-cli": "^3.1.2",
"webpack": "^4.36.1",
"webpack-cli": "^3.3.6",
"webpack-visualizer-plugin": "^0.1.11"
}
}
3 changes: 3 additions & 0 deletions src/apps.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"data_feed": {
"params": "object"
},
"definition": {
"params": "object"
},
"definition_template": {
"params": "array"
},
Expand Down
8 changes: 4 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import WSClient from './wsclient';
import { getChash160, fromWif } from './utils';
import utils from './utils';
import {
DEFAULT_NODE,
VERSION,
Expand Down Expand Up @@ -49,10 +49,10 @@ export default class Client {
typeof options === 'object'
? { ...self.options, ...options }
: { ...self.options, wif: options };
const privKeyBuf = conf.privateKey || fromWif(conf.wif, conf.testnet).privateKey;
const privKeyBuf = conf.privateKey || utils.fromWif(conf.wif, conf.testnet).privateKey;
const pubkey = toPublicKey(privKeyBuf);
const definition = conf.definition || ['sig', { pubkey }];
const address = conf.address || getChash160(definition);
const address = conf.address || utils.getChash160(definition);
const path = conf.path || 'r';
const version = conf.testnet ? VERSION_TESTNET : VERSION;
const bJsonBased = version !== VERSION_WITHOUT_TIMESTAMP;
Expand All @@ -69,7 +69,7 @@ export default class Client {
`Definition or definition change for address ${address} is not stable yet`,
);

if (objDefinition.definition_chash !== getChash160(definition))
if (objDefinition.definition_chash !== utils.getChash160(definition))
throw new Error(
`Definition chash of address doesn't match the definition chash provided`,
);
Expand Down
23 changes: 17 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import wif from 'wif';
import { chashGetChash160, getSourceString, isChashValid } from './internal';
import { chashGetChash160, getSourceString, isChashValid, getJsonSourceString } from './internal';

export function getChash160(obj) {
return chashGetChash160(getSourceString(obj));
function getChash160(obj) {
const sourceString =
Array.isArray(obj) && obj.length === 2 && obj[0] === 'autonomous agent'
? getJsonSourceString(obj)
: getSourceString(obj);
return chashGetChash160(sourceString);
}

export function toWif(privateKey, testnet) {
function toWif(privateKey, testnet) {
const version = testnet ? 239 : 128;
return wif.encode(version, privateKey, false);
}

export function fromWif(string, testnet) {
function fromWif(string, testnet) {
const version = testnet ? 239 : 128;
return wif.decode(string, version);
}

export function isValidAddress(address) {
function isValidAddress(address) {
return (
typeof address === 'string' &&
address === address.toUpperCase() &&
address.length === 32 &&
isChashValid(address)
);
}

export default {
getChash160,
toWif,
fromWif,
isValidAddress,
};
4 changes: 2 additions & 2 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getChash160 } from '../src/utils';
import { utils } from '../src';
import definition from './definition.json';

describe('utils', () => {
describe('getChash160', () => {
it('should convert definition to valid address', () => {
expect(getChash160(definition)).toEqual('5TROF7O466QKXR3N6AUYKYYQ2JCY24EJ');
expect(utils.getChash160(definition)).toEqual('5TROF7O466QKXR3N6AUYKYYQ2JCY24EJ');
});
});
});

0 comments on commit 82fc570

Please sign in to comment.