Skip to content

Commit

Permalink
Merge pull request #111 from elastos/merge_to_master
Browse files Browse the repository at this point in the history
Merge to master
  • Loading branch information
stiartsly committed Dec 13, 2022
2 parents 86f6a2b + 0bcebc3 commit d97acd7
Show file tree
Hide file tree
Showing 133 changed files with 3,829 additions and 1,858 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports = {

// Test specific
"jest/expect-expect":"off",
"jest/no-disabled-tests":"warn",
"jest/no-disabled-tests": "off",
"jest/no-focused-tests": "warn",
"jest/no-identical-title": "warn",
"jest/prefer-to-have-length": "warn",
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@
"test:node:profile": "JEST_CIRCUS=1 node --prof --no-logfile-per-isolate ./node_modules/.bin/jest --silent=false --verbose --runInBand --config jest.node.config.ts -- crypto/hdkey.test && node --prof-process v8.log > processed.log && echo '' && echo 'Please open processed.log'. && echo ''"
},
"dependencies": {
"@elastosfoundation/did-js-sdk": "^2.2.12",
"axios": "^0.24.0",
"browserfs": "^1.4.3",
"buffer": "^5.6.0",
"dayjs": "^1.10.4",
"promise-queue": "^2.2.5",
"ts-retrofit": "^1.17.0"
"ts-retrofit": "^1.17.0",
"url": "^0.11.0",
"web3": "^1.6.0"
},
"repository": {
"type": "git",
Expand All @@ -53,6 +51,7 @@
"@babel/plugin-proposal-decorators": "^7.16.4",
"@babel/preset-env": "^7.16.4",
"@babel/preset-typescript": "^7.13.0",
"@elastosfoundation/did-js-sdk": "^2.2.12",
"@rollup/plugin-alias": "^3.1.5",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-eslint": "^8.0.1",
Expand All @@ -62,14 +61,19 @@
"@rollup/plugin-replace": "^3.0.0",
"@rollup/plugin-typescript": "^8.2.1",
"@types/bn.js": "^5.1.0",
"@types/bs58": "^4.0.1",
"@types/crypto-js": "^4.0.1",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^26.0.24",
"@types/node": "^14.18.2",
"@types/promise-queue": "^2.2.0",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"antlr4ts-cli": "^0.5.0-alpha.4",
"axios": "^0.24.0",
"babel-jest": "^27.5.1",
"bs58": "^5.0.0",
"buffer": "^5.6.0",
"eslint": "^8.16.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jest": "^26.2.2",
Expand Down Expand Up @@ -97,6 +101,7 @@
"rollup-plugin-visualizer": "^5.5.2",
"ts-jest": "^26.5.6",
"ts-node": "^10.8.0",
"tslib": "^2.3.1",
"typedoc": "^0.22.13",
"typescript": "^4.5.2"
},
Expand Down
64 changes: 45 additions & 19 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import size from 'rollup-plugin-size';
import eslint from '@rollup/plugin-eslint';
import alias from "@rollup/plugin-alias";
import globals from 'rollup-plugin-node-globals';
import inject from "@rollup/plugin-inject";
import { visualizer } from 'rollup-plugin-visualizer';
import { terser } from 'rollup-plugin-terser';
import {terser} from "rollup-plugin-terser";
import pkg from './package.json';

