Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
555d36d
add an aes256 lib, required for scrypt later
Jun 2, 2021
3f85f54
remove duplicated test
Jun 2, 2021
d74cf26
minor jsdoc update, add ip ref
Jun 2, 2021
a7da375
remove direct native crypto pkg usage, use browserify libs
Jun 8, 2021
0e18502
allow custom iv for aes enc
Jun 8, 2021
e3fe183
remove invalid npm dev dep
Jun 8, 2021
8df8329
change iv arg from buffer to () => buffer
Jun 8, 2021
3c12bc1
few requested changes
Jun 9, 2021
b7f64a7
update comment
Jun 9, 2021
54f9625
add an aes256 lib, required for scrypt later
Jun 2, 2021
bed1564
remove duplicated test
Jun 2, 2021
6646c61
minor jsdoc update, add ip ref
Jun 2, 2021
96bc0fb
add a lib to do store passphrase protected data using scrypt
Jun 2, 2021
1a7a652
some refactoring, and scrypt interface has to be provided by caller, …
Jun 2, 2021
6c67cb3
reverted unwated changed
Jun 3, 2021
abff71a
move dep to sub package
Jun 3, 2021
ebb5bea
rename var
Jun 3, 2021
bf11d28
rename package
Jun 3, 2021
071167b
rename package
Jun 3, 2021
b00fece
some refactor, implement EncryptedMnemoniceNode
Jun 3, 2021
a685419
add secp to jlf-wal-encryted
Jun 4, 2021
d3e52b8
minor rename, some jsdoc
Jun 4, 2021
7f1cf13
done with all wallet-encrypted implementation
Jun 4, 2021
c589408
minor syntax clean up
Jun 4, 2021
a9f0293
allow custom scrypt param for scryptsy implementation, reduce test di…
Jun 4, 2021
18aba36
sort doc alphabetically (did not do so after rename package)
Jun 4, 2021
78eb2ed
add tests
Jun 4, 2021
f2800d8
update some description in md
Jun 4, 2021
3778f61
add dev dep
Jun 4, 2021
69f69b5
add missing dep
Jun 4, 2021
822112a
fix tiny secp version
Jun 4, 2021
58bc8dd
make ScryptStorage require 2 stores to instantiate, store hash for pa…
Jun 4, 2021
55bdbc0
minor comment update
Jun 4, 2021
379246b
put back seedHash in provider
Jun 4, 2021
4595330
add more desc, + minor fix
Jun 4, 2021
7dc7c6b
fix test
Jun 4, 2021
f103863
remove direct native crypto pkg usage, use browserify libs
Jun 8, 2021
5466474
allow custom iv for aes enc
Jun 8, 2021
af7f094
remove invalid npm dev dep
Jun 8, 2021
20cd86e
fix import (dep renamed in jlf crypto)
Jun 8, 2021
0e12e38
change iv arg from buffer to () => buffer
Jun 8, 2021
a6465fb
allow custom iv randomizer logic to for ScryptStorage aes enc
Jun 8, 2021
825e829
update some comments in demo
Jun 8, 2021
e4235bb
reverting unwanted package-lock changes
Jun 8, 2021
bfd2709
few requested changes
Jun 9, 2021
5f49e06
update comment
Jun 9, 2021
7a96183
quick save
Jun 10, 2021
a0b15b4
fix build script command
Jun 10, 2021
5479041
remove import from dist dir
Jun 10, 2021
38399bb
add missing jest cli config
Jun 10, 2021
a515433
enhance test
Jun 11, 2021
bb143e7
start over using updated main
Jun 11, 2021
ddc3f1c
import SIGHASH from jlf-tx
Jun 11, 2021
63a6345
add config for gh actions
Jun 11, 2021
67d8936
add module mapper and build config
Jun 11, 2021
a398c4e
add doc, add new tests to jlf-tx-sig
Jun 11, 2021
f69f435
replace transactionsigner from jlf-tx-sig
Jun 11, 2021
5fed58c
replace all signer logic import from simple jlf-tx
Jun 11, 2021
5d626b9
fix jest config module mapper order
Jun 11, 2021
f63f539
repeat test
Jun 11, 2021
2bef85e
Merge branch 'feature/mnemonic-wallet/passphrase-encrypt-seed' into f…
Jun 11, 2021
0e1969a
remove PR irrelevant code
Jun 11, 2021
bab5aba
Merge branch 'handpick-jellyfish-tx-signature' into feature/mnemonic-…
Jun 11, 2021
b31582d
npm i
Jun 11, 2021
9d8a227
trim down and split PR, to ease review process
Jun 11, 2021
ef75507
Merge branch 'main' into mnemonic-seed-encryption
Jun 15, 2021
f5c66ea
Merge branch 'main' into mnemonic-seed-encryption
Jun 15, 2021
5bd91e9
fix test, add desc
Jun 15, 2021
fcb8393
Merge branch 'main' into mnemonic-seed-encryption
Jul 16, 2021
01278e5
fix invalidtype cast
Jul 19, 2021
9207a6f
remove husky gitigonore (no longer needed)
Jul 19, 2021
60e138d
address some change request
Jul 19, 2021
28a1f89
remove outdate husky file, updated package-lock
Jul 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
/packages/jellyfish-transaction-signature/ @fuxingloh @ivan-zynesis
/packages/jellyfish-wallet/ @fuxingloh @ivan-zynesis @monstrobishi
/packages/jellyfish-wallet-classic/ @fuxingloh @ivan-zynesis @monstrobishi
/packages/jellyfish-wallet-encrypted/ @fuxingloh @ivan-zynesis
/packages/jellyfish-wallet-mnemonic/ @fuxingloh @ivan-zynesis

