管理身份,支持身份的创建、登录、登出、更新和导入导出等操作

Constructors

Methods

  • 创建新身份,生成 BlockAddress 并加密存储,同时缓存登录信息

    Parameters

    • password: string

      身份密码

    • template: IdentityTemplate

      身份模板

    Returns Promise<Identity>

    返回创建的身份信息

    const template = { code: IdentityCodeEnum.IDENTITY_CODE_PERSONAL }
    const identity = await identityManager.createIdentity('example-password', template)
  • 导出身份信息为 JSON 字符串,验证身份的完整性和合法性后返回序列化的身份信息

    Parameters

    • did: string

      身份的 DID

    Returns Promise<string>

    返回身份的 JSON 字符串

    const identityJson = await identityManager.exportIdentity('example-did')
    
  • 获取当前登录的身份 DID

    Returns undefined | string

    返回当前登录的 DID,如果未登录则返回 undefined

    const activeDid = identityManager.getActiveDid()
    
  • 获取当前登录的身份信息

    Returns Promise<undefined | Identity>

    返回当前登录的身份对象,如果未登录则返回 undefined

    const activeIdentity = await identityManager.getActiveIdentity()
    
  • 获取身份的 BlockAddress

    Parameters

    • did: string

      身份的 DID

    Returns Promise<BlockAddress>

    返回解密后的 BlockAddress

    const blockAddress = await identityManager.getBlockAddress('example-did')
    
  • 获得当前身份状态

    Returns Promise<IdentityState>

  • 获取历史登录记录

    Returns string[]

    返回历史登录的 DID 列表

    const history = identityManager.getHistory()
    
  • 获取身份信息,如果身份已缓存,则直接返回;否则从本地缓存中加载并验证

    Parameters

    • did: string

      身份的 DID

    Returns Promise<Identity>

    返回身份信息

    const identity = await identityManager.getIdentity('example-did')
    
  • 获取当前节点信息

    Parameters

    • Optionaldomain: string

      可选的节点域名(默认为当前窗口的域名)

    Returns Promise<undefined | ServiceMetadata>

    返回节点的元数据

    const nodeMetadata = await identityManager.getNode()
    
  • 导入身份信息,从 JSON 字符串导入身份,解密 BlockAddress 并缓存登录信息

    Parameters

    • content: string

      身份的 JSON 字符串

    • password: string

      身份密码

    Returns Promise<Identity>

    返回导入的身份信息

    InvalidPassword 密码错误

    const identity = await identityManager.importIdentity(identityJson, 'example-password')
    
  • 检查是否已登录指定身份

    Parameters

    • did: string

      身份的 DID

    Returns boolean

    如果已登录,返回 true;否则返回 false

    const isLogged = identityManager.isLogin('example-did')
    
  • 登录身份,解密身份信息

    Parameters

    • did: string

      身份的 DID

    • password: string

      登录密码

    Returns Promise<Identity>

    返回登录的身份信息

    InvalidPassword 密码错误

    const identity = await identityManager.login('example-did', 'example-password')
    
  • 登出当前身份

    Returns void

    identityManager.logout()
    
  • 更新身份信息, 解密 BlockAddress 并使用新的模板信息更新身份

    Parameters

    • did: string

      身份的 DID

    • template: Partial<IdentityTemplate>

      部分更新的身份模板

    • Optionalpassword: string

      身份密码,如果已登陆可以不用传密码

    Returns Promise<Identity>

    返回更新后的身份信息

    InvalidPassword 密码错误

    NoPermission 不允许执行更新操作

    const template = { extend: { name: 'New Name' } }
    const updatedIdentity = await identityManager.updateIdentity('example-did', template, 'example-password')