UNPKG

@arcblock/did

Version:

Javascript lib to work with ArcBlock DID

103 lines (101 loc) 4.56 kB
import { ALIAS_METHODS, ALL_METHODS, CRYPTO_METHODS, DEFAULT_METHOD, isAliasMethod, isCryptoMethod, isKnownMethod } from "./method.mjs"; import { extractIdentifier, extractMethod, formatDid, parse } from "./parse.mjs"; import { getEntityId, isSameEntity } from "./entity.mjs"; import { DID_PREFIX, toBytes, toStrictHex } from "./util.mjs"; import { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, fromTypeInfo, isEthereumDid, isEthereumType, toChecksumAddress, toTypeInfo, toTypeInfoStr } from "./type.mjs"; import { isKnownDid } from "./validate.mjs"; import { expandShortForm, toShortForm } from "./short-form.mjs"; import { getResolveRoute, isGlobalName, isLocalName, parseNameHierarchy } from "./name.mjs"; import { InMemoryNameRegistry } from "./name-registry.mjs"; import { DIDNameResolver } from "./name-resolver.mjs"; import { getHasher, getSigner, types } from "@ocap/mcrypto"; import { stripHexPrefix, toAddress, toBase58, toDid } from "@ocap/util"; //#region src/index.ts /** * Gen DID from private key and type config * * Spec: https://github.com/ArcBlock/ABT-DID-Protocol#create-did * * @public * @static * @param {string} sk - hex encoded secret key string * @param {object} type - wallet type, {@see @ocap/wallet#WalletType} * @returns {string} DID string */ const fromSecretKey = (sk, type) => { const info = DidType(type); const pub = getSigner(info.pk).getPublicKey(sk); return fromPublicKey(pub.indexOf("0x") === 0 ? pub : `0x${pub}`, info); }; /** * Gen DID from public key and type config * * @public * @static * @param {string} pk - hex encoded public key * @param {object} type - wallet type, {@see @ocap/wallet#WalletType} * @returns {string} DID string */ const fromPublicKey = (pk, type) => { const info = DidType(type); return fromPublicKeyHash(getHasher(info.hash)(pk, 1), info); }; const fromPublicKeyHash = (buffer, type) => { const info = DidType(type); const pkHash = stripHexPrefix(buffer).slice(0, 40); const hashFn = getHasher(info.hash); const typeHex = fromTypeInfo(info); const didHash = `0x${typeHex}${pkHash}${stripHexPrefix(hashFn(`0x${typeHex}${pkHash}`, 1)).slice(0, 8)}`; if (isEthereumType(info)) return toChecksumAddress(`0x${buffer.slice(-40)}`); if (info.address === types.EncodingType.BASE58) return toBase58(didHash); return didHash; }; /** * Gen DID from an hex encoded hash and role type * * @public * @static * @param {string} hash - hex encoded hash * @param {enum} role - role type, {@see @ocap/mcrypto#types} * @returns {string} DID string */ const fromHash = (hash, role = types.RoleType.ROLE_ACCOUNT) => { const roleKeys = Object.keys(types.RoleType); const roleValues = Object.values(types.RoleType); if (roleValues.indexOf(role) === -1) throw new Error(`Unsupported role type ${role} when gen ddi from hash`); return fromPublicKeyHash(hash, DidType({ role: types.RoleType[roleKeys[roleValues.indexOf(role)]] })); }; /** * Check if an DID is generated from a publicKey * * @public * @static * @param {string} did - string of the did, usually base58btc format * @param {string} pk - hex encoded publicKey string * @returns {boolean} */ const isFromPublicKey = (did, pk) => { if (isValid(did) === false) return false; return fromPublicKey(pk, toTypeInfo(did)) === toAddress(did); }; /** * Check if a DID string is valid * * @public * @static * @param {string} did - address string * @returns {boolean} */ const isValid = (did) => { if (!did) return false; const address = toAddress(String(did)); const { hash } = toTypeInfo(address); if (typeof hash === "undefined") return false; if (isEthereumDid(address)) return true; const hashFn = getHasher(hash); const bytes = toBytes(address); const bytesHex = toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(0, 22))).toString("hex")); return toStrictHex(Buffer.from(Uint8Array.from(bytes.slice(22, 26))).toString("hex")) === stripHexPrefix(hashFn(`0x${bytesHex}`, 1)).slice(0, 8); }; //#endregion export { ALIAS_METHODS, ALL_METHODS, CRYPTO_METHODS, DEFAULT_METHOD, DIDNameResolver, DID_PREFIX, DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, DidType, InMemoryNameRegistry, expandShortForm, extractIdentifier, extractMethod, formatDid, fromHash, fromPublicKey, fromPublicKeyHash, fromSecretKey, fromTypeInfo, getEntityId, getResolveRoute, isAliasMethod, isCryptoMethod, isEthereumDid, isEthereumType, isFromPublicKey, isGlobalName, isKnownDid, isKnownMethod, isLocalName, isSameEntity, isValid, parse, parseNameHierarchy, toAddress, toDid, toShortForm, toStrictHex, toTypeInfo, toTypeInfoStr, types };