Dashmachine crypto utility library
Cryptographic helper functions for use in Dash Platform Web Dapp Sample Messaging.
Note: this is an experimental library to support the web dapp sample investigation of Dash Platform usage - not for production use.
Breaking Changes From v1.0.0 DashJS is required as an external dependency in both browser and node.js versions
Prerequisites
To utilise the encryption & decryption by username The library requires DashJS as an external dependency, so this must be referenced BEFORE the Dashmachine Crypto library. In Node.js:
const Dash = require('dash');
const DashmachineCrypto = require('dashmachine-crypto');
In the browser:
<script src="https://unpkg.com/dash"></script>
<script src="dashmachine-crypto-lib.js" type="text/javascript"></script>
Please see the examples below for sample usage
Browser usage
Include the dashmachine-crypto-lib.js
script file available from the releases page.
Nodejs usage
npm i dashmachine-crypto
Documentation
Table of Contents
DashmachineCrypto
DashmachineCrypto performs ECIES encryption & decryption and Double SHA256 Hashing. Note the class contains only static methods so you do not have to call the contructor, i.e. use DashmachineCrypto.encrypt, not new DashmachineCrypto()
Examples
<!-- Usage in HTML file --><script src="https://unpkg.com/dash"></script><script src="dashmachine-crypto-lib.js" type="text/javascript"></script><script>const vendorPrivateKey = '40148175614f062fb0b4e5c519be7b6f57b872ebb55ea719376322fd12547bff'const message = 'hello';const userPublicKey = 'A7GGInyvn7ExXkSVg+OFhbhVjEMhIFv0oyeJl03gFDRo'const userPrivateKey = '219c8a8f9376750cee9f06e0409718f2a1b88df4acc61bf9ed9cf252c8602768'const vendorPublicKey = 'A0/qSE6tis4l6BtQlTXB2PHW+WV+Iy0rpF5hAvX8hDRz'console;const encrypted = DashmachineCrypto;consoledirencrypteddata;console;const decrypted = DashmachineCrypto;consoledirdecrypted;console;const digest = DashmachineCryptohashmessage;consoledirdigestdata;console;const verifies = DashmachineCrypto;consoledirverifiessuccessconst entropy = DashmachineCrypto;console; const senderName = 'alice';const senderMnemonic = 'uniform analyst paper father soldier toe lesson fetch exhaust jazz swim response';const recipientName = 'bob';const recipientMnemonic = 'liar fee island situate deal exotic flat direct save bag fiscal news';const userMessage = `Hello !`;const dpnsContractId = '295xRRRMGYyAruG39XdAibaU9jMAzxhknkkAxFE7uVkW' {try consoleconst encrypted = await DashmachineCrypto;console;const decrypted = await DashmachineCryptoconsole;catch e console; } async { await }</script>
//use in nodejsconst Dash = ;const DashmachineCrypto = const vendorPrivateKey = '40148175614f062fb0b4e5c519be7b6f57b872ebb55ea719376322fd12547bff'const message = 'hello';const userPublicKey = 'A7GGInyvn7ExXkSVg+OFhbhVjEMhIFv0oyeJl03gFDRo'const userPrivateKey = '219c8a8f9376750cee9f06e0409718f2a1b88df4acc61bf9ed9cf252c8602768'const vendorPublicKey = 'A0/qSE6tis4l6BtQlTXB2PHW+WV+Iy0rpF5hAvX8hDRz'console;const encrypted = DashmachineCrypto;consoledirencrypteddata;console;const decrypted = DashmachineCrypto;consoledirdecrypted;console;const entropy = DashmachineCrypto;console; const senderName = 'alice';const senderMnemonic = 'uniform analyst paper father soldier toe lesson fetch exhaust jazz swim response';const recipientName = 'bob';const recipientMnemonic = 'liar fee island situate deal exotic flat direct save bag fiscal news';const userMessage = `Hello !`;const dpnsContractId = '295xRRRMGYyAruG39XdAibaU9jMAzxhknkkAxFE7uVkW' {try consoleconst encrypted = await DashmachineCrypto;console;const decrypted = await DashmachineCryptoconsole;catch e console; } async { await }
encrypt
Parameters
senderPrivateKey
string The base64 repesentation of the HD private key of the Dash User sending the message, the result of calling client.account.getIdentityHDKey(0, 'user').privateKey where client is an instance of Dash.Clientmessage
string message to encryptrecipientPublicKey
object The base64 repesentation of the public key for the Identity of the Dash User receiveing the message
Returns Object Either {success: true, data: [encrypted message]} or {error: true, message: [error message]}
decrypt
Parameters
recipientPrivateKey
string The base64 repesentation of the HD private key of the Dash User receiving the message, the result of calling client.account.getIdentityHDKey(0, 'user').privateKey where client is an instance of Dash.ClientencryptedMessage
string message to decrypt as a hex representation of the stringified JSON of the encryption result buffersenderPublicKey
object The base64 repesentation of the public key for the Identity of the Dash User sending the message
Returns Object Either {success: true, data: [decrypted message]} or {error: true, message: [error message]}
hash
Parameters
message
string full message to be hashed
Returns Object Either {success: true, data: [digest]} or {error: true, message: [error message]}
verify
Parameters
generateEntropy
Returns Object Either {success: true, data: [generated entropy]} or {error: true, message: [error message]}
encryptForUsername
Parameters
message
string The message to encryptsenderName
string DPNS username of senderrecipientName
string DPNS username of recipientsenderMnemonic
string Account mnemonic of senderdpnsContractId
string contractId for DPNS contract
Returns Object {success: true, data: encryptedMessage} | {Error}
decryptForUsername
Parameters
message
string The message to decryptrecipientName
string DPNS username of recipientsenderName
string DPNS username of senderrecipientMnemonic
string Account mnemonic of recipientdpnsContractId
string contractId for DPNS contract
Returns Object {success: true, data: encryptedMessage} or {Error}
License
Development
To develop this library:
-
clone the repository and change to project directory
git clone https://github.com/dashmachine/dashmachine-crypto.git && cd dashmachine-crypto
The source file is src/crypto.service.js
-
build output
npm run build
-
test with webpack dev server
npm start
-
update documentation (requires npm documentation package installed globally:
npm i -g documentation
)
Update the Documentation section of the README.md file
npm run docs:readme