/packages/testcontainers/ @fuxingloh
/packages/testing/ @fuxingloh @canonbrother

Expand Down
1 change: 1 addition & 0 deletions .github/governance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ issue:
- jellyfish-transaction-builder
- jellyfish-transaction-signature
- jellyfish-wallet
- jellyfish-wallet-encrypted
- jellyfish-wallet-classic
- jellyfish-wallet-mnemonic
- testcontainers
Expand Down
5 changes: 5 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ labels:
matcher:
files: "packages/jellyfish-transaction-signature/**"

- label: area/jellyfish-wallet-encrypted
sync: true
matcher:
files: "packages/jellyfish-wallet-encrypted/**"

- label: area/jellyfish-wallet-classic
sync: true
matcher:
Expand Down
2 changes: 2 additions & 0 deletions .github/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
name: area/jellyfish-wallet
- color: fbca04
name: area/jellyfish-wallet-classic
- color: fbca04
name: area/jellyfish-wallet-encrypted
- color: fbca04
name: area/jellyfish-wallet-mnemonic
- color: fbca04
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Package | Description
`@defichain/jellyfish-transaction-builder` | Provides a high-high level abstraction for constructing transaction ready to be broadcast for DeFi Blockchain.
`@defichain/jellyfish-wallet-classic` | WalletClassic implements a simple, single elliptic pair wallet.
`@defichain/jellyfish-wallet` | Jellyfish wallet is a managed wallet, where account can get discovered from an HD seed.
`@defichain/jellyfish-wallet-encrypted` | Library to encrypt MnemonicHdNode as EncryptedMnemonicHdNode. Able to perform as MnemonicHdNode with passphrase known.
`@defichain/jellyfish-wallet-mnemonic` | MnemonicHdNode implements the WalletHdNode from jellyfish-wallet; a CoinType-agnostic HD Wallet for noncustodial DeFi.
`@defichain/testcontainers` | Provides a lightweight, throw away instances for DeFiD node provisioned automatically in a Docker container.
`@defichain/testing` | Provides rich test fixture setup functions for effective and effortless testing.
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'@defichain/jellyfish-transaction-builder': '<rootDir>/packages/jellyfish-transaction-builder/src',
'@defichain/jellyfish-transaction': '<rootDir>/packages/jellyfish-transaction/src',
'@defichain/jellyfish-wallet-classic': '<rootDir>/packages/jellyfish-wallet-classic/src',
'@defichain/jellyfish-wallet-encrypted': '<rootDir>/packages/jellyfish-wallet-encrypted/src',
'@defichain/jellyfish-wallet-mnemonic': '<rootDir>/packages/jellyfish-wallet-mnemonic/src',
'@defichain/jellyfish-wallet': '<rootDir>/packages/jellyfish-wallet/src',
'@defichain/testcontainers': '<rootDir>/packages/testcontainers/src',
Expand Down
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/jellyfish-wallet-encrypted/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[![npm](https://img.shields.io/npm/v/@defichain/jellyfish-wallet-encrypted)](https://www.npmjs.com/package/@defichain/jellyfish-wallet-encrypted/v/latest)
[![npm@next](https://img.shields.io/npm/v/@defichain/jellyfish-wallet-encrypted/next)](https://www.npmjs.com/package/@defichain/jellyfish-wallet-encrypted/v/next)

# @defichain/jellyfish-wallet-encrypted

This is an extended wallet hd node which has similar interface as @defichain/jellyfish-wallet-mnemonic. The library assists caller to encrypt mnemonic seed into provided storage. See example/demo.ts for more detail.

100 changes: 100 additions & 0 deletions packages/jellyfish-wallet-encrypted/__tests__/scrypt-storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { ScryptStorage, Storage, SimpleScryptsy } from '../src'

class MockStorage implements Storage {
inMemory: string | undefined
async getter (): Promise<string|undefined> {
return this.inMemory
}

async setter (encrypted: string | undefined): Promise<void> {
this.inMemory = encrypted
}
}
// Mock storage
let scryptStorage: ScryptStorage
const encryptedSeedStorage = new MockStorage()
const seedHashStorage = new MockStorage()

const scryptProvider = new SimpleScryptsy()

// 32 bytes
const samplePrivateKey = 'e9873d79c6d87dc0fb6a5778633389f4e93213303da61f20bd67fc233aa33262'

beforeEach(async () => {
scryptStorage = new ScryptStorage(scryptProvider, encryptedSeedStorage, seedHashStorage)
await encryptedSeedStorage.setter(undefined)
await seedHashStorage.setter(undefined)
})

it('Should be able to encrypt / decrypt', async () => {
const pass = 'password'

const privateKey = Buffer.from(samplePrivateKey, 'hex')
await scryptStorage.encrypt(privateKey, pass)

// encrypted data stored
expect(await encryptedSeedStorage.getter()).not.toStrictEqual(null)

const decrypted = await scryptStorage.decrypt(pass)
expect(decrypted?.toString('hex')).toStrictEqual(samplePrivateKey)
})

it('Should be able to encrypt / decrypt - simple passphrase', async () => {
// let say a 6 digit pin
const pass = '135790'

const privateKey = Buffer.from(samplePrivateKey, 'hex')
await scryptStorage.encrypt(privateKey, pass)

const decrypted = await scryptStorage.decrypt(pass)
expect(decrypted?.toString('hex')).toStrictEqual(samplePrivateKey)
})

it('decrypt() - should return null when no data in storage', async () => {
// let say a 6 digit pin
const pass = 'password'

const decrypted = await scryptStorage.decrypt(pass)
expect(decrypted).toStrictEqual(null)
})

it('Should work with variable data length - long', async () => {
const longData = samplePrivateKey + samplePrivateKey + samplePrivateKey + samplePrivateKey
const pass = 'password'

const data = Buffer.from(longData, 'hex')
await scryptStorage.encrypt(data, pass)

const decrypted = await scryptStorage.decrypt(pass)
expect(decrypted?.toString('hex')).toStrictEqual(longData)
})

it('Should work with variable data length - short', async () => {
const shortData = 'ffaa'
const pass = 'password'

const data = Buffer.from(shortData, 'hex')
await scryptStorage.encrypt(data, pass)

const decrypted = await scryptStorage.decrypt(pass)
expect(decrypted?.toString('hex')).toStrictEqual(shortData)
})

it('Should reject "odd" number long data, only accept "even" number long data', async () => {
const pass = 'password'

const fourBytes = 'eeffaabb'
await scryptStorage.encrypt(Buffer.from(fourBytes, 'hex'), pass)

const threeBytes = 'ffaabb'
const data = Buffer.from(threeBytes, 'hex')
await expect(async () => {
await scryptStorage.encrypt(data, pass)
}).rejects.toThrow('Data length must be even number')

const padded = Buffer.from('00' + threeBytes, 'hex')
await scryptStorage.encrypt(padded, pass)

const decrypted = await scryptStorage.decrypt(pass)
expect(decrypted?.toString('hex')).toStrictEqual(padded.toString('hex'))
})
38 changes: 38 additions & 0 deletions packages/jellyfish-wallet-encrypted/__tests__/scryptsy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { SimpleScryptsy } from '../src'

describe('passphraseToKey()', () => {
it('Should be able to generate from random (same) passphrase into multiple desired length secret', () => {
const scryptsy = new SimpleScryptsy()
const secret1 = scryptsy.passphraseToKey('random password', Buffer.from('abcd', 'hex'), 64)
expect(secret1.length).toStrictEqual(64)

const secret2 = scryptsy.passphraseToKey('random password', Buffer.from('abcd', 'hex'), 40)
expect(secret2.length).toStrictEqual(40)
})

it('Configurable params (difficulty)', () => {
const easy = new SimpleScryptsy({
N: 16384, // 2 ^ 14
r: 8,
p: 1
})

const hard = new SimpleScryptsy({
N: 16384, // 2 ^ 14
r: 8,
p: 8
})

const easyStart = Date.now()
easy.passphraseToKey('random password', Buffer.from('abcd', 'hex'), 32)
const easyTime = Date.now() - easyStart

const hardStart = Date.now()
hard.passphraseToKey('random password', Buffer.from('abcd', 'hex'), 32)
const hardTime = Date.now() - hardStart

// significantly slower
// technically it is 8x harder, but they can be processed in parallel
expect(hardTime).toBeGreaterThan(easyTime * 2)
})
})
12 changes: 12 additions & 0 deletions packages/jellyfish-wallet-encrypted/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
testEnvironment: 'node',
testMatch: [
'**/__tests__/**/*.test.ts'
],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
clearMocks: true,
testTimeout: 120000
}
45 changes: 45 additions & 0 deletions packages/jellyfish-wallet-encrypted/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"private": false,
"name": "@defichain/jellyfish-wallet-encrypted",
"version": "0.0.0",
"description": "A collection of TypeScript + JavaScript tools and libraries for DeFi Blockchain developers to build decentralized finance on Bitcoin",
"keywords": [
"DeFiChain",
"DeFi",
"Blockchain",
"API",
"Bitcoin"
],
"repository": "DeFiCh/jellyfish",
"bugs": "https://github.com/DeFiCh/jellyfish/issues",
"license": "MIT",
"contributors": [
{
"name": "DeFiChain Foundation",
"email": "engineering@defichain.com",
"url": "https://defichain.com/"
},
{
"name": "DeFi Blockchain Contributors"
},
{
"name": "DeFi Jellyfish Contributors"
}
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsc -b ./tsconfig.build.json"
},
"dependencies": {
"@defichain/jellyfish-crypto": "0.0.0",
"@defichain/jellyfish-wallet": "0.0.0",
"scryptsy": "^2.1.0"
},
"devDependencies": {
"@types/scryptsy": "^2.0.0"
}
}
2 changes: 2 additions & 0 deletions packages/jellyfish-wallet-encrypted/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './scryptsy'
export * from './scrypt_storage'
Loading