Skip to main content

@portkey/did

Introduction

Provide DID services

Installation

npm install @portkey/did

did.setConfig

Where you configure did server node, graphQL node, storage suite.
Environmental configuration

setConfig(options: IConfig): void;

Usage

did.setConfig({
requestDefaults: {
baseURL: "your did server node",
timeout: "timeout", // optional default 8000ms
},
graphQLUrl: "your graphQL node",
storageMethod: "your storage suite",
});

did.login

type: loginAccount

Email, Google, Apple, or Telegram login.

did.login(type: 'loginAccount', params: AccountLoginParams): Promise<LoginResult>;

Usage

did.login("loginAccount", {
chainId: "chainId",
loginGuardianIdentifier: "loginGuardianIdentifier",
guardiansApproved: [
{
type: "Email",
identifier: "identifier",
verifierId: "verifierId",
verificationDoc: "verificationDoc",
signature: "signature",
},
],
extraData: "extraData",
context: {
requestId: "requestId",
clientId: "clientId",
},
});

Example response

{
status: {
caAddress: 'xxxxxx',
caHash: 'xxxxx';
recoveryStatus: 'pending';
recoveryMessage: 'loading';
};
sessionId: 'xxxx';
error: undefined;
}

type: scan

Logged in management to add management.

login(type: 'scan', params: ScanLoginParams): Promise<true>;

Usage

did.login('scan',{
chainId: 'chainId',
caHash: 'caHash',
managerInfo: {
address: 'address',
extraData: 'extraData'
};
})

did.getLoginStatus

getLoginStatus(params: { chainId: ChainId; sessionId: string }): Promise<RecoverStatusResult>;

Usage

did.getLoginStatus({
chainId: "chainId",
sessionId: "sessionId",
});

Example response

{
caAddress: 'xxxxxx',
caHash: 'xxxxx';
recoveryStatus: 'pending';
recoveryMessage: 'loading';
}

did.logout

Log out. The default setting is to initiate a logout transaction and query the transaction result. When onMethod is the transactionHash, only the TXID will be returned after the transaction and the result will not be queried.

logout(params: EditManagerParams,  sendOption?: SendOptions): Promise<boolean>;

Usage

did.logout({ chainId: 'chainId' }, { onMethod: 'transactionHash' }});

did.sign

sign(data: string): Buffer

Usage

const signStr = did.sign(hexStr).toString("hex");

Example: Verify signature

import AElf from "aelf-sdk";
import elliptic from "elliptic";
const ec = new elliptic.ec("secp256k1");
import { did } from "@portkey/did";
function recoverAElfPubKey(hexStr: string, signStr: string) {
const signData = {
r: signStr.slice(0, 64),
s: signStr.slice(64, 128),
recoveryParam: Number(signStr.slice(signStr.length - 1, signStr.length)),
};
return ec.recoverPubKey(
Buffer.from(AElf.utils.sha256(Buffer.from(hexStr, "hex")), "hex"),
signData,
signData.recoveryParam
);
}

// did
(async () => {
did.create();
const originAddress = did.didWallet.managementAccount.address;
const hexStr = Buffer.from("hello world!").toString("hex");
const signStr = did.sign(hexStr).toString("hex");
const publicKey = recoverAElfPubKey(hexStr, signStr);
const pubKey = ec.keyFromPublic(publicKey).getPublic("hex");
const recoverManagerAddress = AElf.wallet.getAddressFromPubKey(publicKey);
console.log(
pubKey,
// If recoverManagerAddress === originAddress, it means correct recovery
recoverManagerAddress === originAddress,
"recovery result"
);
})();

did.register

register(params: Omit<RegisterParams, 'manager'>): Promise<RegisterResult>;

Usage

did.register({
type: "Email",
loginGuardianIdentifier: "loginGuardianIdentifier",
extraData: "extraData",
chainId: "chainId",
verifierId: "verifierId",
verificationDoc: "verificationDoc",
signature: "signature",
context: {
requestId: "requestId",
clientId: "clientId",
},
});

Example response

{
status: {
caAddress: 'xxxxxx',
caHash: 'xxxxx';
recoveryStatus: 'pending';
recoveryMessage: 'loading';
};
sessionId: 'xxxx';
error: undefined;
}

did.getRegisterStatus

getRegisterStatus(params: { chainId: ChainId; sessionId: string }): Promise<RegisterStatusResult>;

Usage

did.getRegisterStatus({
chainId: "chainId",
sessionId: "sessionId",
});

Example response

{
caAddress: 'xxxxxx',
caHash: 'xxxxx';
recoveryStatus: 'pending';
recoveryMessage: 'loading';
}

did.getHolderInfo

getHolderInfo by graphQL

getHolderInfo(params: Pick<GetHolderInfoParams, 'manager' | 'chainId'>): Promise<GetCAHolderByManagerResult>;

Usage

did.getHolderInfo({
manager: "manager", // optional
chainId: "chainId",
});

Example response

{
id: "tDVV-...sWsX",
chainId: "tDVV",
caHash: "05da...850b",
caAddress: "nJ2K...sWsX",
managerInfos: [
{
address: "2HTm...4jy9",
extraData: "{\"transactionTime\":1697...4232,\"deviceInfo\":\"Ompd...mOJN\",\"version\":\"2.0.0\"}",
__typename: "ManagerInfo"
}
],
originChainId: "tDVV",
__typename: "CAHolderManagerDto",
loginGuardianInfo: [],
}

getHolderInfo by server

getHolderInfo(params: Omit<GetHolderInfoParams, 'manager'>): Promise<IHolderInfo>;

Usage

did.getHolderInfo({
caHash: "caHash", // loginGuardianIdentifier and caHash choose one
loginGuardianIdentifier: "loginGuardianIdentifier", // loginGuardianIdentifier and caHash choose one
chainId: "chainId",
});

Example response

  caAddress: "nJ2K...sWsX",
caHash: "05da...850b",
guardianList: {
guardians: [{
guardianIdentifier: '182...8550@x.com',
identifierHash: 'c6f7...a9bb',
isLoginGuardian: true,
salt: "1c02...7631",
type: "Email",
verifierId: "3775e...5444f",
}];
};
managerInfos: [
{
address: "2HTm...4jy9",
extraData: "{\"transactionTime\":1697...4232,\"deviceInfo\":\"Ompd...mOJN\",\"version\":\"2.0.0\"}",
__typename: "ManagerInfo"
}
],

did.getVerifierServers

Get the VerifierServer information of the corresponding chain.

getVerifierServers(chainId: ChainId): Promise<VerifierItem[]>;

Usage

did.getVerifierServers({
chainId: "chainId",
});

Example response

{
data: [
{
id: 'fdse...twer',
name: 'verfier01',
imageUrl: 'http://xxx.com',
endPoints?: ['http://xxx.com'];
verifierAddresses?: ['http://xxx.com'];
}
],
}

did.checkManagerIsExist

Check if the specified address is the manager of the wallet.
When chainId is the registration chain of the caHash, check if the address is the manager or if it has been removed by other devices.
When chainId isn't the registration chain of the caHash, check if the synchronisation of the manager is in progress.

checkManagerIsExist(params: CheckManagerParams): Promise<boolean>;

Usage

const isExist = await did.checkManagerIsExist({
chainId: "AELF",
caHash: "caHash",
managementAddress: "managementAddress",
});