Skip to content

Commit

Permalink
fix: review and update encryption readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsan-javaid authored and diwakergupta committed Aug 17, 2021
1 parent d9df8f5 commit 79735f4
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion packages/encryption/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,89 @@ Encryption functions used by Stacks.js packages.
## Installation

```
npm install @stacks/cli
npm install @stacks/encryption
```

### Encrypt and decrypt string
```typescript
import { encryptECIES, decryptECIES } from '@stacks/encryption';
import { Buffer } from '@stacks/common';

const privateKey = 'a5c61c6ca7b3e7e55edee68566aeab22e4da26baa285c7bd10e8d2218aa3b229';
const publicKey = '027d28f9951ce46538951e3697c62588a87f1f1f295de4a14fdd4c780fc52cfe69';

const testString = 'all work and no play makes jack a dull boy';

// Encrypt string with public key
const cipherObj = await encryptECIES(publicKey, Buffer.from(testString), true);

// Decrypt the cipher with private key to get the message
const deciphered = await decryptECIES(privateKey, cipherObj)
console.log(deciphered);
```

### Sign content using ECDSA

```typescript
import { signECDSA, verifyECDSA } from '@stacks/encryption';

const privateKey = 'a5c61c6ca7b3e7e55edee68566aeab22e4da26baa285c7bd10e8d2218aa3b229';
const testString = 'all work and no play makes jack a dull boy'

const sigObj = await signECDSA(privateKey, testString)
// Verify content using ECDSA
const result = await verifyECDSA(testString, sigObj.publicKey, sigObj.signature);
console.log(result); // true
```
### EncryptMnemonic and decryptMnemonic

```typescript
import { encryptMnemonic, decryptMnemonic } from '@stacks/encryption';
import { Buffer } from '@stacks/common';

const rawPhrase = 'march eager husband pilot waste rely exclude taste '
+ 'twist donkey actress scene';
const rawPassword = 'rawPassword';
const mockSalt = Buffer.from('ff'.repeat(16), 'hex')

//Encrypt a raw mnemonic phrase to be password protected
const encoded = await encryptMnemonic(rawPhrase, rawPassword, { getRandomBytes: () => mockSalt });

//Decrypt an encrypted mnemonic phrase with a password
const decoded = await decryptMnemonic(encoded.toString('hex'), rawPassword);

console.log(decoded);
```

### Make EC PrivateKey

```typescript
import { makeECPrivateKey, publicKeyToAddress } from '@stacks/encryption';
import { SECP256K1Client } from 'jsontokens';

// makeECPrivateKey
const privateKey = makeECPrivateKey();
const publicKey = SECP256K1Client.derivePublicKey(privateKey);
const address = publicKeyToAddress(publicKey);
console.log(address);
```

### EC pair to hex string

```typescript
import {
ecPairToAddress,
ecPairToHexString,
hexStringToECPair,
} from '@stacks/encryption';


const privateKey = '00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb01';
const expectedAddress = '1WykMawQRnLh7SWmmoRL4qTDNCgAsVRF1';

const computedECPair = hexStringToECPair(privateKey);
const exToHex = ecPairToHexString(computedECPair);
const address = ecPairToAddress(computedECPair);
console.log(exToHex);
console.log(address === expectedAddress); // true
```

1 comment on commit 79735f4

@vercel
Copy link

@vercel vercel bot commented on 79735f4 Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.