Skip to content

Commit

Permalink
initial to public github
Browse files Browse the repository at this point in the history
  • Loading branch information
Dulguun committed Jul 4, 2018
0 parents commit 15acbb2
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
lib/
package-lock.json
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src/
.git/
._*
.DS_Store
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Hycon javascript utilities
Common javascript functions for Hycon.

## Examples

```js
import * as utils from "@glosfer/hyconjs-util";

const result = utils.addressToUint8Array("H497fHm8gbPZxaXySKpV17a7beYBF9Ut3");
console.log(result);
```

### Install dependencies

```bash
npm install
```

### Build

```bash
npm run-script build
```

### Deploy

Checklist before deploying a new release:

* you have the right in the glosfer org on NPM
* you have run `npm login` once (check `npm whoami`)
* Go to **master** branch
* your master point on glosfer repository (check with `git config remote.$(git config branch.master.remote).url` and fix it with `git branch --set-upstream master origin/master`)
* you are in sync (`git pull`) and there is no changes in `git status`
* Run `npm intall` once, there is still no changes in `git status`

**deploy a new release**

```
npm install
npm run-script build
npm publish
```

then, go to [/releases](https://github.com/arigatodl/hyconjs-util/releases) and create a release with change logs.

## Issues & Pull Requests

If you have an issue, feel free to add it to the [Issues](https://github.com/arigatodl/hyconjs-util/issues) tab.
If you'd like to help us out, the [Pull Request](https://github.com/arigatodl/hyconjs-util/pulls) tab is a great place to start.

**If you have found a security bug, please contact us at [security@glosfer.com](security@glosfer.com).**

## Authors

* **Dulguun Batmunkh** - *Initial work* <dulguun@glosfer.com>
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@glosfer/hyconjs-util",
"version": "0.0.2",
"description": "Common javascript functions for Hycon",
"main": "lib/index.js",
"scripts": {
"build": "tsc --declaration"
},
"keywords": [
"hycon",
"utils",
"hyc"
],
"repository": {
"type": "git",
"url": "git://github.com/arigatodl/hyconjs-util.git"
},
"bugs": {
"url": "https://github.com/arigatodl/hyconjs-util/issues"
},
"homepage": "https://github.com/arigatodl/hyconjs-util",
"publishConfig": {
"access": "public"
},
"author": "",
"license": "ISC",
"dependencies": {
"base-58": "0.0.1",
"blake2b": "^2.1.2",
"long": "^4.0.0"
},
"devDependencies": {
"@types/node": "^10.5.1",
"@types/long": "^4.0.0",
"typescript": "^2.9.2"
}
}
85 changes: 85 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as Base58 from "base-58"
import * as blake2b from "blake2b"
import Long = require("long")

export function blake2bHash(ob: Uint8Array | string): Uint8Array {
typeof ob === "string" ? ob = Buffer.from(ob) : ob = ob
return blake2b(32).update(ob).digest()
}

export function publicKeyToAddress(publicKey: Uint8Array): Uint8Array {
const hash: Uint8Array = blake2bHash(publicKey)
const address = new Uint8Array(20)
for (let i = 12; i < 32; i++) {
address[i - 12] = hash[i]
}
return address
}

export function addressToString(publicKey: Uint8Array): string {
return "H" + Base58.encode(publicKey) + addressCheckSum(publicKey)
}

export function addressToUint8Array(address: string) {
if (address.charAt(0) !== "H") {
throw new Error(`Address is invalid. Expected address to start with 'H'`)
}
const check = address.slice(-4)
address = address.slice(1, -4)
const out: Uint8Array = Base58.decode(address)
if (out.length !== 20) {
throw new Error("Address must be 20 bytes long")
}
const expectedChecksum = addressCheckSum(out)
if (expectedChecksum !== check) {
throw new Error(`Address hash invalid checksum '${check}' expected '${expectedChecksum}'`)
}
return out
}

export function addressCheckSum(arr: Uint8Array): string {
const hash = blake2bHash(arr)
let str = Base58.encode(hash)
str = str.slice(0, 4)
return str
}

export function zeroPad(input: string, length: number) {
return (Array(length + 1).join("0") + input).slice(-length)
}

export function hycontoString(val: Long): string {
const int = val.divide(1000000000)
const sub = val.modulo(1000000000)
if (sub.isZero()) {
return int.toString()
}

let decimals = sub.toString()
while (decimals.length < 9) {
decimals = "0" + decimals
}

while (decimals.charAt(decimals.length - 1) === "0") {
decimals = decimals.substr(0, decimals.length - 1)
}

return int.toString() + "." + decimals
}

export function hyconfromString(val: string): Long {
if (val === "" || val === undefined || val === null) { return Long.fromNumber(0, true) }
if (val[val.length - 1] === ".") { val += "0" }
const arr = val.toString().split(".")
let hycon = Long.fromString(arr[0], true).multiply(Math.pow(10, 9))
if (arr.length > 1) {
arr[1] = arr[1].length > 9 ? arr[1].slice(0, 9) : arr[1]
const subCon = Long.fromString(arr[1], true).multiply(Math.pow(10, 9 - arr[1].length))
hycon = hycon.add(subCon)
}
return hycon.toUnsigned()
}

export function encodingMnemonic(str: string): string {
return str.normalize("NFKD")
}
23 changes: 23 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "lib",
"sourceMap": true,
"typeRoots": [
"./node_modules/@types",
"./types"
],
"jsx": "react"
},
"include": [
"src/**/*",
"test/*"
],
"exclude": [
"node_modules"
]
}
16 changes: 16 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"semicolon": [
true,
"never"
],
"no-empty": false,
"max-line-length": false
},
"rulesDirectory": []
}
5 changes: 5 additions & 0 deletions types/base-58/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'base-58'
{
export function encode(buffer: Uint8Array): string
export function decode(string: string): Uint8Array
}
28 changes: 28 additions & 0 deletions types/blake2b/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
declare module 'blake2b'
{
class Blake2b {
public update(input: ArrayLike<number>): Blake2b;
public digest(out?: "binary" | "hex" | ArrayLike<number>): Uint8Array
public final(out?: "binary" | "hex" | ArrayLike<number>): Uint8Array
}

let createHash: {
(outlen: number, key?: ArrayLike<number>, salt?: ArrayLike<number>, personal?: ArrayLike<number>, noAssert?: boolean): Blake2b

WASM_SUPPORTED: boolean
WASM_LOADED: boolean

BYTES_MIN: number
BYTES_MAX: number
BYTES: number
KEYBYTES_MIN: number
KEYBYTES_MAX: number
KEYBYTES: number
SALTBYTES: number
PERSONALBYTES: number

ready(callback: () => void): void;
}

export = createHash
}

0 comments on commit 15acbb2

Please sign in to comment.