const production = !process.env.ROLLUP_WATCH;
Expand Down Expand Up @@ -178,29 +177,55 @@ export default command => {
input: rollupSourceFile,
onwarn,
external: [
//'browserfs'
/* 'readable-stream',
'readable-stream/transform' */
'axios',
'fs',
'fsevents',
'module',
'os',
'url',
'util',
'crypto-browserify',
'path-browserify',
'stream-browserify',
'elliptic',
'buffer',
'dayjs',
'promise-queue',
'ts-retrofit',
'process-es6',
"lodash",
"util",
"dayjs",
"string_decoder",
"bip32",
"bip39",
"bn.js",
"secp256r1",
"js-crypto-key-utils",
"create-hash",
"jose",
"bs58",
"bs58check",
"jszip",
"libsodium-wrappers",
'@elastosfoundation/did-js-sdk'
],
plugins: [
// IMPORTANT: DON'T CHANGE THE ORDER OF THINGS BELOW TOO MUCH! OTHERWISE YOU'LL GET
// GOOD HEADACHES WITH RESOLVE ERROR, UNEXPORTED CLASSES AND SO ON...
json(),
// Replace fs with browser implementation.
replace({
delimiters: ['', ''],
preventAssignment: true,
include: [
'src/utils/storage/file.ts'
],
values: {
'fs from "./fs"' : 'fs from "./fs.browser.ts"'
}
}),
// Dirty circular dependency removal atttempt
// replace({
// delimiters: ['', ''],
// preventAssignment: true,
// include: [
// 'src/utils/storage/file.ts'
// ],
// values: {
// 'fs from "./fs"' : 'fs from "./fs.browser.ts"'
// }
// }),
// Dirty circular dependency removal attempt
replace({
delimiters: ['', ''],
preventAssignment: true,
Expand Down Expand Up @@ -260,15 +285,16 @@ export default command => {
}),
globals(), // Defines process, Buffer, etc
typescript({
sourceMap: !prodBuild,
exclude: "*.node.ts"
}),
/* nodePolyfills({
stream: true
// crypto:true // Broken, the polyfill just doesn't work. We have to use crypto-browserify directly in our TS code instead.
}), */ // To let some modules bundle NodeJS stream, util, fs ... in browser
inject({
"BrowserFS": "browserfs"
}),
// inject({
// "BrowserFS": "browserfs"
// }),
size(),
...prodBuild ? [
terser()
Expand Down
21 changes: 21 additions & 0 deletions samples/hiveutils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Hive Utilities

This folder contains some useful utilities. Before use this tool:

```
$ npm install
$ npm run hiveutils -- --help
```

## Hive Url Check

Hive url is for downloading the file from other user by the scripting service.

```
# Usage
$ npm run hiveutils -- -e <testnet|mainnet> -u <hive_url> -o <file_name>
# Example
$ npm run hiveutils -- -e testnet -u 'hive://did:elastos:iT6mGBL8nATPhLGowgi2PRRRG85X4TKVYc@did:elastos:ig1nqyyJhwTctdLyDFbZomSbZSjyMN1uor/getMainIdentityAvatar1658844232162?params={"empty":0}' -o icon.png
$ npm run hiveutils -- -e mainnet -u 'hive://did:elastos:iabbGwqUN18F6YxkndmZCiHpRPFsQF1imT@did:elastos:ig1nqyyJhwTctdLyDFbZomSbZSjyMN1uor/getMainIdentityAvatar1627717470347?params={"empty":0}' -o icon.png
```
38 changes: 38 additions & 0 deletions samples/hiveutils/config/mainnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"resolverUrl": "mainnet",
"ipfsGateUrl": "https://ipfs.trinity-tech.io",

"application": {
"name": "developApp",
"mnemonic": "curious push water point border mutual install govern message ordinary fish small",
"passPhrase": "password",
"storepass": "password",
"did": "did:elastos:iYz6EqxJc72BjRPnEZoTn1mjiWsWTgRh6U"
},

"user": {
"name": "developUser",
"mnemonic": "aisle invest solar duck push romance laundry can chimney parade goose gaze",
"passPhrase": "",
"storepass": "password",
"did": "did:elastos:ifJkpmsjykgzT7XDyFo9cqwWYJiaWeTSxL"
},

"node": {
"provider": "https://hive1.trinity-tech.io",
"__comment__": "target is for backup",
"targetDid": "did:elastos:ijYUBb36yCXU6yzhydnkCCAXh7ZRW4X85J",
"targetHost": "https://hive2.trinity-tech.io"
},

"cross": {
"user": {
"__comment__": "the caller for the scripting service",
"name": "crossUser",
"mnemonic": "earth surface exotic absent key divert sun ten engage sadness nasty lyrics",
"passPhrase": "",
"storepass": "password",
"did": "did:elastos:iTGfs68mWLJuEcZ6Fibm7yoeBFaNfFQugi"
}
}
}
134 changes: 134 additions & 0 deletions samples/hiveutils/config/test.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import {Claims, DIDDocument, JWTParserBuilder} from '@elastosfoundation/did-js-sdk';
import {HiveException, AppContext, Logger, ScriptRunner} from '@elastosfoundation/hive-js-sdk';
import testnet from "./testnet.json";
import mainnet from "./mainnet.json";
import {AppDID} from '../did/appdid';
import {UserDID} from '../did/userdid';

export class TestData {
private static LOG = new Logger("TestData");

private static INSTANCE: TestData;

private appInstanceDid: AppDID;
private appInstanceDidDoc: DIDDocument;
// user & owner DID
private userDid: UserDID;
// caller DID for the scripting service
private callerDid: UserDID;

// for user and caller DIDs
private userAppContext: AppContext;
private callerAppContext: AppContext;
// ???
private anonymousContext: AppContext;

private environ: any;

static getInstance(): TestData {
if (!TestData.INSTANCE) {
TestData.INSTANCE = new TestData();
}
return TestData.INSTANCE;
}

private constructor() {
Logger.setDefaultLevel(Logger.ERROR);
}

async setEnv(env: string): Promise<void> {
this.environ = env === 'mainnet' ? mainnet : testnet;
const storeRoot = this.getLocalCachePath(true);
AppContext.setupResolver(this.environ.resolverUrl, storeRoot);

let applicationConfig = this.environ.application;
this.appInstanceDid = await AppDID.create(applicationConfig.name,
applicationConfig.mnemonic,
applicationConfig.passPhrase,
applicationConfig.storepass,
this.environ.resolverUrl,
storeRoot, applicationConfig.did);
this.appInstanceDidDoc = await this.appInstanceDid.getDocument();

let userConfig = this.environ.user;
this.userDid = await UserDID.create(userConfig.name,
userConfig.mnemonic,
userConfig.passPhrase,
userConfig.storepass,
storeRoot, userConfig.did);

TestData.LOG.info(`user_did: ${this.userDid.toString()}, app_did: ${this.appInstanceDid.getAppDid()}, app_ins_did: ${this.appInstanceDid.toString()}`);

let userConfigCaller = this.environ.cross.user;
this.callerDid = await UserDID.create(userConfigCaller.name,
userConfigCaller.mnemonic,
userConfigCaller.passPhrase,
userConfigCaller.storepass,
storeRoot, userConfigCaller.did);

this.userAppContext = await this.createContext(this.userDid, AppDID.APP_DID);
this.callerAppContext = await this.createContext(this.callerDid, AppDID.APP_DID2);
this.anonymousContext = await this.createContext(this.callerDid, AppDID.APP_DID2);
}

/**
* cache tokens and DIDs, to separate testnet and mainnet
*/
private getLocalCachePath(containDIDs): string {
const network = this.environ.resolverUrl;
if (containDIDs) {
// based on 'tests' folder for DIDs cache dir.
return `data/${network}/didCache`;
}

// absolute path for tokens cache dir.
const testsDir = __dirname + '/../../..';
return `${testsDir}/data/${network}/localCache`;
}

/**
* Create app context
* @param userDid
* @param appDid
* @private
*/
private async createContext(userDid?: UserDID, appDid?: string): Promise<AppContext> {
const self = this;
userDid = userDid ? userDid : this.userDid;
appDid = appDid ? appDid : AppDID.APP_DID;
return AppContext.build({
getLocalDataDir() : string {
return self.getLocalCachePath(false);
},
getAppInstanceDocument() : DIDDocument {
return self.appInstanceDidDoc;
},
async getAuthorization(jwtToken : string) : Promise<string> {
try {
let claims : Claims = (await new JWTParserBuilder().setAllowedClockSkewSeconds(300).build().parse(jwtToken)).getBody();
if (claims == null)
throw new HiveException("Invalid jwt token as authorization.");

let presentation = await self.appInstanceDid.createPresentation(
await userDid.issueDiplomaFor(self.appInstanceDid, appDid),
claims.getIssuer(), claims.get("nonce") as string);

TestData.LOG.debug("TestData->presentation: " + presentation.toString(true));
return await self.appInstanceDid.createToken(presentation, claims.getIssuer());
} catch (e) {
TestData.LOG.info("TestData->getAuthorization error: " + e);
TestData.LOG.error(e.stack);
throw e;
}
}
}, userDid.getDid().toString(), appDid);
}

getProviderAddress(): string {
return this.environ.node.provider;
}

newCallerScriptRunner(): ScriptRunner {
return new ScriptRunner(this.callerAppContext, this.getProviderAddress());
}
}
38 changes: 38 additions & 0 deletions samples/hiveutils/config/testnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"resolverUrl": "testnet",
"ipfsGateUrl": "https://ipfs-test.trinity-feeds.app",

"application": {
"name": "developAppTestnet",
"mnemonic": "wash busy tell relief street maximum dumb fat legend end panic warrior",
"passPhrase": "password",
"storepass": "password",
"did":"did:elastos:imMY7uHc5WCDUhWCVaH5MtFFCBMegCaEYH"
},

"user": {
"name": "developUser",
"mnemonic": "decrease eagle devote evoke dragon dignity affair fame honey tourist shrug divorce",
"passPhrase": "123456",
"storepass": "123456",
"did": "did:elastos:ipBaBr6HQ6h92T1fh5fFQS14xhTcyt3Azq"
},

"node": {
"provider": "https://hive-testnet1.trinity-tech.io",
"__comment__": "target is for backup",
"targetDid": "did:elastos:ijebJBsSZxJjKkSZD86kZXXM3uJiUbZcjh",
"targetHost": "https://hive-testnet2.trinity-tech.io"
},

"cross": {
"user": {
"__comment__": "the caller for the scripting service",
"name": "crossUser",
"mnemonic": "stage west lava group genre ten farm pony small family february drink",
"passPhrase": "secret",
"storepass": "password",
"did": "did:elastos:ip1e2eAY6Kw6shCbZhzksy9pYwLjTRFEU7"
}
}
}
Loading

0 comments on commit d97acd7

Please sign in to comment.