parent
51eb43237f
commit
72a1442ec5
@ -1,4 +0,0 @@ |
||||
{ |
||||
"files": [], |
||||
"references": [] |
||||
} |
@ -0,0 +1,23 @@ |
||||
const { Account } = require('../packages/harmony-account/lib/index.js'); |
||||
const { |
||||
getAddressFromPublicKey, |
||||
} = require('../packages/harmony-crypto/lib/index.js'); |
||||
const { |
||||
isAddress, |
||||
isPrivateKey, |
||||
} = require('../packages/harmony-utils/lib/index.js'); |
||||
|
||||
// const a = Account.new();
|
||||
|
||||
// console.log(isAddress(a.checksumAddress));
|
||||
|
||||
const importKey = |
||||
'0x87b3ec80f36f9553fb63624d0805d87cfe461145c7be972d23db95fb1a53b1e7'; |
||||
|
||||
const c = Account.add(importKey); |
||||
|
||||
// console.log(isPrivateKey(importKey));
|
||||
|
||||
console.log(c); |
||||
|
||||
console.log(getAddressFromPublicKey(c.publicKey)); |
@ -1,11 +1,16 @@ |
||||
{ |
||||
"name": "harmony-account", |
||||
"name": "@harmony/account", |
||||
"version": "0.0.1", |
||||
"description": "account and wallet for harmony", |
||||
"main": "index.js", |
||||
"main": "lib/index.js", |
||||
"typings":"lib/index.d.ts", |
||||
"scripts": { |
||||
"test": "echo \"Error: no test specified\" && exit 1" |
||||
}, |
||||
"author": "neeboo@firestack.one", |
||||
"license": "ISC" |
||||
"license": "ISC", |
||||
"dependencies": { |
||||
"@harmony/crypto": "^0.0.1", |
||||
"@harmony/utils": "^0.0.1" |
||||
} |
||||
} |
||||
|
@ -0,0 +1,55 @@ |
||||
import { |
||||
generatePrivateKey, |
||||
getAddressFromPrivateKey, |
||||
getPubkeyFromPrivateKey, |
||||
toChecksumAddress, |
||||
} from '@harmony/crypto'; |
||||
|
||||
import { isPrivateKey } from '@harmony/utils'; |
||||
|
||||
class Account { |
||||
static new(): Account { |
||||
const newAcc = new Account()._new(); |
||||
return newAcc; |
||||
} |
||||
static add(key: string): Account { |
||||
const newAcc = new Account()._import(key); |
||||
return newAcc; |
||||
} |
||||
|
||||
privateKey?: string; |
||||
publicKey?: string; |
||||
address?: string; |
||||
|
||||
get checksumAddress(): string { |
||||
return this.address ? toChecksumAddress(this.address) : ''; |
||||
} |
||||
constructor(key?: string) { |
||||
if (key === null) { |
||||
this._new(); |
||||
} |
||||
} |
||||
|
||||
private _new(): Account { |
||||
const prv = generatePrivateKey(); |
||||
if (!isPrivateKey(prv)) { |
||||
throw new Error('key gen failed'); |
||||
} |
||||
this.privateKey = prv; |
||||
this.publicKey = getPubkeyFromPrivateKey(this.privateKey); |
||||
this.address = getAddressFromPrivateKey(this.privateKey); |
||||
return this; |
||||
} |
||||
|
||||
private _import(key: string): Account { |
||||
if (!isPrivateKey(key)) { |
||||
throw new Error(`${key} is not PrivateKey`); |
||||
} |
||||
this.privateKey = key; |
||||
this.publicKey = getPubkeyFromPrivateKey(this.privateKey); |
||||
this.address = getAddressFromPrivateKey(this.privateKey); |
||||
return this; |
||||
} |
||||
} |
||||
|
||||
export { Account }; |
@ -0,0 +1 @@ |
||||
export * from './account'; |
@ -0,0 +1,14 @@ |
||||
{ |
||||
"extends": "../../tsconfig.base.json", |
||||
"compilerOptions": { |
||||
"baseUrl": ".", |
||||
"paths": { "*": ["types/*"] }, |
||||
"outDir": "lib", |
||||
"rootDir": "src" |
||||
}, |
||||
"include": ["src", "../../typings/**/*.d.ts"], |
||||
"references": [ |
||||
{ "path": "../harmony-crypto" }, |
||||
{ "path": "../harmony-utils" } |
||||
] |
||||
} |
@ -1,4 +1,8 @@ |
||||
{ |
||||
"extends": "../../tsconfig.base.json", |
||||
"compilerOptions": { |
||||
"outDir": "lib", |
||||
"rootDir": "src" |
||||
}, |
||||
"include": ["src", "../../typings/**/*.d.ts"] |
||||
} |
||||
|
@ -0,0 +1 @@ |
||||
export * from './validators'; |
@ -0,0 +1,15 @@ |
||||
export const isKeyString = (keyString: string, lengh: number): boolean => { |
||||
return !!keyString.replace('0x', '').match(`^[0-9a-fA-F]{${lengh}}$`); |
||||
}; |
||||
|
||||
export const isAddress = (address: string): boolean => { |
||||
return isKeyString(address, 40); |
||||
}; |
||||
|
||||
export const isPrivateKey = (privateKey: string): boolean => { |
||||
return isKeyString(privateKey, 64); |
||||
}; |
||||
|
||||
export const isPublicKey = (publicKey: string): boolean => { |
||||
return isKeyString(publicKey, 66); |
||||
}; |
@ -0,0 +1,8 @@ |
||||
{ |
||||
"extends": "../../tsconfig.base.json", |
||||
"compilerOptions": { |
||||
"outDir": "lib", |
||||
"rootDir": "src" |
||||
}, |
||||
"include": ["src", "../../typings/**/*.d.ts"] |
||||
} |
@ -1,6 +1,6 @@ |
||||
export default [ |
||||
// 'harmony-account,
|
||||
'harmony-utils', |
||||
'harmony-crypto', |
||||
'harmony-account', |
||||
// 'harmony-network',
|
||||
// 'harmony-utils',
|
||||
]; |
||||
|
@ -0,0 +1,8 @@ |
||||
const packages = [ |
||||
'harmony-utils', |
||||
'harmony-crypto', |
||||
'harmony-account', |
||||
// 'harmony-network',
|
||||
]; |
||||
|
||||
export { packages }; |
@ -0,0 +1,8 @@ |
||||
{ |
||||
"compilerOptions": { |
||||
"esModuleInterop": true, |
||||
"module": "commonjs", |
||||
"target": "es2015", |
||||
"resolveJsonModule": true |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
import * as fs from 'fs'; |
||||
import * as path from 'path'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import * as schemas from 'typescript-json-schema'; |
||||
|
||||
import { packages } from '../packagesTs'; |
||||
import tsConfig from '../../tsconfig.base.json'; |
||||
|
||||
const outputs = process.argv.slice(2)[0].split(','); |
||||
|
||||
const rootPath = path.resolve(__dirname, '../..'); |
||||
const includesPath = path.join(rootPath, 'includes'); |
||||
const packagesPath = path.join(rootPath, 'packages'); |
||||
|
||||
async function generateSchemas() { |
||||
packages |
||||
// @ts-ignore
|
||||
.filter((pkg) => { |
||||
return ( |
||||
pkg !== 'harmony-' && |
||||
outputs.indexOf(pkg.replace('harmony-', '')) !== -1 |
||||
); |
||||
}) |
||||
.forEach((pkg) => { |
||||
const pkgPath = path.join(packagesPath, pkg); |
||||
const pkgSrc = path.join(pkgPath, 'src'); |
||||
const settings = { |
||||
ref: false, |
||||
}; |
||||
// tslint:disable-next-line: no-shadowed-variable
|
||||
const tsConfig: schemas.CompilerOptions = { |
||||
lib: ['es2015'], |
||||
}; |
||||
|
||||
const prog = schemas.getProgramFromFiles( |
||||
[path.resolve(path.join(pkgSrc, 'types.ts'))], |
||||
tsConfig, |
||||
); |
||||
|
||||
const schema = schemas.generateSchema(prog, '*', settings); |
||||
|
||||
fs.writeFileSync( |
||||
path.join(pkgPath, 'test', 'schema.json'), |
||||
JSON.stringify(schema, undefined, 2), |
||||
); |
||||
}); |
||||
} |
||||
|
||||
generateSchemas(); |
@ -0,0 +1,8 @@ |
||||
{ |
||||
"files": [], |
||||
"references": [ |
||||
{ "path": "packages/harmony-account" }, |
||||
{ "path": "packages/harmony-crypto" } |
||||
{ "path": "packages/harmony-utils" } |
||||
] |
||||
} |
Loading…
Reference in new issue