用于身份验证的类,负责生成和验证消息头签名
构造函数
包含 DID 和私钥的地址信息
const blockAddress = { identifier: 'example-did', privateKey: 'example-private-key' };const auth = new Authenticate(blockAddress); Copy
const blockAddress = { identifier: 'example-did', privateKey: 'example-private-key' };const auth = new Authenticate(blockAddress);
生成签名的消息头
Optional
可选的附加消息体
返回签名后的消息头
const header = await auth.createHeader(); Copy
const header = await auth.createHeader();
处理响应并验证其签名
响应对象
响应体的 Protobuf 模式
判断状态是否成功函数
InvalidArgument 如果时间戳过期
NoPermission 如果签名无效
const response = { header: { ... }, body: { ... } };const bodySchema = MessageHeaderSchema;try { await auth.doResponse(response, bodySchema); console.log('Response is valid');} catch (err) { console.error(err);} Copy
const response = { header: { ... }, body: { ... } };const bodySchema = MessageHeaderSchema;try { await auth.doResponse(response, bodySchema); console.log('Response is valid');} catch (err) { console.error(err);}
获取当前实例的 DID
返回 DID 字符串
const did = auth.getDid(); Copy
const did = auth.getDid();
对数据进行签名
需要签名的数据
返回签名后的数据
const data = new Uint8Array([1, 2, 3]);const signature = await auth.sign(data); Copy
const data = new Uint8Array([1, 2, 3]);const signature = await auth.sign(data);
验证消息头的签名是否有效
消息头对象
const header = await auth.createHeader();try { await auth.verifyHeader(header, undefined); console.log('Header is valid');} catch (err) { console.error(err);} Copy
const header = await auth.createHeader();try { await auth.verifyHeader(header, undefined); console.log('Header is valid');} catch (err) { console.error(err);}
Static
验证签名是否有效
DID 字符串
需要验证的数据
签名字符串
如果签名有效,返回 true;否则返回 false
const data = new Uint8Array([1, 2, 3]);const isValid = await auth.verify('example-did', data, 'example-signature');console.log(isValid); // 输出: true 或 false Copy
const data = new Uint8Array([1, 2, 3]);const isValid = await auth.verify('example-did', data, 'example-signature');console.log(isValid); // 输出: true 或 false
用于身份验证的类,负责生成和验证消息头签名