Skip to content

Commit

Permalink
support forge v0.27.0 (#281)
Browse files Browse the repository at this point in the history
* chore: support --yes on forge reset

* fix: js2dts dependency

* chore: upgrade forge-proto with forge v0.27.0

* chore: upgrade grpc-client with forge v0.27.0

* chore: upgrade graphql-client with forge v0.27.0

* chore: add fromHash and fromPublicKeyHash for did

* feat: support new asset address generating algorithm

* chore: update dts and cleanup test case

* chore: upgrade sdk-util dependency version

* chore: bump version
  • Loading branch information
wangshijun authored May 16, 2019
1 parent 6518d84 commit 75d414f
Show file tree
Hide file tree
Showing 47 changed files with 8,809 additions and 5,474 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 0.27.0 (May 16, 2019)
- chore: upgrade sdk-util dependency version
- chore: update dts and cleanup test case
- feat: support new asset address generating algorithm
- chore: add fromHash and fromPublicKeyHash for did
- chore: upgrade graphql-client with forge v0.27.0
- chore: upgrade grpc-client with forge v0.27.0
- chore: upgrade forge-proto with forge v0.27.0
- fix: js2dts dependency
- chore: support --yes on forge reset
- update readme

## 0.26.11 (May 14, 2019)
- feat: add api to mark token as expired in did handler
- update readme
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TOP_DIR=.
README=$(TOP_DIR)/README.md
SDK_VERSION=0.25
SDK_VERSION=0.27

VERSION=$(strip $(shell cat version))

Expand Down
13 changes: 12 additions & 1 deletion apps/forge-cli/src/cli/node/reset/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const shell = require('shelljs');
const chalk = require('chalk');
const { cli, action } = require('core/cli');
const { execute, run } = require('./reset');

Expand All @@ -8,5 +10,14 @@ cli('reset', 'Reset current chain state, run with caution', input => action(exec
rpcClient: false,
wallet: false,
},
options: [],
options: [['-y, --yes', 'Use default value for all options']],
handlers: {
'--help': () => {
shell.echo(`
Examples:
- ${chalk.cyan('forge reset')} reset interactively
- ${chalk.cyan('forge reset --yes')} force reset
`);
},
},
});
29 changes: 17 additions & 12 deletions apps/forge-cli/src/cli/node/reset/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const inquirer = require('inquirer');
const { config } = require('core/env');
const { symbols } = require('core/ui');

