Created
July 8, 2020 14:38
-
-
Save stuartf/9c7e497034a4803d57d1034067e0701a to your computer and use it in GitHub Desktop.
Attempt to create long-form sidetree dids using element-lib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const element = require("@transmute/element-lib"); | |
const db = new element.adapters.database.ElementRXDBAdapter({ | |
name: "element-rxdb.element-app", | |
adapter: "memory", | |
}); | |
const storage = new element.adapters.storage.StorageManager( | |
db, | |
element.storage.ipfs.configure({ | |
multiaddr: "/dns4/ipfs.infura.io/tcp/5001/https", | |
}), | |
{ | |
autoPersist: false, | |
retryIntervalSeconds: 5, | |
} | |
); | |
const blockchain = element.blockchain.ethereum.configure({ | |
mnemonic: | |
"hazard pride garment scout search divide solution argue wait avoid title cave", | |
hdPath: "m/44'/60'/0'/0/0", | |
providerUrl: `https://ropsten.infura.io/v3/${process.env.INFURA_KEY}`, | |
anchorContractAddress: "0xD49Da2b7C0A15f6ac5A856f026D68A9B9848D96f", | |
}); | |
const parameters = { | |
maxOperationsPerBatch: 5, | |
batchingIntervalInSeconds: 1, | |
didMethodName: "did:elem:ropsten", | |
logLevel: "error", | |
mapSync: false, | |
maxNumberOfBlocksPerSync: 1000, | |
}; | |
const sidetree = new element.Sidetree({ | |
db, | |
storage, | |
blockchain, | |
parameters, | |
}); | |
/** | |
* | |
*/ | |
module.exports.createDid = function () { | |
// Generate a simple did document model | |
const mks = new element.MnemonicKeySystem( | |
element.MnemonicKeySystem.generateMnemonic() | |
); | |
return Promise.all([ | |
mks.getKeyForPurpose("primary", 0), | |
mks.getKeyForPurpose("recovery", 0), | |
]).then((keys) => { | |
const primaryKey = keys[0]; | |
const recoveryKey = keys[1]; | |
const didDocumentModel = sidetree.op.getDidDocumentModel( | |
primaryKey, | |
recoveryKey | |
); | |
// Generate Sidetree Create payload | |
const createPayload = sidetree.op.getCreatePayload( | |
didDocumentModel, | |
primaryKey | |
); | |
// We could now write this did to the chain | |
// const createTransaction = await element.batchScheduler.writeNow(createPayload) | |
const didUniqueSuffix = element.func.getDidUniqueSuffix(createPayload); | |
// I expect to find Create Operation Suffix Data Object and Create Operation Delta Object in this payload per https://identity.foundation/sidetree/spec/#create | |
// but instead it looks like: | |
// {"@context":"https://w3id.org/did/v1","publicKey":[{"id":"#primary","usage":"signing","type":"Secp256k1VerificationKey2018","publicKeyHex":{"publicKey":"034326b0ab91510d63563f188d58e956fb6693fafd62cfe9a6aae8de401352af47","privateKey":"2a44be3fe7fab7752c0267aa53307214e5ef0bc07d3c20a81be69dc9feb290a7"}},{"id":"#recovery","usage":"recovery","type":"Secp256k1VerificationKey2018","publicKeyHex":{"publicKey":"03c57843baee5d5876e03e6cb5e553a154963daaa76bae9b775c25308ba9722a7c","privateKey":"abf92b49d7cc299434cbb644abb285b0a9a775b1caf810e86e255b5b0c2d168d"}}]} | |
// Which leads me to more questions about why are private keys in the payload | |
const [ COSuffixData, CODelta ] = element.func.decodeJson(createPayload.payload)); | |
const did = `did:elem:ropsten:${didUniqueSuffix}?initial-state=${COSuffixData}.${CODelta}`; | |
return did; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment