@stardust-collective/dag-keystore
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -5,2 +5,4 @@ # Changelog | ||
### [1.0.5](https://github.com/StardustCollective/dag-keystore/compare/v1.0.4...v1.0.5) (2020-10-19) | ||
### [1.0.4](https://github.com/StardustCollective/dag-keystore/compare/v1.0.3...v1.0.4) (2020-10-15) | ||
@@ -7,0 +9,0 @@ |
@@ -20,4 +20,24 @@ import { asn1, pkcs12, pki } from "node-forge"; | ||
} | ||
writeP12(crt, sk) { | ||
// var crt = fs.readFileSync ('jiji.cert.pem'); | ||
// var crt_forge = pki.certificateFromPem(CERT_PEM); | ||
// // var sk = fs.readFileSync ('jiji.key.pem'); | ||
// var sk_forge = pki.privateKeyFromPem(PRIV_PEM); | ||
// var encryptedPrivateKeyInfo = pki.encryptedPrivateKeyFromPem(PRIV_PEM); | ||
// var privateKeyInfo = pki.decryptPrivateKeyInfo(encryptedPrivateKeyInfo, 'test1'); | ||
// get the ASN.1 representation of a PKCS#12 archive | ||
//var p12Asn1 = pkcs12.toPkcs12Asn1(sk_forge, [crt_forge], 'test1', {algorithm: '3des'}); | ||
// encode the ASN.1 representation using DER | ||
// const hex = asn1.toDer(privateKeyInfo).toHex(); | ||
// | ||
// const keyMinusPrefix = hex.substring(this.privateKeyHexPrefixLength, hex.length); | ||
// const keyParts = keyMinusPrefix.split(this.secp256k1StringIdentifier); | ||
// | ||
// const privateKey = keyParts[0]; | ||
// const publicKey = keyParts[1]; | ||
// | ||
// return { privateKey, publicKey} ; | ||
} | ||
} | ||
export const keyStoreFile = new KeyStoreFile(); | ||
//# sourceMappingURL=key-store-file.js.map |
@@ -7,5 +7,6 @@ import { sha256 as jsSha256 } from "js-sha256"; | ||
import * as EC from "elliptic"; | ||
import { bip32 } from './bip32/bip32'; | ||
import { bip39 } from './bip39/bip39'; | ||
const curve = new EC.ec("secp256k1"); | ||
import Wallet from 'ethereumjs-wallet'; | ||
import { hdkey } from 'ethereumjs-wallet'; | ||
// coin used by ledger nano s. | ||
@@ -25,11 +26,16 @@ const CONSTELLATION_COIN = 1137; | ||
generatePrivateKey() { | ||
return curve.genKeyPair(); | ||
return Wallet.generate().getPrivateKey().toString("hex"); | ||
} | ||
async generateEncryptedPrivateKey(privateKey) { | ||
const wallet = Wallet.fromPrivateKey(Buffer.from(privateKey, "hex")); | ||
const result = await wallet.toV3("password"); | ||
return result; | ||
} | ||
async decryptPrivateKey(jKey, password) { | ||
const wallet = await Wallet.fromV3(jKey, password); | ||
const key = wallet.getPrivateKey().toString("hex"); | ||
return key; | ||
} | ||
generateSeedPhrase() { | ||
for (let i = 0; i < 5; i++) { | ||
const mnemonic = bip39.generateMnemonic(); | ||
if (bip39.validateMnemonic(mnemonic)) { | ||
return mnemonic; | ||
} | ||
} | ||
return bip39.generateMnemonic(); | ||
} | ||
@@ -39,9 +45,19 @@ getPrivateKeyFromMnemonic(mnemonic) { | ||
const seedBytes = bip39.mnemonicToSeedSync(mnemonic); | ||
const bip32node = bip32.fromSeed(seedBytes); | ||
const bip32child = bip32node.derivePath(CONSTANTS.BIP_44_PATH); | ||
return Buffer.from(bip32child.privateKey).toString('hex'); | ||
const rootKey = hdkey.fromMasterSeed(seedBytes); | ||
const hardenedKey = rootKey.derivePath(CONSTANTS.BIP_44_PATH); | ||
return hardenedKey.getWallet().getPrivateKey().toString("hex"); | ||
} | ||
} | ||
sign(privateKey, hash) { | ||
const sha512Hash = this.sha512(hash); | ||
getMasterKeyFromMnemonic(mnemonic, index) { | ||
const seedBytes = bip39.mnemonicToSeedSync(mnemonic); | ||
const rootKey = hdkey.fromMasterSeed(seedBytes); | ||
return rootKey.derivePath(CONSTANTS.BIP_44_PATH); | ||
} | ||
getMasterDerivedChild(masterKey, index) { | ||
const childKey = masterKey.deriveChild(index); | ||
const wallet = childKey.getWallet(); | ||
return wallet.getPrivateKeyString(); | ||
} | ||
sign(privateKey, msg) { | ||
const sha512Hash = this.sha512(msg); | ||
const ecSig = curve.sign(sha512Hash, Buffer.from(privateKey, 'hex')); //, {canonical: true}); | ||
@@ -51,4 +67,4 @@ const ecSigHex = Buffer.from(ecSig.toDER()).toString('hex'); | ||
} | ||
verify(publicKey, hash, signature) { | ||
const sha512Hash = this.sha512(hash); | ||
verify(publicKey, msg, signature) { | ||
const sha512Hash = this.sha512(msg); | ||
const validSig = curve.verify(sha512Hash, signature, Buffer.from(publicKey, 'hex')); | ||
@@ -55,0 +71,0 @@ return validSig; |
@@ -20,2 +20,22 @@ import { asn1, pkcs12, pki } from "node-forge"; | ||
}; | ||
KeyStoreFile.prototype.writeP12 = function (crt, sk) { | ||
// var crt = fs.readFileSync ('jiji.cert.pem'); | ||
// var crt_forge = pki.certificateFromPem(CERT_PEM); | ||
// // var sk = fs.readFileSync ('jiji.key.pem'); | ||
// var sk_forge = pki.privateKeyFromPem(PRIV_PEM); | ||
// var encryptedPrivateKeyInfo = pki.encryptedPrivateKeyFromPem(PRIV_PEM); | ||
// var privateKeyInfo = pki.decryptPrivateKeyInfo(encryptedPrivateKeyInfo, 'test1'); | ||
// get the ASN.1 representation of a PKCS#12 archive | ||
//var p12Asn1 = pkcs12.toPkcs12Asn1(sk_forge, [crt_forge], 'test1', {algorithm: '3des'}); | ||
// encode the ASN.1 representation using DER | ||
// const hex = asn1.toDer(privateKeyInfo).toHex(); | ||
// | ||
// const keyMinusPrefix = hex.substring(this.privateKeyHexPrefixLength, hex.length); | ||
// const keyParts = keyMinusPrefix.split(this.secp256k1StringIdentifier); | ||
// | ||
// const privateKey = keyParts[0]; | ||
// const publicKey = keyParts[1]; | ||
// | ||
// return { privateKey, publicKey} ; | ||
}; | ||
return KeyStoreFile; | ||
@@ -22,0 +42,0 @@ }()); |
@@ -43,5 +43,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import * as EC from "elliptic"; | ||
import { bip32 } from './bip32/bip32'; | ||
import { bip39 } from './bip39/bip39'; | ||
var curve = new EC.ec("secp256k1"); | ||
import Wallet from 'ethereumjs-wallet'; | ||
import { hdkey } from 'ethereumjs-wallet'; | ||
// coin used by ledger nano s. | ||
@@ -63,11 +64,35 @@ var CONSTELLATION_COIN = 1137; | ||
KeyStore.prototype.generatePrivateKey = function () { | ||
return curve.genKeyPair(); | ||
return Wallet.generate().getPrivateKey().toString("hex"); | ||
}; | ||
KeyStore.prototype.generateEncryptedPrivateKey = function (privateKey) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var wallet, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
wallet = Wallet.fromPrivateKey(Buffer.from(privateKey, "hex")); | ||
return [4 /*yield*/, wallet.toV3("password")]; | ||
case 1: | ||
result = _a.sent(); | ||
return [2 /*return*/, result]; | ||
} | ||
}); | ||
}); | ||
}; | ||
KeyStore.prototype.decryptPrivateKey = function (jKey, password) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var wallet, key; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, Wallet.fromV3(jKey, password)]; | ||
case 1: | ||
wallet = _a.sent(); | ||
key = wallet.getPrivateKey().toString("hex"); | ||
return [2 /*return*/, key]; | ||
} | ||
}); | ||
}); | ||
}; | ||
KeyStore.prototype.generateSeedPhrase = function () { | ||
for (var i = 0; i < 5; i++) { | ||
var mnemonic = bip39.generateMnemonic(); | ||
if (bip39.validateMnemonic(mnemonic)) { | ||
return mnemonic; | ||
} | ||
} | ||
return bip39.generateMnemonic(); | ||
}; | ||
@@ -77,9 +102,19 @@ KeyStore.prototype.getPrivateKeyFromMnemonic = function (mnemonic) { | ||
var seedBytes = bip39.mnemonicToSeedSync(mnemonic); | ||
var bip32node = bip32.fromSeed(seedBytes); | ||
var bip32child = bip32node.derivePath(CONSTANTS.BIP_44_PATH); | ||
return Buffer.from(bip32child.privateKey).toString('hex'); | ||
var rootKey = hdkey.fromMasterSeed(seedBytes); | ||
var hardenedKey = rootKey.derivePath(CONSTANTS.BIP_44_PATH); | ||
return hardenedKey.getWallet().getPrivateKey().toString("hex"); | ||
} | ||
}; | ||
KeyStore.prototype.sign = function (privateKey, hash) { | ||
var sha512Hash = this.sha512(hash); | ||
KeyStore.prototype.getMasterKeyFromMnemonic = function (mnemonic, index) { | ||
var seedBytes = bip39.mnemonicToSeedSync(mnemonic); | ||
var rootKey = hdkey.fromMasterSeed(seedBytes); | ||
return rootKey.derivePath(CONSTANTS.BIP_44_PATH); | ||
}; | ||
KeyStore.prototype.getMasterDerivedChild = function (masterKey, index) { | ||
var childKey = masterKey.deriveChild(index); | ||
var wallet = childKey.getWallet(); | ||
return wallet.getPrivateKeyString(); | ||
}; | ||
KeyStore.prototype.sign = function (privateKey, msg) { | ||
var sha512Hash = this.sha512(msg); | ||
var ecSig = curve.sign(sha512Hash, Buffer.from(privateKey, 'hex')); //, {canonical: true}); | ||
@@ -89,4 +124,4 @@ var ecSigHex = Buffer.from(ecSig.toDER()).toString('hex'); | ||
}; | ||
KeyStore.prototype.verify = function (publicKey, hash, signature) { | ||
var sha512Hash = this.sha512(hash); | ||
KeyStore.prototype.verify = function (publicKey, msg, signature) { | ||
var sha512Hash = this.sha512(msg); | ||
var validSig = curve.verify(sha512Hash, signature, Buffer.from(publicKey, 'hex')); | ||
@@ -93,0 +128,0 @@ return validSig; |
{ | ||
"name": "@stardust-collective/dag-keystore", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A key management tool for the Constellation Network", | ||
@@ -34,6 +34,6 @@ "module": "./esm5/index.js", | ||
"eth-lib": "^0.1.29", | ||
"ethereumjs-wallet": "^1.0.1", | ||
"js-sha256": "^0.9.0", | ||
"js-sha512": "^0.8.0", | ||
"node-forge": "^0.10.0", | ||
"typeforce": "^1.18.0", | ||
"pbkdf2": "^3.0.9", | ||
@@ -40,0 +40,0 @@ "randombytes": "^2.0.1" |
@@ -0,3 +1,4 @@ | ||
/// <reference types="node" /> | ||
declare class Bip39 { | ||
mnemonicToSeedSync(mnemonic: any, password?: any): any; | ||
mnemonicToSeedSync(mnemonic: any, password?: any): Buffer; | ||
mnemonicToSeed(mnemonic: any, password: any): Promise<unknown>; | ||
@@ -4,0 +5,0 @@ mnemonicToEntropy(mnemonic: any, wordlist: any): string; |
@@ -1,2 +0,2 @@ | ||
// Type definitions for @stardust-collective/dag-keystore 1.0.4 | ||
// Type definitions for @stardust-collective/dag-keystore 1.0.5 | ||
// Project: https://github.com/StardustCollective/dag-keystore.git | ||
@@ -3,0 +3,0 @@ // Definitions by: Frank Fox <https://github.com/StardustCollective> |
@@ -9,2 +9,3 @@ declare class KeyStoreFile { | ||
}; | ||
writeP12(crt?: any, sk?: any): void; | ||
} | ||
@@ -11,0 +12,0 @@ export declare const keyStoreFile: KeyStoreFile; |
/// <reference types="node" /> | ||
import { KeyTrio } from './key-trio'; | ||
import * as EC from "elliptic"; | ||
import { hdkey } from 'ethereumjs-wallet'; | ||
declare class KeyStore { | ||
sha512(hash: string | Buffer): string; | ||
sha256(hash: string | Buffer): string; | ||
generatePrivateKey(): any; | ||
generatePrivateKey(): string; | ||
generateEncryptedPrivateKey(privateKey: string): Promise<V3Keystore>; | ||
decryptPrivateKey(jKey: V3Keystore, password: any): Promise<string>; | ||
generateSeedPhrase(): any; | ||
getPrivateKeyFromMnemonic(mnemonic: string): string; | ||
sign(privateKey: string, hash: string): string; | ||
verify(publicKey: string, hash: string, signature: EC.SignatureInput): any; | ||
getMasterKeyFromMnemonic(mnemonic: string, index: number): hdkey; | ||
getMasterDerivedChild(masterKey: hdkey, index: number): string; | ||
sign(privateKey: string, msg: string): string; | ||
verify(publicKey: string, msg: string, signature: EC.SignatureInput): any; | ||
validateDagAddress(address: string): boolean; | ||
@@ -23,3 +28,24 @@ getPublicKeyFromPrivate(privateKey: string, compact?: boolean): string; | ||
}; | ||
interface V3Keystore { | ||
crypto: { | ||
cipher: string; | ||
cipherparams: { | ||
iv: string; | ||
}; | ||
ciphertext: string; | ||
kdf: string; | ||
kdfparams: KDFParamsOut; | ||
mac: string; | ||
}; | ||
id: string; | ||
version: number; | ||
} | ||
interface KDFParamsOut { | ||
dklen: number; | ||
n: number; | ||
p: number; | ||
r: number; | ||
salt: string; | ||
} | ||
export {}; | ||
//# sourceMappingURL=key-store.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
171116
46
1286
+ Addedethereumjs-wallet@^1.0.1
+ Added@types/bn.js@5.1.5(transitive)
+ Added@types/node@20.12.12(transitive)
+ Added@types/pbkdf2@3.1.2(transitive)
+ Added@types/secp256k1@4.0.6(transitive)
+ Addedaes-js@3.1.2(transitive)
+ Addedblakejs@1.2.1(transitive)
+ Addedbrowserify-aes@1.2.0(transitive)
+ Addedbs58check@2.1.2(transitive)
+ Addedbuffer-xor@1.0.3(transitive)
+ Addedethereum-cryptography@0.1.3(transitive)
+ Addedethereumjs-util@7.1.5(transitive)
+ Addedethereumjs-wallet@1.0.2(transitive)
+ Addedevp_bytestokey@1.0.3(transitive)
+ Addedkeccak@3.0.4(transitive)
+ Addednode-addon-api@2.0.2(transitive)
+ Addednode-gyp-build@4.8.1(transitive)
+ Addedrlp@2.2.7(transitive)
+ Addedscrypt-js@3.0.1(transitive)
+ Addedsecp256k1@4.0.3(transitive)
+ Addedsetimmediate@1.0.5(transitive)
+ Addedundici-types@5.26.5(transitive)
+ Addedutf8@3.0.0(transitive)
+ Addeduuid@8.3.2(transitive)
- Removedtypeforce@^1.18.0
- Removedtypeforce@1.18.0(transitive)