1.3.3 β’ Published 7 months ago
@kaytrust/did-near-resolver v1.3.3
NearDIDResolver SDK (TypeScript)
A lightweight TypeScript SDK to resolve did:near:<identifier> DIDs from a NEAR smart contract registry and/or NEAR accounts.
π¦ Installation
npm install near-api-js bs58Or with Yarn:
yarn add near-api-js bs58π Usage
import NearDIDResolver from './NearDIDResolver';
const resolver = new NearDIDResolver(
'registry.contract.testnet',
'https://rpc.testnet.near.org',
'testnet'
);
const didDocument = await resolver.resolveDID('did:near:CF5RiJYh4EVmEt8UADTjoP3XaZo1NPWxv6w5TmkLqjpR');
console.log(didDocument);π§ Constructor
new NearDIDResolver(contractId: string, rpcUrl: string, networkId: string = 'testnet')contractId: The NEAR smart contract that implementsidentity_owner.rpcUrl: RPC URL for NEAR network.networkId: NEAR network (e.g.,testnet,mainnet).
π resolveDID(did: string)
Resolves a did:near:<base58PublicKey | accountId> into a DID Document.
Resolution Strategy:
- Calls the
identity_ownerview function on the specified contract. - If the result is a
did:near:<key>, it strips the prefix. - Otherwise assumes itβs a base58-encoded Ed25519 public key.
Example output:
{
"@context": "https://w3id.org/did/v1",
"id": "did:near:CF5Ri...",
"verificationMethod": [
{
"id": "did:near:CF5Ri...#owner",
"type": "Ed25519VerificationKey2018",
"controller": "did:near:CF5Ri...",
"publicKeyBase58": "CF5Ri..."
}
],
"authentication": ["did:near:CF5Ri...#owner"],
"assertionMethod": ["did:near:CF5Ri...#owner"]
}π§° getPublicKeyFromRPC(accountId: string): Promise<string>
Fetches the public key of a NEAR account directly from the RPC (not from the registry).
π Cryptographic Notes
- Uses Ed25519 public keys encoded in base58.
- Compatible with
Ed25519VerificationKey2018type in the DID Document spec. Supports keys stored:
- Directly on-chain in the smart contract
- From NEAR account keys using
view_account
π§ͺ Testing
To test your resolver:
- Deploy a compatible
NearDIDRegistrycontract. - Register a DID and owner.
- Call
resolveDID(...)from this SDK. - Validate it against a DID resolver or JWT verifier like
did-jwt.
π License
MIT β use freely in your own DID resolver stacks and NEAR integrations.