@arcblock/mcrypto
Advanced tools
Comparing version 0.12.2 to 0.12.3
@@ -9,3 +9,3 @@ const sha3 = require('js-sha3'); | ||
const hasher = data => sha3[`keccak${x}`](data); | ||
const fn = (data, round = 1) => { | ||
const hashFn = (data, round) => { | ||
const input = isHexStrict(data) ? hexToBytes(data) : data; | ||
@@ -16,6 +16,6 @@ if (round === 1) { | ||
return fn(hasher(input), round - 1); | ||
return hashFn(hasher(input), round - 1); | ||
}; | ||
this[name] = fn; | ||
this[name] = (data, round = 1) => `0x${hashFn(data, round)}`; | ||
}); | ||
@@ -22,0 +22,0 @@ } |
@@ -8,7 +8,3 @@ const { isHexStrict, hexToBytes } = require('@arcblock/forge-util'); | ||
}; | ||
const encoders = { | ||
utf8: require('crypto-js/enc-utf8'), | ||
hex: require('crypto-js/enc-hex'), | ||
base64: require('crypto-js/enc-base64'), | ||
}; | ||
const hex = require('crypto-js/enc-hex'); | ||
@@ -20,12 +16,12 @@ class Sha2Hasher { | ||
const hasher = hashFns[`sha${x}`]; | ||
const fn = (data, round = 2, outputEncoding = 'hex') => { | ||
const hashFn = (data, round) => { | ||
const input = isHexStrict(data) ? hexToBytes(data) : data; | ||
if (round === 1) { | ||
return hasher(input).toString(encoders[outputEncoding]); | ||
return hasher(input).toString(hex); | ||
} | ||
return fn(hasher(input), round - 1, outputEncoding); | ||
return hashFn(hasher(input), round - 1); | ||
}; | ||
this[name] = fn; | ||
this[name] = (data, round = 2) => `0x${hashFn(data, round)}`; | ||
}); | ||
@@ -32,0 +28,0 @@ } |
@@ -9,3 +9,3 @@ const sha3 = require('js-sha3'); | ||
const hasher = sha3[`sha3_${x}`]; | ||
const fn = (data, round = 1) => { | ||
const hashFn = (data, round) => { | ||
const input = isHexStrict(data) ? hexToBytes(data) : data; | ||
@@ -16,6 +16,6 @@ if (round === 1) { | ||
return fn(hasher(input), round - 1); | ||
return hashFn(hasher(input), round - 1); | ||
}; | ||
this[name] = fn; | ||
this[name] = (data, round = 1) => `0x${hashFn(data, round)}`; | ||
}); | ||
@@ -22,0 +22,0 @@ } |
@@ -89,3 +89,8 @@ declare class Ed25519Signer { | ||
RoleType: _Lib.T106; | ||
EncodingType: _Lib.T109; | ||
} | ||
export interface T109 { | ||
BASE16: number; | ||
BASE58: number; | ||
} | ||
export interface T108 { | ||
@@ -92,0 +97,0 @@ Signer: _Lib.T101; |
@@ -0,1 +1,2 @@ | ||
// FIXME: enum definition of forge-abi and abt-did-elixir are not exactly the same | ||
const types = { | ||
@@ -9,6 +10,6 @@ KeyType: { | ||
SHA3: 1, | ||
KECCAK_384: 6, | ||
SHA3_384: 7, | ||
KECCAK_512: 13, | ||
SHA3_512: 14, | ||
KECCAK_384: 2, | ||
SHA3_384: 3, | ||
KECCAK_512: 4, | ||
SHA3_512: 5, | ||
}, | ||
@@ -25,3 +26,9 @@ RoleType: { | ||
ROLE_VALIDATOR: 8, | ||
ROLE_GROUP: 9, | ||
ROLE_ANY: 63, | ||
}, | ||
EncodingType: { | ||
BASE16: 0, | ||
BASE58: 1, | ||
}, | ||
}; | ||
@@ -28,0 +35,0 @@ |
@@ -24,23 +24,24 @@ const ed25519 = require('tweetnacl').sign; | ||
genKeyPair(encoding = 'hex') { | ||
const keyPair = ed25519.keyPair.fromSecretKey(Uint8Array.from(randomBytes(64))); | ||
if (encoding === 'hex') { | ||
keyPair.publicKey = bytesToHex(keyPair.publicKey); | ||
keyPair.secretKey = bytesToHex(keyPair.secretKey); | ||
} | ||
genKeyPair() { | ||
const seed = Uint8Array.from(randomBytes(32)); | ||
const keyPair = ed25519.keyPair.fromSeed(seed); | ||
keyPair.publicKey = bytesToHex(keyPair.publicKey); | ||
keyPair.secretKey = bytesToHex(keyPair.secretKey); | ||
return keyPair; | ||
} | ||
getPublicKey(sk, encoding = 'hex') { | ||
getPublicKey(sk) { | ||
const skBytes = this.toUint8Array(sk); | ||
const pk = ed25519.keyPair.fromSecretKey(skBytes).publicKey; | ||
return encoding === 'hex' ? bytesToHex(pk) : pk; | ||
return bytesToHex(pk); | ||
} | ||
sign(message, sk, encoding = 'hex') { | ||
sign(message, sk) { | ||
const skBytes = this.toUint8Array(sk); | ||
const messageBytes = this.toUint8Array(message); | ||
// console.log('mcrypto.sign', { skBytes, sk, messageBytes, message }); | ||
const signature = ed25519.detached(messageBytes, skBytes); | ||
return encoding === 'hex' ? bytesToHex(signature) : signature; | ||
return bytesToHex(signature); | ||
} | ||
@@ -47,0 +48,0 @@ |
const EC = require('elliptic').ec; | ||
const { isHexStrict } = require('@arcblock/forge-util'); | ||
const BN = require('bn.js'); | ||
const randomBytes = require('randombytes'); | ||
const { isHexStrict, bytesToHex } = require('@arcblock/forge-util'); | ||
const Signer = require('../protocols/signer'); | ||
const secp256k1 = new EC('secp256k1'); | ||
const compressed = false; | ||
const encoding = 'hex'; | ||
@@ -16,14 +20,17 @@ class Secp256k1Signer extends Signer { | ||
genKeyPair(compressed = false, encoding = 'hex') { | ||
const keyPair = secp256k1.genKeyPair(); | ||
const sk = keyPair.getPrivate(encoding); | ||
const pk = keyPair.getPublic(compressed, encoding); | ||
if (encoding === 'hex') { | ||
return { secretKey: `0x${sk}`, publicKey: `0x${pk}` }; | ||
} | ||
isValidSK(sk) { | ||
const bn = new BN(sk); | ||
return bn.cmp(secp256k1.curve.n) < 0 && !bn.isZero(); | ||
} | ||
return { secretKey: sk, publicKey: pk }; | ||
genKeyPair() { | ||
let sk = null; | ||
do { | ||
sk = Uint8Array.from(randomBytes(32)); | ||
} while (!this.isValidSK(sk)); | ||
const pk = this.getPublicKey(bytesToHex(sk)); | ||
return { secretKey: bytesToHex(sk), publicKey: pk }; | ||
} | ||
getPublicKey(sk, compressed = false, encoding = 'hex') { | ||
getPublicKey(sk) { | ||
const pk = secp256k1.keyFromPrivate(this.strip0x(sk), encoding).getPublic(compressed, encoding); | ||
@@ -33,3 +40,3 @@ return `0x${pk}`; | ||
sign(message, sk, encoding = 'hex') { | ||
sign(message, sk) { | ||
const signature = secp256k1 | ||
@@ -42,3 +49,3 @@ .keyFromPrivate(this.strip0x(sk), encoding) | ||
verify(message, signature, pk, encoding = 'hex') { | ||
verify(message, signature, pk) { | ||
return secp256k1 | ||
@@ -45,0 +52,0 @@ .keyFromPublic(this.strip0x(pk), encoding) |
{ | ||
"name": "@arcblock/mcrypto", | ||
"version": "0.12.2", | ||
"version": "0.12.3", | ||
"description": "Crypto lib that provides signer,crypter,hasher interface", | ||
@@ -40,3 +40,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@arcblock/forge-util": "^0.12.2", | ||
"@arcblock/forge-util": "^0.12.3", | ||
"crypto-js": "^3.1.9-1", | ||
@@ -49,3 +49,3 @@ "elliptic": "^6.4.1", | ||
}, | ||
"gitHead": "56a2e548c7abc10ce76a96c259b7a6b2cd11148e" | ||
"gitHead": "750553ccecfb063e55f5a62d721b6c3553678bcf" | ||
} |
@@ -32,2 +32,4 @@ # `@arcblock/mcrypto` | ||
**Important Note: results from signer and hasher are all hex encoded string** | ||
## Implementation | ||
@@ -34,0 +36,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14183
358
50
Updated@arcblock/forge-util@^0.12.3