UNPKG

5.62 kBMarkdownView Raw
1# cardano-crypto.js
2* [input-output-hk/cardano-crypto](https://github.com/input-output-hk/cardano-crypto/tree/master/cbits)
3* [haskell-crypto/cryptonite](https://github.com/haskell-crypto/cryptonite)
4* [grigorig/chachapoly](https://github.com/grigorig/chachapoly)
5
6Compiled to pure javascript using Emscripten. This is a collection of cryptolibraries and functions useful for working with Cardano cryptocurrency, eliminating the need for many dependencies.
7
8# Examples
9## Signing
10
11``` javascript
12var lib = require('cardano-crypto.js')
13
14var mnemonic = 'logic easily waste eager injury oval sentence wine bomb embrace gossip supreme'
15var walletSecret = await lib.mnemonicToRootKeypair(mnemonic, 1)
16var msg = new Buffer('hello there')
17var sig = lib.sign(msg, walletSecret)
18```
19
20## Deriving child keys (hardened derivation, you can choose either derivation scheme 1 or 2)
21
22``` javascript
23var lib = require('cardano-crypto.js')
24
25var mnemonic = 'logic easily waste eager injury oval sentence wine bomb embrace gossip supreme'
26var parentWalletSecret = lib.mnemonicToRootKeypair(mnemonic, 1)
27var childWalletSecret = lib.derivePrivate(parentWalletSecret, 0x80000001, 1)
28```
29
30## Deriving child public keys (nonhardened derivation, you can choose either derivation scheme 1 or 2)
31
32``` javascript
33var lib = require('cardano-crypto.js')
34
35var mnemonic = 'logic easily waste eager injury oval sentence wine bomb embrace gossip supreme'
36var parentWalletSecret = lib.mnemonicToRootKeypair(mnemonic, 1)
37var parentWalletPublicKey = parentWalletSecret.slice(64, 128)
38var childWalletSecret = lib.derivePublic(parentWalletPublicKey, 1, 1)
39```
40
41# Available Functions
42
43## Signing/Verification
44* `Buffer sign(Buffer msg, Buffer walletSecret)`
45* `Bool verify(Buffer msg, Buffer publicKey, Buffer sig)`
46
47## Key derivation
48* `async Buffer mnemonicToRootKeypair(String mnemonic, int derivationScheme)`
49* `Buffer derivePrivate(Buffer parentKey, int index, int derivationScheme)`
50* `Buffer derivePublic(Buffer parentExtPubKey, int index, int derivationScheme)`
51* `Buffer toPublic(Buffer privateKey)`
52
53## Address encoding/decoding/validation
54* `Buffer packBootstrapAddress(Array[int] derivationPath, Buffer xpub, Buffer hdPassphrase, int derivationScheme, int protocolMagic)`
55* `Buffer packBaseAddress(Buffer spendingKeyHash, Buffer stakingPubKey, int networkId)`
56* `Buffer packPointerAddress(Buffer pubKeyHash, Object pointer, int networkId)`
57* `Buffer packEnterpriseAddress(Buffer spendingKeyHash, int networkId)`
58* `Buffer packRewardAddress(Buffer stakingKeyHash, int networkId)`
59* `Object getAddressType(Buffer address)`
60* `Object getShelleyAddressInfo(Buffer address)`
61* `Object AddressTypes`
62* `Buffer addressToBuffer(string address) // address can be either bech32 or base58 encoded`
63* `Map getBootstrapAddressAttributes(Buffer address)`
64* `Array<int>? getBootstrapAddressDerivationPath(Buffer address, Buffer hdPassphrase)`
65* `int getBootstrapAddressProtocolMagic(Buffer address)`
66* `Bool isValidBootstrapAddress(string address)`
67* `Bool isValidShelleyAddress(string address)`
68* `Buffer xpubToHdPassphrase(Buffer xpub)`
69* `Buffer getPubKeyBlake2b224Hash(Buffer pubKey)`
70
71## Cardano crypto primitives
72* `Buffer blake2b(Buffer input, outputLen)`
73* `Buffer cardanoMemoryCombine(Buffer input, String password)`
74* `string bech32.encode(string prefix, Buffer data)`
75* `Object bech32.decode(string address)`
76* `[base58](https://www.npmjs.com/package/base58)`
77* `[scrypt](https://www.npmjs.com/package/scrypt-async)`
78
79## Daedalus paper wallets (27-word mnemonics)
80* `Buffer decodePaperWalletMnemonic(string paperWalletMnemonic)`
81
82We encourage you to take a look `at test/index.js` to see how the functions above should be used.
83
84# Development
85
86* Install [emscripten](http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html#installation-instructions), recommended version is 1.38.8
87* run `npm install`
88* run `npm run build`
89
90# Emscripten build example
91
92```
93git clone https://github.com/emscripten-core/emsdk.git
94cd emsdk
95./emsdk install 1.39.19
96./emsdk activate 1.39.19
97source ./emsdk_env.sh
98cd ../
99git clone https://github.com/vacuumlabs/cardano-crypto.js
100cd cardano-crypto.js
101npm install
102npm run build
103shasum lib.js # should match shasum of published version of lib.js
104```
105
106# known issues
107
108When trying to compile the library with emscripten 1.38.41, the `cardanoMemoryCombine` function slows down significantly. With the 1.38.8 version it runs significantly faster.
109
110# tests
111
112* run `npm run test`
113
114# Removing wordlists from webpack/browserify
115
116* [bitcoinjs/bip39](https://github.com/bitcoinjs/bip39)
117
118Browserify/Webpack bundles can get very large if you include all the wordlists, so you can now exclude wordlists to make your bundle lighter.
119
120For example, if we want to exclude all wordlists besides chinese_simplified, you could build using the browserify command below.
121
122 ```bash
123$ browserify -r bip39 -s bip39 \
124 --exclude=./wordlists/english.json \
125 --exclude=./wordlists/japanese.json \
126 --exclude=./wordlists/spanish.json \
127 --exclude=./wordlists/italian.json \
128 --exclude=./wordlists/french.json \
129 --exclude=./wordlists/korean.json \
130 --exclude=./wordlists/chinese_traditional.json \
131 > bip39.browser.js
132```
133
134 This will create a bundle that only contains the chinese_simplified wordlist, and it will be the default wordlist for all calls without explicit wordlists.
135
136 You can also do this in Webpack using the `IgnorePlugin`. Here is an example of excluding all non-English wordlists
137
138 ```javascript
139 ...
140 plugins: [
141 new webpack.IgnorePlugin(/^\.\/wordlists\/(?!english)/, /bip39\/src$/),
142 ],
143 ...
144 ```
145
\No newline at end of file