Working with DIDs in Other Wallets
Developers can also work with decentralized identitiers (DIDs) using any standard interface, such ethers.js or web3.js and submit DID payloads through popular wallets such as MetaMask.
W3C-compliant DID transaction payloads are highly complex!
In order to generate them, developers can use one of the identity SDKs.
This method is only recommended for advanced applications; most developers will prefer to use the Essentials Connector for the user-friendly features.
Publish DID Transaction
The example code below demonstrates how to publish a DID on the Elastos Identity (EID) chain using the Ethers package and the official DID smart contract.
DID functionality for the EID chain is built directly into the virtual machine, so the smart contract serves only as a general message passing interface and does not implement the full DID spec.
- 🌐 JavaScript
const { ethers } = require("ethers");
const provider = new ethers.providers.JsonRpcProvider(
"https://api.elastos.io/eid" // set chain rpc url
);
const contractAbi = [
{
inputs: [],
stateMutability: "nonpayable",
type: "constructor",
},
{
inputs: [
{
internalType: "string",
name: "data",
type: "string",
},
],
name: "publishDidTransaction",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
];
const contractAddress = "0x46E5936a9bAA167b3368F4197eDce746A66f7a7a"; // set DID contract address
const contract = new ethers.Contract(contractAddress, contractAbi, provider);
const keystore = "..."; // Add keystore
const password = "..."; // Add password
// Decrypt account using the keystore and password
const wallet = ethers.Wallet.fromEncryptedJsonSync(keystore, password);
// Create a DID
const createDID =
'{"header":{"specification":"elastos/did/1.0","operation":"create"},"payload":"..."}'; // Add DID payload
// Function to publish the DID transaction
async function publishDIDTransaction() {
try {
const result = await contract.publishDidTransaction(createDID, {
from: wallet.address,
});
console.log("Transaction successful with transaction hash:", result.hash);
} catch (err) {
console.error("Error:", err);
}
}
publishDIDTransaction();