-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from elastos/merge_to_master
Merge to master
- Loading branch information
Showing
133 changed files
with
3,829 additions
and
1,858 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
Oops, something went wrong.