-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c40095e
Showing
14 changed files
with
1,014 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Node.js Package | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
- run: yarn | ||
- run: yarn build ${{ github.event.release.tag_name }} | ||
- uses: JS-DevTools/npm-publish@v1 | ||
with: | ||
token: ${{ secrets.NPM_TOKEN }} | ||
package: ./dist/package.json |
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,2 @@ | ||
/dist | ||
/node_modules |
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 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Ukrainian Social Platform | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,25 @@ | ||
# Support Ukraine <img alt="ukraine" height="32" width="32" src="https://github.githubassets.com/images/icons/emoji/unicode/1f1fa-1f1e6.png"> | ||
|
||
While you're reading this text we're suffering from russia's bombs. Please help us to stand against russia's invasion and prevent World War III. It's pretty easy with **[UNITED24 fundraising platform](https://u24.gov.ua/)**. Thank you! | ||
|
||
## Ukrainian Social Platform BIP39 dictionaries | ||
|
||
[![License][license_badge]][license] [![Runtime][runtime_badge]][runtime] [![Node.js version][runtime_version_badge]][runtime] | ||
[![Build][build_badge]][build_action] [![GitHub release][release_badge]][releases] [![NPM release][npm_version_badge]][npm_link] | ||
|
||
[license_badge]: https://img.shields.io/github/license/ukrainian-social-platform/client-crypto?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGZpbGw9Im5vbmUiIHZpZXdCb3g9IjAgMCAyNCAyNCIgc3Ryb2tlPSIjRkZENzAwIj48cGF0aCBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS13aWR0aD0iMiIgZD0iTTMgNmwzIDFtMCAwbC0zIDlhNS4wMDIgNS4wMDIgMCAwMDYuMDAxIDBNNiA3bDMgOU02IDdsNi0ybTYgMmwzLTFtLTMgMWwtMyA5YTUuMDAyIDUuMDAyIDAgMDA2LjAwMSAwTTE4IDdsMyA5bS0zLTlsLTYtMm0wLTJ2Mm0wIDE2VjVtMCAxNkg5bTMgMGgzIi8%2BPC9zdmc%2BCg%3D%3D&label=License&style=flat-square | ||
[runtime_badge]: https://img.shields.io/badge/Runtime-Node.js-339933?logo=nodedotjs&logoColor=339933&style=flat-square | ||
[runtime_version_badge]: https://img.shields.io/badge/dynamic/json?label=Node.js%20version&query=%24.engines.node&url=https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2F%40ukrainian-social-platform%2Fclient-crypto%2Fpackage.json&color=339933&logo=nodedotjs&logoColor=339933&style=flat-square | ||
[build_badge]: https://img.shields.io/github/workflow/status/ukrainian-social-platform/client-crypto/Node.js%20Package?style=flat-square&label=Build&logo=github | ||
[release_badge]: https://img.shields.io/github/v/release/ukrainian-social-platform/client-crypto?label=GitHub%20release&logo=github&style=flat-square | ||
[npm_version_badge]: https://img.shields.io/npm/v/%40ukrainian-social-platform%2Fclient-crypto?color=cb3837&label=NPM%20release&logo=npm&style=flat-square | ||
|
||
[license]: https://github.com/ukrainian-social-platform/client-crypto/blob/master/LICENSE | ||
[runtime]: https://nodejs.org/ | ||
[build_action]: https://github.com/ukrainian-social-platform/client-crypto/actions/workflows/package.yml | ||
[releases]: https://github.com/ukrainian-social-platform/client-crypto/releases | ||
[npm_link]: https://www.npmjs.com/package/%40ukrainian-social-platform%2Fclient-crypto | ||
|
||
|
||
A set of client-side cryptographic methods for Ukrainian Social Platform. | ||
Builds itself using [tsup](https://tsup.egoist.dev). |
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,31 @@ | ||
{ | ||
"name": "@ukrainian-social-platform/client-crypto", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsup && ./postbuild.sh" | ||
}, | ||
"dependencies": { | ||
"@ukrainian-social-platform/bip39-dictionaries": "^0.0.1" | ||
}, | ||
"devDependencies": { | ||
"tsup": "^6.3.0", | ||
"typescript": "^4.8.4" | ||
}, | ||
"tsup": { | ||
"entry": [ | ||
"src/index.ts" | ||
], | ||
"splitting": false, | ||
"sourcemap": true, | ||
"clean": true, | ||
"minify": true, | ||
"dts": true, | ||
"target": [ | ||
"node18" | ||
], | ||
"format": [ | ||
"esm", | ||
"cjs" | ||
] | ||
} | ||
} |
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,76 @@ | ||
#!/bin/bash | ||
|
||
# mimic tsup =) | ||
|
||
start=`date +%s` | ||
|
||
# basic helpers | ||
|
||
prefix='PKG' | ||
dist_dir='dist' | ||
|
||
styled() { | ||
echo -e "\e[$1m$2\e[0m" | ||
} | ||
|
||
notify_text() { | ||
echo "$(styled 34 "$prefix") $1" | ||
} | ||
|
||
notify_file_result() { | ||
echo "$(styled 32 "$prefix") $(styled 1 "$1") $(styled 32 "$2")" | ||
} | ||
|
||
notify_success() { | ||
echo "$(styled 32 "$prefix") ⚡️ Build success in $((`date +%s` - start))ms" | ||
} | ||
|
||
to_kb() { | ||
local input="$1" | ||
local prefix="" | ||
if [ $1 -lt 1024 ]; then | ||
prefix=0 | ||
fi | ||
echo "$prefix$(echo "scale=2; $input/1024" | bc -l)" KB | ||
} | ||
|
||
# start the process | ||
|
||
notify_text "Build start" | ||
|
||
package_json_file="$dist_dir/package.json" | ||
license_file="$dist_dir/LICENSE" | ||
readme_file="$dist_dir/README.md" | ||
|
||
read -r -d '' package_json_text <<EOF | ||
{ | ||
"name": "@ukrainian-social-platform/client-crypto", | ||
"version": "${1:1}", | ||
"type": "module", | ||
"private": false, | ||
"main": "index.cjs", | ||
"module": "index.js", | ||
"license": "MIT", | ||
"description": "Crypto methods intended to be used within Ukrainian Social Platform", | ||
"repository": "github:ukrainian-social-platform/client-crypto", | ||
"dependencies": { | ||
"@ukrainian-social-platform/bip39-dictionaries": "^0.0.1" | ||
}, | ||
"engines" : { | ||
"node" : ">=18.0.0" | ||
} | ||
} | ||
EOF | ||
license_text="$(cat LICENSE)" | ||
readme_text="$(cat README.md)" | ||
|
||
echo "$package_json_text" > "$package_json_file" | ||
notify_file_result "$package_json_file" "$(to_kb $(echo "$package_json_text" | wc -c))" | ||
|
||
echo "$license_text" > "$license_file" | ||
notify_file_result "$license_file" "$(to_kb $(echo "$license_text" | wc -c))" | ||
|
||
echo "$readme_text" > "$readme_file" | ||
notify_file_result "$readme_file" "$(to_kb $(echo "$readme_text" | wc -c))" | ||
|
||
notify_success |
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,27 @@ | ||
export { default as getDictionary } from '@ukrainian-social-platform/bip39-dictionaries'; | ||
|
||
export async function generatePhrase(wordlist: string[]) { | ||
const buf = crypto.getRandomValues(new Uint8Array(32)); | ||
const sha = new Uint8Array(await crypto.subtle.digest('SHA-256', buf)); | ||
const binaryString = [...buf, sha[0]].map((v) => v.toString(2).padStart(8, '0')).join(''); | ||
const positions = binaryString.match(/.{11}/g)!.map((v) => Number.parseInt(v, 2)); | ||
return positions.map((i) => wordlist[i]).join(' '); | ||
} | ||
|
||
export async function parsePhrase(phrase: string, wordlist: string[]): Promise<[result: Uint8Array, shaOk: boolean]> { | ||
const words = phrase.split(' '); | ||
if (words.length !== 24) { | ||
throw new Error('Phrase should consist of 24 words'); | ||
} | ||
let binaryString = ''; | ||
words.forEach((word) => { | ||
const position = wordlist.indexOf(word); | ||
if (position === -1) throw new Error(`Can't find word ${word} in the dictionary`); | ||
binaryString += position.toString(2).padStart(11, '0'); | ||
}); | ||
const bufValues = binaryString.match(/.{8}/g)!.map((v) => Number.parseInt(v, 2)); | ||
const sha0 = bufValues.pop(); | ||
const buf = new Uint8Array(bufValues); | ||
const sha = new Uint8Array(await crypto.subtle.digest('SHA-256', buf)); | ||
return [ buf, sha0 === sha[0] ]; | ||
} |
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,4 @@ | ||
export { getDictionary, parsePhrase, generatePhrase } from './bip39'; | ||
export { createKey } from './utils/key'; | ||
export { encrypt } from './utils/encrypt'; | ||
export { decrypt } from './utils/decrypt'; |
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,11 @@ | ||
export const encryptionAlgorithmName = 'AES-CBC'; | ||
|
||
export const encryptionKeyLength = 128; | ||
|
||
export const encrypionIvLength = 16; | ||
|
||
export const derivationAlgorithmName = 'PBKDF2'; | ||
|
||
export const derivationHash = 'SHA-256'; | ||
|
||
export const derivationIterations = 4096; |
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,11 @@ | ||
import { encryptionAlgorithmName, encrypionIvLength } from './algorithm'; | ||
|
||
export async function decrypt(data: Uint8Array, key: CryptoKey) { | ||
const iv = data.slice(0, encrypionIvLength); | ||
data = data.slice(encrypionIvLength); | ||
return new Uint8Array(await crypto.subtle.decrypt( | ||
{ name: encryptionAlgorithmName, iv }, | ||
key, | ||
data, | ||
)); | ||
} |
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,14 @@ | ||
import { encryptionAlgorithmName, encrypionIvLength } from './algorithm'; | ||
|
||
export async function encrypt(data: Uint8Array, key: CryptoKey) { | ||
const iv = crypto.getRandomValues(new Uint8Array(encrypionIvLength)); | ||
const encrypted = await crypto.subtle.encrypt( | ||
{ name: encryptionAlgorithmName, iv }, | ||
key, | ||
data, | ||
); | ||
const res = new Uint8Array(iv.byteLength + encrypted.byteLength); | ||
res.set(iv, 0); | ||
res.set(new Uint8Array(encrypted), iv.byteLength); | ||
return res; | ||
} |
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,24 @@ | ||
import { encryptionKeyLength, encryptionAlgorithmName, derivationAlgorithmName, derivationHash, derivationIterations } from './algorithm'; | ||
|
||
export async function createKey(rawRoot: Uint8Array) { | ||
const masterKey = await crypto.subtle.importKey( | ||
'raw', | ||
rawRoot, | ||
derivationAlgorithmName, | ||
false, | ||
['deriveKey'], | ||
); | ||
|
||
return await crypto.subtle.deriveKey({ | ||
name: derivationAlgorithmName, | ||
hash: derivationHash, | ||
iterations: derivationIterations, | ||
salt: rawRoot, | ||
}, masterKey, { | ||
name: encryptionAlgorithmName, | ||
length: encryptionKeyLength, | ||
}, false, [ | ||
'encrypt', | ||
'decrypt', | ||
]); | ||
} |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"moduleResolution": "node", | ||
"baseUrl": "./", | ||
"allowJs": true, | ||
"outDir": "./dist", | ||
"module": "esnext", | ||
"target": "esnext", | ||
"checkJs": true, | ||
"declaration": true, | ||
"emitDeclarationOnly": true, | ||
}, | ||
"include": [ | ||
"src/**/*", | ||
], | ||
"exclude": [ | ||
"node_modules/*", | ||
"doc/*", | ||
"dist/*", | ||
], | ||
} |
Oops, something went wrong.