我的代码运行良好,除非尝试发出未签名的凭据。我已经试着弄清楚出了什么问题,但我搞不清楚
我导入的静态testIssuer和凭据对象的结构很好,但未签名Vc的控制台日志不会显示。这意味着在尝试使用VerifiableCredential.extend()
函数创建未签名凭证时,代码get被终止
这是摘录,完整文件如下
提取:
const personalInformation = {
id: user.doc.id.toString(),
name: {
first: firstName,
last: lastName
},
birthDate: birthDate,
sex: sex,
email: email,
phoneNumber: phoneNumber,
address: {
street: streetNumber,
city: city,
state: state,
postalCode: postalCode,
country: country
}
}
console.log(personalInformation)
const unsignedVc = VerifiableCredential.extend({
id: "http://example.edu/credentials/3732",
type: "personalInformationCredential",
issuer: testIssuer.doc.id.toString(),
personalInformation,
})
console.log("Unsigned verifiable credential", unsignedVc)
完整的帖子请求:
const express = require("express");
const Identity = require("@iota/identity-wasm/node")
const cors = require("cors");
const server = express();
const testIssuer = require("./testIssuer.json")
const {
Digest,
DID,
Document,
KeyCollection,
KeyPair,
KeyType,
Method,
VerifiableCredential,
VerifiablePresentation,
} = Identity
const CLIENT_CONFIG = {
network: "main",
node: "https://nodes.thetangle.org:443",
}
server.use(cors({origin: "http://localhost:3000", credentials: true }))
server.use(express.json())
server.post("/create", async (req, res) => {
const {firstName, lastName, birthDate, sex, email, phoneNumber, streetNumber, city, state, postalCode, country} = req.body;
const name = `${firstName} ${lastName}`
try {
const reqIsIncomplete = Object.values(req.body).find(value => !value);
if (reqIsIncomplete) {
return res
.json({
message: "You are missing personal information",
success: false
})
.status(500);
}
// helper function
function generateUser(name) {
const {doc, key} = new Document(KeyType.Ed25519)
return {
doc,
key,
name,
}
}
// Generate a KeyPair, DID, and Document for user
const user = generateUser(name)
// Sign users DID Documents
user.doc.sign(user.key)
user.message = await Identity.publish(user.doc.toJSON(), CLIENT_CONFIG)
console.log(`Published user: https://explorer.iota.org/mainnet/transaction/${user.message}`)
const personalInformation = {
id: user.doc.id.toString(),
name: {
first: firstName,
last: lastName
},
birthDate: birthDate,
sex: sex,
email: email,
phoneNumber: phoneNumber,
address: {
street: streetNumber, //Schuldorffstraße 10
city: city,
state: state,
postalCode: postalCode,
country: country
}
}
console.log(personalInformation)
const unsignedVc = VerifiableCredential.extend({
id: "http://example.edu/credentials/3732",
type: "personalInformationCredential",
issuer: testIssuer.doc.id.toString(),
personalInformation,
})
console.log("Unsigned verifiable credential", unsignedVc)
// Sign the credential with testIssuer's Merkle Key Collection method
const signedVc = testIssuer.doc.signCredential(unsignedVc, {
method: method.id.toString(),
public: keys.public(0),
secret: keys.secret(0),
proof: keys.merkleProof(Digest.Sha256, 0),
})
console.log("Verifiable Credential", signedVc)
if (!testIssuer.doc.verify(signedVc)) {
return res
.json({
message: `Error creating credentials for you, ${firstName}`,
success: false
})
.status(500);
}
return res
.json({
id: user.doc.id.tag,
docHash: user.message,
pubKey: user.key.public,
privKey: user.key.secret,
credential: signedVc,
message: `You have successfully created your digital identity, ${firstName}`,
success: true
})
.status(500);
} catch (error) {
return res
.json({
error: error,
message: "There was a Problem with our Servers",
success: false
})
.status(500);
}
});
我在使用您的代码时收到一个错误
缺少属性:
credentialSubject
而不是:
const unsignedVc = VerifiableCredential.extend({
id: "http://example.edu/credentials/3732",
type: "personalInformationCredential",
issuer: testIssuer.doc.id.toString(),
personalInformation,
})
尝试:
const unsignedVc = VerifiableCredential.extend({
id: "http://example.edu/credentials/3732",
type: "personalInformationCredential",
issuer: testIssuer.doc.id.toString(),
credentialSubject: personalInformation,
})