async function main() {
async function main({ opts: { yes } }) {
// Confirm stopped
const { starterBinPath, forgeConfigPath } = config.get('cli');
const { stdout: pid } = shell.exec(`FORGE_CONFIG=${forgeConfigPath} ${starterBinPath} pid`, {
Expand All @@ -20,17 +20,22 @@ async function main() {
}

// Confirm
const questions = [
{
type: 'confirm',
name: 'confirm',
default: false,
message: chalk.red(
'Reset chain state will erase all local data, including transactions/logs, are you sure to continue?'
),
},
];
const { confirm } = await inquirer.prompt(questions);
let confirm = yes;
if (!yes) {
const questions = [
{
type: 'confirm',
name: 'confirm',
default: false,
message: chalk.red(
'Reset chain state will erase all local data, including transactions/logs, are you sure to continue?'
),
},
];
const answers = await inquirer.prompt(questions);
confirm = answers.confirm;
}

if (confirm) {
shell.exec('rm -rf ~/.forge_release');
shell.echo(`${symbols.info} rm -rf ~/.forge_release`);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
"url": "https://github.com/wangshijun"
},
"devDependencies": {
"@arcblock/docblock": "1.2.0",
"eslint": "5.6.0",
"eslint-config-prettier": "latest",
"eslint-plugin-prettier": "latest",
"eslint-plugin-react-hooks": "^1.6.0",
"husky": "^0.14.3",
"js2dts": "^0.3.3",
"jsdoc": "^3.5.5",
"@arcblock/docblock": "1.2.0",
"lerna": "^3.3.2",
"lint-staged": "^7.2.2",
"prettier": "^1.6.1",
Expand Down
1 change: 0 additions & 1 deletion packages/did-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"multibase": "^0.6.0"
},
"devDependencies": {
"j2d": "^1.0.3-dev",
"jest": "^23.5.0",
"remark-cli": "^5.0.0",
"remark-preset-github": "^0.0.9"
Expand Down
48 changes: 44 additions & 4 deletions packages/did-util/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
// Generate by [[email protected].2](https://github.com/whxaxes/js2dts#readme)
// Generate by [[email protected].3](https://github.com/whxaxes/js2dts#readme)

/**
* Create an asset address, eg: the did of an asset
* Create an asset address
*
* @public
* @static
* @param {object} itx - an object of `CreateAssetTx`
* @param {string} sender - creator address, also the initial owner of the asset
* @returns {string} asset address without `did:abt:` prefix
*/
declare function toAssetAddress(itx: any, sender: string): string;
declare function toAssetAddress(itx: any): string;
/**
* Create an asset did
*
* @public
* @static
* @param {object} itx - an object of `CreateAssetTx`
* @returns {string} asset address without `did:abt:` prefix
*/
declare function toAssetDid(itx: any): string;
/**
* Create an itx address
*
* @public
* @static
* @param {object} itx - an object of forge supported itx
* @returns {string} itx address without `did:abt:` prefix
*/
declare function toItxAddress(itx: any, type: any): string;
/**
* Create an itx did
*
* @public
* @static
* @param {object} itx - an object of forge supported itx
* @returns {string} itx address without `did:abt:` prefix
*/
declare function toItxDid(itx: any, type: any): string;
/**
* Generate an stake address, eg: the did of the stake
*
Expand All @@ -20,11 +46,25 @@ declare function toAssetAddress(itx: any, sender: string): string;
* @returns {string} stake address without `did:abt:` prefix
*/
declare function toStakeAddress(sender: string, receiver: string): string;
/**
* Generate an stake address, eg: the did of the stake
*
* @public
* @static
* @param {string} sender - sender address
* @param {string} receiver - receiver address
* @returns {string} stake address without `did:abt:` prefix
*/
declare function toStakeDid(sender: string, receiver: string): string;
declare const _Lib: _Lib.T100;
declare namespace _Lib {
export interface T100 {
toAssetAddress: typeof toAssetAddress;
toAssetDid: typeof toAssetDid;
toItxAddress: typeof toItxAddress;
toItxDid: typeof toItxDid;
toStakeAddress: typeof toStakeAddress;
toStakeDid: typeof toStakeDid;
}
}
export = _Lib;
85 changes: 63 additions & 22 deletions packages/did-util/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,66 @@
* @requires @arcblock/forge-message
*/
const { types, Hasher } = require('@arcblock/mcrypto');
const { WalletType } = require('@arcblock/forge-wallet');
const { hexToBytes } = require('@arcblock/forge-util');
const { fromPublicKey, toTypeInfo } = require('@arcblock/did');
const { fromHash, toDid } = require('@arcblock/did');
const { createMessage } = require('@arcblock/forge-message/lite');
const { transactions } = require('@arcblock/forge-proto/lite');

/**
* Create an asset address, eg: the did of an asset
* Create an asset address
*
* @public
* @static
* @param {object} itx - an object of `CreateAssetTx`
* @param {string} sender - creator address, also the initial owner of the asset
* @returns {string} asset address without `did:abt:` prefix
*/
function toAssetAddress(itx, sender) {
const walletType = toTypeInfo(sender);
walletType.role = types.RoleType.ROLE_ASSET;
function toAssetAddress(itx) {
return toItxAddress(itx, 'CreateAssetTx');
}

itx.address = '';
/**
* Create an asset did
*
* @public
* @static
* @param {object} itx - an object of `CreateAssetTx`
* @returns {string} asset address without `did:abt:` prefix
*/
function toAssetDid(itx) {
return toDid(toAssetAddress(itx));
}

const addressBuffer = Buffer.from(sender);
const message = createMessage('CreateAssetTx', itx);
const itxBytes = message.serializeBinary();
const itxSha3Buffer = Buffer.from(hexToBytes(Hasher.SHA3.hash256(itxBytes)));
const buffer = Buffer.concat([addressBuffer, itxSha3Buffer]);
/**
* Create an itx address
*
* @public
* @static
* @param {object} itx - an object of forge supported itx
* @returns {string} itx address without `did:abt:` prefix
*/
function toItxAddress(itx, type) {
if (transactions.indexOf(type) === -1) {
throw new Error(`Unsupported itx type ${type}`);
}

const address = fromPublicKey(buffer, WalletType(walletType));
const message = createMessage(type, itx);
const itxBytes = message.serializeBinary();
const hash = Hasher.SHA3.hash256(itxBytes);
const address = fromHash(hash, types.RoleType.ROLE_ASSET);
return address;
}

/**
* Create an itx did
*
* @public
* @static
* @param {object} itx - an object of forge supported itx
* @returns {string} itx address without `did:abt:` prefix
*/
function toItxDid(itx, type) {
return toDid(toItxAddress(itx, type));
}

/**
* Generate an stake address, eg: the did of the stake
*
Expand All @@ -51,20 +81,31 @@ function toAssetAddress(itx, sender) {
function toStakeAddress(sender, receiver) {
const senderBuffer = Buffer.from(sender);
const receiverBuffer = Buffer.from(receiver);

const buffer = Buffer.concat(
sender < receiver ? [senderBuffer, receiverBuffer] : [receiverBuffer, senderBuffer]
);
const walletType = WalletType({
role: types.RoleType.ROLE_STAKE,
pk: types.KeyType.ED25519,
hash: types.HashType.SHA3,
});
const hash = Hasher.SHA3.hash256(buffer);
return fromHash(hash, types.RoleType.ROLE_STAKE);
}

return fromPublicKey(buffer, walletType);
/**
* Generate an stake address, eg: the did of the stake
*
* @public
* @static
* @param {string} sender - sender address
* @param {string} receiver - receiver address
* @returns {string} stake address without `did:abt:` prefix
*/
function toStakeDid(sender, receiver) {
toDid(toStakeAddress(sender, receiver));
}

module.exports = {
toAssetAddress,
toAssetDid,
toItxAddress,
toItxDid,
toStakeAddress,
toStakeDid,
};
1 change: 0 additions & 1 deletion packages/did-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@arcblock/mcrypto": "^0.26.10"
},
"devDependencies": {
"j2d": "^1.0.3-dev",
"jest": "^23.5.0",
"remark-cli": "^5.0.0",
"remark-preset-github": "^0.0.9"
Expand Down
16 changes: 2 additions & 14 deletions packages/did-util/tests/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
const { types } = require('@arcblock/mcrypto');
const { fromSecretKey, WalletType } = require('@arcblock/forge-wallet');
const { hexToBytes } = require('@arcblock/forge-util');
const { toAssetAddress, toStakeAddress } = require('../lib');

describe('#toAssetAddress', () => {
test('should be a function', () => {
expect(typeof toAssetAddress).toEqual('function');
});
test('should return correct address', () => {
const wallet = fromSecretKey(
'0xac6d1d66322add9e67eb31d26eb30ef108a9cb601bd1323acb637d40469a342cd50c92de3e9387c848c9f8a8d6180502da85ebb761b888cba74c6af090ec65f9',
WalletType({
role: types.RoleType.ROLE_APPLICATION,
pk: types.KeyType.ED25519,
hash: types.HashType.SHA3,
})
);
const itx = {
moniker: 'ABTWalletHelloWorld',
pk: Buffer.from(hexToBytes(wallet.publicKey)),
moniker: 'ABT Wallet Demo Course',
readonly: true,
transferrable: false,
parent: '',
Expand All @@ -37,7 +25,7 @@ describe('#toAssetAddress', () => {
},
};

expect(toAssetAddress(itx, wallet.toAddress())).toEqual('zjdjo9SJJ6CLn2CBMn84fD4f6spiyKDJA8ov');
expect(toAssetAddress(itx)).toEqual('zjdfDYa9m3eBHnAy2djWENwsuDbVQDP9Cz4M');
});
});

Expand Down
4 changes: 3 additions & 1 deletion packages/did/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generate by [[email protected].2](https://github.com/whxaxes/js2dts#readme)
// Generate by [[email protected].3](https://github.com/whxaxes/js2dts#readme)

/**
* Convert did to address: remove `did:abt:` prefix if exists
Expand All @@ -25,6 +25,8 @@ declare namespace _Lib {
toStrictHex: (hex: string, length: number) => string;
fromSecretKey: (sk: string, type: any) => string;
fromPublicKey: (pk: string, type: any) => string;
fromPublicKeyHash: (buffer: any, type: any) => any;
fromHash: (hash: string, role: any) => string;
toTypeInfo: (did: string, returnString?: boolean) => any;
toAddress: typeof toAddress;
toDid: typeof toDid;
Expand Down
Loading

0 comments on commit 75d414f

Please sign in to comment.