var didJwt = require('did-jwt'); var secp256r1 = require('@mysten/sui/keypairs/secp256r1'); var utils = require('@mysten/sui/utils'); var cryptography = require('@mysten/sui/cryptography'); var bcs = require('@mysten/sui/bcs'); const Publisher = { testnet: 'https://publisher-devnet.walrus.space/v1', // TODO: update devnet: 'https://publisher-devnet.walrus.space/v1' // TODO: update }; const Aggregator = { testnet: 'https://aggregator-devnet.walrus.space/v1', // TODO: update devnet: 'https://aggregator-devnet.walrus.space/v1' // TODO: update }; class WalrusDIDController { constructor(network, privateKey) { this.signer = void 0; this.network = void 0; this.publicKey = void 0; this.network = network; this.signer = secp256r1.Secp256r1Keypair.fromSecretKey(utils.fromB64(privateKey)); this.publicKey = this.signer.getPublicKey().toBase64(); } static GetSerializer() { const Serializer = bcs.bcs.struct('SigData', { data: bcs.bcs.string(), publicKey: bcs.bcs.string(), signature: bcs.bcs.string() }); return Serializer; } create(payload, epochs = 1) { try { const _this = this; if (_this.signer && payload.subject.match(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/)) { const Serializer = WalrusDIDController.GetSerializer(); const data = Serializer.serialize({ data: payload.subject, publicKey: payload.publicKey, signature: payload.signature }); return Promise.resolve(_this.signer.sign(data.toBytes())).then(function (signature) { const created = new Date().toISOString(); const metaData = { created, epochs, issuer: _this.signer.getPublicKey().toBase64(), walrus: Serializer.serialize({ data: data.toBase64(), publicKey: _this.signer.getPublicKey().toBase64(), signature: utils.toB64(signature) }).toBase64() }; const url = Publisher[_this.network]; return Promise.resolve(fetch(`${url}/store?epochs=${epochs}`, { method: 'PUT', body: JSON.stringify(metaData) })).then(function (res) { if (res.status === 200) { return Promise.resolve(res.json()).then(function ({ newlyCreated }) { if (newlyCreated && newlyCreated.blobObject) { const identifier = didJwt.bytesToBase58(didJwt.base64ToBytes(newlyCreated.blobObject.blobId)); const did = _this.network !== 'mainnet' ? `did:walrus:${_this.network}:${identifier}` : `did:walrus:${identifier}`; return WalrusDIDController.CreateDoc(did, metaData); } else { throw new Error('Something went wrong when storing the blob! (2)'); } }); } else { throw new Error('Something went wrong when storing the blob! (1)'); } }); }); } else { throw new Error('signer error'); } return Promise.resolve(); } catch (e) { return Promise.reject(e); } } getPublicKey() { return this.publicKey; } static CreateDoc(did, metaData) { const Serializer = WalrusDIDController.GetSerializer(); const publicKey = utils.fromB64(Serializer.fromBase64(Serializer.fromBase64(metaData.walrus).data).publicKey); const signatureScheme = cryptography.SIGNATURE_FLAG_TO_SCHEME[publicKey[0]]; const type = signatureScheme === 'Secp256r1' ? 'EcdsaSecp256r1VerificationKey2019' : 'zkLoginSignatureVerification'; return { didDocumentMetadata: { ...metaData }, didResolutionMetadata: { contentType: 'application/did+ld+json' }, didDocument: { '@context': ['https://www.w3.org/ns/did/v1'], id: did, verificationMethod: [{ id: `${did}#keys-1`, controller: `${did}`, type, publicKeyBase64: utils.toB64(publicKey.slice(1)) }], authentication: [], assertionMethod: [] } }; } } function _catch(body, recover) { try { var result = body(); } catch (e) { return recover(e); } if (result && result.then) { return result.then(void 0, recover); } return result; } class WalrusDIDResolver { constructor(network) { this.aggregator = {}; this.aggregator = network && network.aggregator || Aggregator; } resolve(did) { try { const _this = this; const parsedId = did.split(':'); const fullId = parsedId[parsedId.length - 1].match(/^[1-9A-HJ-NP-Za-km-z]+$/); if (!fullId || parsedId.length !== 3 && parsedId.length !== 4) { return Promise.resolve({ didResolutionMetadata: { error: 'invalid did', message: `Not a valid did: ${did}` }, didDocumentMetadata: {}, didDocument: null }); } const network = parsedId.length === 3 ? 'mainnet' : parsedId[2]; if (network !== 'mainnet' && network !== 'testnet' && network !== 'devnet') { return Promise.resolve({ didResolutionMetadata: { error: 'invalid network', message: `Not a valid network: ${network}` }, didDocumentMetadata: {}, didDocument: null }); } return Promise.resolve(_catch(function () { const blobId = didJwt.bytesToBase64url(didJwt.base58ToBytes(parsedId[parsedId.length - 1])); return Promise.resolve(fetch(`${_this.aggregator[network]}/${blobId}`)).then(function (res) { return Promise.resolve(res.text()).then(function (_res$text) { const metadata = JSON.parse(_res$text); return WalrusDIDController.CreateDoc(did, metadata); }); }); }, function (error) { return { didResolutionMetadata: { error: 'notFound', message: error.toString() }, didDocumentMetadata: {}, didDocument: null }; })); } catch (e) { return Promise.reject(e); } } build() { return { walrus: this.resolve.bind(this) }; } } exports.WalrusDIDController = WalrusDIDController; exports.WalrusDIDResolver = WalrusDIDResolver; //# sourceMappingURL=index.cjs.map