@arcblock/abt-did
Advanced tools
Comparing version 0.12.0 to 0.12.2
@@ -7,12 +7,4 @@ const upperFirst = require('lodash/upperFirst'); | ||
const hdkey = require('hdkey'); | ||
const { | ||
DID_PREFIX, | ||
toBits, | ||
toBytes, | ||
toStrictHex, | ||
signer, | ||
hasher, | ||
types, | ||
jwtHeaders, | ||
} = require('./util'); | ||
const { DID_PREFIX, toBits, toBytes, toStrictHex } = require('./util'); | ||
const { types, getSigner, getHasher } = Mcrypto; | ||
@@ -78,3 +70,3 @@ /** | ||
const { key = types.KeyType.ED25519 } = type || {}; | ||
const pk = signer[key].getPublicKey(sk); | ||
const pk = getSigner(key).getPublicKey(sk); | ||
return fromPublicKey(pk.indexOf('0x') === 0 ? pk : `0x${pk}`, type); | ||
@@ -92,7 +84,8 @@ }; | ||
const { hash = types.HashType.SHA3 } = type || {}; | ||
const hashFn = getHasher(hash); | ||
const typeHex = fromTypeInfo(type); | ||
const pkHash = hasher[hash](pk); | ||
const pkHash = hashFn(pk); | ||
const checksum = hasher[hash](`0x${typeHex}${pkHash.slice(0, 40)}`).slice(0, 8); | ||
const checksum = hashFn(`0x${typeHex}${pkHash.slice(0, 40)}`).slice(0, 8); | ||
const didHash = `${typeHex}${pkHash.slice(0, 40)}${checksum}`; | ||
@@ -199,6 +192,7 @@ | ||
const hashFn = getHasher(hash); | ||
const bytes = toBytes(did); | ||
const bytesHex = toStrictHex(Buffer.from(bytes.slice(0, 22)).toString('hex')); | ||
const didChecksum = toStrictHex(Buffer.from(bytes.slice(22, 26)).toString('hex')); | ||
const checksum = hasher[hash](`0x${bytesHex}`).slice(0, 8); | ||
const checksum = hashFn(`0x${bytesHex}`).slice(0, 8); | ||
return didChecksum === checksum; | ||
@@ -221,5 +215,15 @@ }; | ||
const type = toTypeInfo(did); | ||
const headers = { | ||
[types.KeyType.SECP256K1]: { | ||
alg: 'ES256K', | ||
type: 'JWT', | ||
}, | ||
[types.KeyType.ED25519]: { | ||
alg: 'Ed25519', | ||
type: 'JWT', | ||
}, | ||
}; | ||
// make header | ||
const header = jwtHeaders[type.key]; | ||
const header = headers[type.key]; | ||
const headerB64 = base64.escape(base64.encode(JSON.stringify(header))); | ||
@@ -258,3 +262,3 @@ | ||
const msgHex = toHex(`${headerB64}.${bodyB64}`); | ||
const sigHex = signer[type.key].sign(msgHex, sk); | ||
const sigHex = getSigner(type.key).sign(msgHex, sk); | ||
const sigB64 = base64.escape(Buffer.from(sigHex.replace(/^0x/, ''), 'hex').toString('base64')); | ||
@@ -296,5 +300,5 @@ | ||
const signers = { | ||
secp256k1: signer[types.KeyType.SECP256K1], | ||
es256k: signer[types.KeyType.SECP256K1], | ||
ed25519: signer[types.KeyType.ED25519], | ||
secp256k1: getSigner(types.KeyType.SECP256K1), | ||
es256k: getSigner(types.KeyType.SECP256K1), | ||
ed25519: getSigner(types.KeyType.ED25519), | ||
}; | ||
@@ -301,0 +305,0 @@ const alg = header.alg.toLowerCase(); |
const padStart = require('lodash/padStart'); | ||
const { toBN } = require('@arcblock/forge-util'); | ||
const Mcrypto = require('@arcblock/mcrypto'); | ||
const multibase = require('multibase'); | ||
@@ -8,53 +7,2 @@ | ||
const enums = Object.freeze({ | ||
KeyType: { | ||
ED25519: 0, | ||
SECP256K1: 1, | ||
}, | ||
HashType: { | ||
KECCAK: 0, | ||
SHA3: 1, | ||
KECCAK_384: 6, | ||
SHA3_384: 7, | ||
KECCAK_512: 13, | ||
SHA3_512: 14, | ||
}, | ||
RoleType: { | ||
ROLE_ACCOUNT: 0, | ||
ROLE_NODE: 1, | ||
ROLE_DEVICE: 2, | ||
ROLE_APPLICATION: 3, | ||
ROLE_SMART_CONTRACT: 4, | ||
ROLE_BOT: 5, | ||
ROLE_ASSET: 6, | ||
ROLE_STAKE: 7, | ||
ROLE_VALIDATOR: 8, | ||
}, | ||
}); | ||
const signer = Object.freeze({ | ||
[enums.KeyType.ED25519]: Mcrypto.Signer.Ed25519, | ||
[enums.KeyType.SECP256K1]: Mcrypto.Signer.Secp256k1, | ||
}); | ||
const hasher = Object.freeze({ | ||
[enums.HashType.KECCAK]: Mcrypto.Hasher.Keccak.hash256, | ||
[enums.HashType.KECCAK_384]: Mcrypto.Hasher.Keccak.hash384, | ||
[enums.HashType.KECCAK_512]: Mcrypto.Hasher.Keccak.hash512, | ||
[enums.HashType.SHA3]: Mcrypto.Hasher.SHA3.hash256, | ||
[enums.HashType.SHA3_384]: Mcrypto.Hasher.SHA3.hash384, | ||
[enums.HashType.SHA3_512]: Mcrypto.Hasher.SHA3.hash512, | ||
}); | ||
const jwtHeaders = { | ||
[enums.KeyType.SECP256K1]: { | ||
alg: 'ES256K', | ||
type: 'JWT', | ||
}, | ||
[enums.KeyType.ED25519]: { | ||
alg: 'Ed25519', | ||
type: 'JWT', | ||
}, | ||
}; | ||
/** | ||
@@ -110,6 +58,2 @@ * Convert did to bytes with length of 26 | ||
toStrictHex, | ||
signer, | ||
hasher, | ||
jwtHeaders, | ||
types: enums, | ||
}; |
{ | ||
"name": "@arcblock/abt-did", | ||
"version": "0.12.0", | ||
"version": "0.12.2", | ||
"description": "Javascript lib to work with ArcBlock DID", | ||
@@ -40,4 +40,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@arcblock/forge-util": "^0.12.0", | ||
"@arcblock/mcrypto": "^0.12.0", | ||
"@arcblock/forge-util": "^0.12.2", | ||
"@arcblock/mcrypto": "^0.12.2", | ||
"base64-url": "^2.2.0", | ||
@@ -50,3 +50,3 @@ "debug": "^4.1.1", | ||
}, | ||
"gitHead": "bcc04e32e2ce37829d4cbcb1daabda14dceba4f9" | ||
"gitHead": "56a2e548c7abc10ce76a96c259b7a6b2cd11148e" | ||
} |
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
13389
338
Updated@arcblock/forge-util@^0.12.2
Updated@arcblock/mcrypto@^0.12.2