ton-crypto
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -6,9 +6,11 @@ export { sha256, sha256_sync } from './primitives/sha256'; | ||
export { getSecureRandomBytes, getSecureRandomWords, getSecureRandomNumber } from './primitives/getSecureRandom'; | ||
export { mnemonicNew, mnemonicValidate, mnemonicToPrivateKey, mnemonicToWalletKey, mnemonicToSeed } from './mnemonic/mnemonic'; | ||
export { wordlist as mnemonicWordList } from './mnemonic/wordlist'; | ||
export { KeyPair, sealBox, openBox, keyPairFromSeed } from './primitives/nacl'; | ||
export { keyPairFromSecretKey, sign, signVerify } from './primitives/nacl'; | ||
export { newSecureWords } from './passwords/newSecureWords'; | ||
export { newSecurePassphrase } from './passwords/newSecurePassphrase'; | ||
export { KeyPair } from './primitives/nacl'; | ||
export { mnemonicNew, mnemonicValidate, mnemonicToPrivateKey, mnemonicToWalletKey, mnemonicToSeed, mnemonicToHDSeed } from './mnemonic/mnemonic'; | ||
export { wordlist as mnemonicWordList } from './mnemonic/wordlist'; | ||
export { sealBox, openBox } from './primitives/nacl'; | ||
export { keyPairFromSeed, keyPairFromSecretKey, sign, signVerify } from './primitives/nacl'; | ||
export { HDKeysState } from './hd/state'; | ||
export { getED25519MasterKeyFromSeed, deriveED25519HardenedKey, deriveEd25519Path } from './hd/ed25519'; | ||
export { getSymmetricMasterKeyFromSeed, deriveSymmetricHardenedKey, deriveSymmetricPath } from './hd/symmetric'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.deriveEd25519Path = exports.deriveED25519HardenedKey = exports.getED25519MasterKeyFromSeed = exports.newSecurePassphrase = exports.newSecureWords = exports.signVerify = exports.sign = exports.keyPairFromSecretKey = exports.keyPairFromSeed = exports.openBox = exports.sealBox = exports.mnemonicWordList = exports.mnemonicToSeed = exports.mnemonicToWalletKey = exports.mnemonicToPrivateKey = exports.mnemonicValidate = exports.mnemonicNew = exports.getSecureRandomNumber = exports.getSecureRandomWords = exports.getSecureRandomBytes = exports.hmac_sha512 = exports.pbkdf2_sha512 = exports.sha512_sync = exports.sha512 = exports.sha256_sync = exports.sha256 = void 0; | ||
exports.deriveSymmetricPath = exports.deriveSymmetricHardenedKey = exports.getSymmetricMasterKeyFromSeed = exports.deriveEd25519Path = exports.deriveED25519HardenedKey = exports.getED25519MasterKeyFromSeed = exports.signVerify = exports.sign = exports.keyPairFromSecretKey = exports.keyPairFromSeed = exports.openBox = exports.sealBox = exports.mnemonicWordList = exports.mnemonicToHDSeed = exports.mnemonicToSeed = exports.mnemonicToWalletKey = exports.mnemonicToPrivateKey = exports.mnemonicValidate = exports.mnemonicNew = exports.newSecurePassphrase = exports.newSecureWords = exports.getSecureRandomNumber = exports.getSecureRandomWords = exports.getSecureRandomBytes = exports.hmac_sha512 = exports.pbkdf2_sha512 = exports.sha512_sync = exports.sha512 = exports.sha256_sync = exports.sha256 = void 0; | ||
var sha256_1 = require("./primitives/sha256"); | ||
@@ -18,2 +18,6 @@ Object.defineProperty(exports, "sha256", { enumerable: true, get: function () { return sha256_1.sha256; } }); | ||
Object.defineProperty(exports, "getSecureRandomNumber", { enumerable: true, get: function () { return getSecureRandom_1.getSecureRandomNumber; } }); | ||
var newSecureWords_1 = require("./passwords/newSecureWords"); | ||
Object.defineProperty(exports, "newSecureWords", { enumerable: true, get: function () { return newSecureWords_1.newSecureWords; } }); | ||
var newSecurePassphrase_1 = require("./passwords/newSecurePassphrase"); | ||
Object.defineProperty(exports, "newSecurePassphrase", { enumerable: true, get: function () { return newSecurePassphrase_1.newSecurePassphrase; } }); | ||
var mnemonic_1 = require("./mnemonic/mnemonic"); | ||
@@ -25,2 +29,3 @@ Object.defineProperty(exports, "mnemonicNew", { enumerable: true, get: function () { return mnemonic_1.mnemonicNew; } }); | ||
Object.defineProperty(exports, "mnemonicToSeed", { enumerable: true, get: function () { return mnemonic_1.mnemonicToSeed; } }); | ||
Object.defineProperty(exports, "mnemonicToHDSeed", { enumerable: true, get: function () { return mnemonic_1.mnemonicToHDSeed; } }); | ||
var wordlist_1 = require("./mnemonic/wordlist"); | ||
@@ -31,11 +36,7 @@ Object.defineProperty(exports, "mnemonicWordList", { enumerable: true, get: function () { return wordlist_1.wordlist; } }); | ||
Object.defineProperty(exports, "openBox", { enumerable: true, get: function () { return nacl_1.openBox; } }); | ||
Object.defineProperty(exports, "keyPairFromSeed", { enumerable: true, get: function () { return nacl_1.keyPairFromSeed; } }); | ||
var nacl_2 = require("./primitives/nacl"); | ||
Object.defineProperty(exports, "keyPairFromSeed", { enumerable: true, get: function () { return nacl_2.keyPairFromSeed; } }); | ||
Object.defineProperty(exports, "keyPairFromSecretKey", { enumerable: true, get: function () { return nacl_2.keyPairFromSecretKey; } }); | ||
Object.defineProperty(exports, "sign", { enumerable: true, get: function () { return nacl_2.sign; } }); | ||
Object.defineProperty(exports, "signVerify", { enumerable: true, get: function () { return nacl_2.signVerify; } }); | ||
var newSecureWords_1 = require("./passwords/newSecureWords"); | ||
Object.defineProperty(exports, "newSecureWords", { enumerable: true, get: function () { return newSecureWords_1.newSecureWords; } }); | ||
var newSecurePassphrase_1 = require("./passwords/newSecurePassphrase"); | ||
Object.defineProperty(exports, "newSecurePassphrase", { enumerable: true, get: function () { return newSecurePassphrase_1.newSecurePassphrase; } }); | ||
var ed25519_1 = require("./hd/ed25519"); | ||
@@ -45,1 +46,5 @@ Object.defineProperty(exports, "getED25519MasterKeyFromSeed", { enumerable: true, get: function () { return ed25519_1.getED25519MasterKeyFromSeed; } }); | ||
Object.defineProperty(exports, "deriveEd25519Path", { enumerable: true, get: function () { return ed25519_1.deriveEd25519Path; } }); | ||
var symmetric_1 = require("./hd/symmetric"); | ||
Object.defineProperty(exports, "getSymmetricMasterKeyFromSeed", { enumerable: true, get: function () { return symmetric_1.getSymmetricMasterKeyFromSeed; } }); | ||
Object.defineProperty(exports, "deriveSymmetricHardenedKey", { enumerable: true, get: function () { return symmetric_1.deriveSymmetricHardenedKey; } }); | ||
Object.defineProperty(exports, "deriveSymmetricPath", { enumerable: true, get: function () { return symmetric_1.deriveSymmetricPath; } }); |
@@ -20,2 +20,9 @@ /// <reference types="node" /> | ||
/** | ||
* Convert mnemonics to HD seed | ||
* @param mnemonicArray mnemonic array | ||
* @param password mnemonic password | ||
* @returns 64 byte seed | ||
*/ | ||
export declare function mnemonicToHDSeed(mnemonicArray: string[], password?: string | null | undefined): Promise<Buffer>; | ||
/** | ||
* Validate Mnemonic | ||
@@ -22,0 +29,0 @@ * @param mnemonicArray mnemonic array |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mnemonicNew = exports.mnemonicValidate = exports.mnemonicToWalletKey = exports.mnemonicToPrivateKey = exports.mnemonicToSeed = exports.mnemonicToEntropy = void 0; | ||
exports.mnemonicNew = exports.mnemonicValidate = exports.mnemonicToHDSeed = exports.mnemonicToWalletKey = exports.mnemonicToPrivateKey = exports.mnemonicToSeed = exports.mnemonicToEntropy = void 0; | ||
const tweetnacl_1 = __importDefault(require("tweetnacl")); | ||
@@ -100,2 +100,13 @@ const getSecureRandom_1 = require("../primitives/getSecureRandom"); | ||
/** | ||
* Convert mnemonics to HD seed | ||
* @param mnemonicArray mnemonic array | ||
* @param password mnemonic password | ||
* @returns 64 byte seed | ||
*/ | ||
async function mnemonicToHDSeed(mnemonicArray, password) { | ||
mnemonicArray = normalizeMnemonic(mnemonicArray); | ||
return (await mnemonicToSeed(mnemonicArray, 'TON HD Keys seed', password)); | ||
} | ||
exports.mnemonicToHDSeed = mnemonicToHDSeed; | ||
/** | ||
* Validate Mnemonic | ||
@@ -102,0 +113,0 @@ * @param mnemonicArray mnemonic array |
{ | ||
"name": "ton-crypto", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"repository": "https://github.com/ex3ndr/ton-crypto.git", | ||
@@ -5,0 +5,0 @@ "author": "Steve Korshakov <steve@korshakov.com>", |
141
README.md
@@ -9,3 +9,4 @@ # TON crypto | ||
- 🦺 Crypto primitives: SHA-256, SHA-512, PBKDF2-SHA-256, HMAC-SHA-512 | ||
- 🦺 All required crypto for TON and apps | ||
- 💪 Works everywhere: Browser, NodeJS and React Native | ||
- 🚀 Promise-based API | ||
@@ -15,2 +16,13 @@ - 🏎 Built on top of Buffer (polifil required in browsers) | ||
## Imlemented | ||
- SHA-256 | ||
- SHA-512 | ||
- HMAC-SHA-512 | ||
- PBKDF2-SHA-512 | ||
- TON Mnemonics | ||
- NaCL sign/verify for signing (used by TON blockchain) | ||
- NaCL sealBox/openBox for symmetric encryption | ||
- SLIP-10 Ed25519 HD Keys (for hierarchical keys) | ||
## Install | ||
@@ -35,15 +47,130 @@ | ||
## Hashing | ||
## Secure random | ||
```js | ||
import { getSecureRandomBytes, getSecureRandomWords, getSecureRandomNumber, newSecureWords, newSecurePassphrase } from 'ton-crypto'; | ||
const randomBytes: Buffer = await getSecureRandomBytes(32); // 32 random bytes | ||
const randomWords: Uint16Array = await getSecureRandomBytes(32); // 64 random bytes packaed into words | ||
const randomNumber: number = await getSecureRandomNumber(0, 10); // Random number from 0 (inclusive) to 10 (exclusive) | ||
const secureWords: string[] = await newSecureWords(12); // Random 12 words from BIP-13 wordlist | ||
const securePassptrase: string = await newSecurePassphrase(10); // Random 10 words that connected by dashes to make it look like password | ||
``` | ||
## SHA-256 and SHA-512 | ||
All methods accept strings or Buffers as arguments. | ||
```js | ||
import { sha256, sha512, pbkdf2_sha512, hmac_sha512 } from 'ton-crypto'; | ||
const hash1 = await sha256('hello-world'); | ||
const hash2 = await sha512(Buffer.from('hello-world', 'utf-8')); | ||
const key = await pbkdf2_sha512('password', 'salt', 10000, 64); | ||
const hmac = await hmac_sha512('hmac-key', 'data'); | ||
import { sha256, sha512, pbkdf2_sha512, hmac_sha512, sha256_sync, sha512_sync } from 'ton-crypto'; | ||
// Async methods | ||
const hash1: Buffer = await sha256('hello-world'); | ||
const hash2: Buffer = await sha512(Buffer.from('hello-world', 'utf-8')); | ||
// Sync methods | ||
const hash1Sync: Buffer = await sha256_sync('hello-world'); | ||
const hash2Sync: Buffer = await sha512_sync('hello-world'); | ||
``` | ||
## HMAC-SHA-512 | ||
```js | ||
const hmac: Buffer = await hmac_sha512('hmac-key', 'data'); | ||
``` | ||
## PBKDF2-SHA-512 | ||
```js | ||
const key: Buffer = await pbkdf2_sha512('password', 'salt', 10000, 64); | ||
``` | ||
## TON mnemonics | ||
TON uses BIP-39 styled english mnemonics with custom key deriviation and built-in checksums. | ||
```js | ||
import { mnemonicNew, mnemonicValidate, mnemonicToPrivateKey, mnemonicToWalletKey, mnemonicToSeed, mnemonicWordList, KeyPair, mnemonicToHDSeed } from 'ton-crypto'; | ||
const password: string | null | undefined = null; // Optional password | ||
const mnemonics: string[] = await mnemonicNew(24, password); // Generate new menemonics | ||
const mnemonicsValid: boolean = await mnemonicValidate(mnemonics, password); // Validate mnemonics | ||
const keypair1: KeyPair = await mnemonicToPrivateKey(mnemonics, password); // Generates KeyPair from mnemonics | ||
const keypair2: KeyPair = await mnemonicToWalletKey(mnemonics, password); // Generates KeyPair from mnemonics (results are SEEMS TO BE same as above) | ||
const mnemonicsSeed: Buffer = await mnemonicToSeed(mnemonics, 'Seed text', password); // Generates 64 bytes of seed from mnemonics and seed text. | ||
const mnemonicHDSeed: Buffer = await mnemonicToHDSeed(mnemonics, password); // Generates 64 bytes of seed for HD Keys | ||
const wordlist = mnemonicWordList; // BIP39 word list | ||
``` | ||
## NaCL-compatible Ed25519 signing | ||
Ed25519 is used by TON in contracts to check signatures. | ||
```js | ||
import { keyPairFromSeed, keyPairFromSecretKey, sign, signVerify, KeyPair } from 'ton-crypto'; | ||
const data = Buffer.from('Hello wordl!'); | ||
// Create Keypair | ||
const seed: Buffer = await getSecureRandomBytes(32); // Seed is always 32 bytes | ||
const keypair: KeyPair = keyPairFromSeed(seed); // Creates keypair from random seed | ||
const keypair2: KeyPair = keyPairFromSecretKey(keypair.secret); // Creates keypair from secret key | ||
// Sign | ||
const signature = sign(data, keypair.secret); // Creates signature for arbitrary data (it is recommended to get hash from data first) | ||
// Check | ||
const valid: boolean = signVerify(data, signature, keypair.public); | ||
``` | ||
## NaCL-compatible symmetrict encryption | ||
```js | ||
import { sealBox, openBox, getSecureRandomBytes } from 'ton-crypto'; | ||
const data = Buffer.from('Hello wordl!'); | ||
// Encryption | ||
const key: Buffer = await getSecureRandomBytes(32); // Key is always 32 bytes and secret | ||
const nonce: Buffer = await getSecureRandomBytes(24); // Nonce is always 24 bytes and public | ||
const sealed: Buffer = sealBox(data, nonce, key); // Sealed box | ||
// Decryption | ||
const decrypted: Buffer | null = openBox(sealed, nonce, key); // Decrypted with integrity check. null if failed. | ||
``` | ||
## SLIP-10 Ed25519 HD Keys | ||
Generates SLIP-10 compatible hierarchy of keys | ||
```js | ||
import { newMnemonics, mnemonicToHDSeed, deriveEd25519Path, KeyPair } from 'ton-crypto'; | ||
// Generate HD seed | ||
// You can just generate 64-128 random bytes, but this way you will be able to | ||
// create it from mnemonics that you already have for a wallet | ||
const mnemonics: string[] = await newMnemonics(); | ||
const seed: Buffer = await mnemonicToHDSeed(mnemonics); | ||
// Derive secret key from path m/0'/1'/2'/3' | ||
const derivedSeed: Buffer = await deriveEd25519Path(seed, [0, 1, 2, 3]); | ||
// Create key pair | ||
const keyPair: KeyPair = keyPairFromSeed(derivedSeed); | ||
``` | ||
## SLIP-21 Symmetric HD Keys | ||
Generates SLIP-21 compatible hierarchy of keys for symmetric encryption. | ||
```js | ||
import { newMnemonics, mnemonicToHDSeed, deriveSymmetricPath, KeyPair } from 'ton-crypto'; | ||
// Generate HD seed | ||
// You can just generate 64-128 random bytes, but this way you will be able to | ||
// create it from mnemonics that you already have for a wallet | ||
const mnemonics: string[] = await newMnemonics(); | ||
const seed: Buffer = await mnemonicToHDSeed(mnemonics); | ||
// Derive secret key from path m/0'/1'/2'/3' | ||
const derivedKey: Buffer = await deriveSymmetricPath(seed, ['ton-seed', 'some-key', 'some-key2']); | ||
``` | ||
# License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
193386
51
8885
174