-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP * WIP: tests * Implement `Accumulator` module for `cheqd` * Tweaks * Fix docs * Lints * Tweak * Tweaks * Renamings * Fix import * Remove outdated test * Test corrections
- Loading branch information
Showing
55 changed files
with
2,372 additions
and
822 deletions.
There are no files selected for viewing
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
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
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
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
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
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
224 changes: 224 additions & 0 deletions
224
packages/cheqd-blockchain-modules/src/accumulator/internal.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
import { | ||
CheqdStoredAccumulator, | ||
AccumulatorParams, | ||
CheqdAccumulatorId, | ||
CheqdAccumulatorParamsRef, | ||
CheqdAccumulatorWithUpdateInfo, | ||
CheqdCreateResource, | ||
CheqdAccumulatorPublicKey, | ||
} from '@docknetwork/credential-sdk/types'; | ||
import { TypedUUID, option } from '@docknetwork/credential-sdk/types/generic'; | ||
import { stringToU8a, u8aToString } from '@docknetwork/credential-sdk/utils'; | ||
import { VBWitnessUpdateInfo } from '@docknetwork/credential-sdk/crypto'; | ||
import { | ||
injectParams, | ||
injectPublicKeys, | ||
createInternalCheqdModule, | ||
SortedResourceVersions, | ||
validateResource, | ||
} from '../common'; | ||
|
||
const Type = 'accumulator'; | ||
|
||
const methods = { | ||
addAccumulator: (accumulatorId, accumulator) => { | ||
const [did, id] = CheqdAccumulatorId.from(accumulatorId).value; | ||
const storedAcc = new CheqdStoredAccumulator( | ||
accumulator, | ||
).toJSONStringBytes(); | ||
|
||
return new CheqdCreateResource( | ||
did.value.value, | ||
TypedUUID.random(), | ||
'1.0', | ||
[], | ||
String(id), | ||
Type, | ||
storedAcc, | ||
); | ||
}, | ||
updateAccumulator: ( | ||
accumulatorId, | ||
accumulator, | ||
{ additions, removals, witnessUpdateInfo }, | ||
) => { | ||
const [did, id] = CheqdAccumulatorId.from(accumulatorId).value; | ||
const storedAcc = new CheqdStoredAccumulator( | ||
accumulator, | ||
additions, | ||
removals, | ||
witnessUpdateInfo, | ||
).toJSONStringBytes(); | ||
|
||
return new CheqdCreateResource( | ||
did.value.value, | ||
TypedUUID.random(), | ||
'1.0', | ||
[], | ||
String(id), | ||
Type, | ||
storedAcc, | ||
); | ||
}, | ||
removeAccumulator: (accumulatorId) => { | ||
const [did, id] = CheqdAccumulatorId.from(accumulatorId).value; | ||
|
||
return new CheqdCreateResource( | ||
did.value.value, | ||
TypedUUID.random(), | ||
'1.0', | ||
[], | ||
String(id), | ||
Type, | ||
stringToU8a('null'), | ||
); | ||
}, | ||
}; | ||
|
||
export default class CheqdInternalAccumulatorModule extends injectParams( | ||
injectPublicKeys(createInternalCheqdModule(methods)), | ||
) { | ||
static get MsgNames() { | ||
return { | ||
...super.MsgNames, | ||
addAccumulator: 'MsgCreateResource', | ||
updateAccumulator: 'MsgCreateResource', | ||
removeAccumulator: 'MsgCreateResource', | ||
}; | ||
} | ||
|
||
static PublicKey = CheqdAccumulatorPublicKey; | ||
|
||
static PublicKeyName = 'AccumulatorPublicKey'; | ||
|
||
static PublicKeyType = 'accumulator-public-key'; | ||
|
||
static Params = AccumulatorParams; | ||
|
||
static ParamsName = 'AccumulatorParams'; | ||
|
||
static ParamsType = 'accumulator-params'; | ||
|
||
static ParamsRef = CheqdAccumulatorParamsRef; | ||
|
||
createAccumulatorMetadataFilter(name) { | ||
const strName = String(name); | ||
|
||
return (meta) => meta.resourceType === 'accumulator' && meta.name === strName; | ||
} | ||
|
||
async accumulator(accumulatorId) { | ||
const [did, name] = CheqdAccumulatorId.from(accumulatorId).value; | ||
const ids = new SortedResourceVersions( | ||
await this.resourcesMetadataBy( | ||
did, | ||
this.createAccumulatorMetadataFilter(name), | ||
), | ||
).ids(); | ||
|
||
if (!ids.length) { | ||
return null; | ||
} | ||
|
||
const acc = option(CheqdStoredAccumulator).from( | ||
JSON.parse( | ||
u8aToString( | ||
validateResource( | ||
await this.resource(did, ids[ids.length - 1]), | ||
String(name), | ||
Type, | ||
), | ||
), | ||
), | ||
); | ||
|
||
if (acc == null) { | ||
return null; | ||
} | ||
|
||
return new CheqdAccumulatorWithUpdateInfo( | ||
ids[0], | ||
ids[ids.length - 1], | ||
acc.accumulator, | ||
); | ||
} | ||
|
||
async lastParamsId(did) { | ||
const res = await this.latestResourceMetadataBy( | ||
did, | ||
this.filterParamsMetadata, | ||
); | ||
|
||
return res?.id; | ||
} | ||
|
||
async lastPublicKeyId(did) { | ||
const res = await this.latestResourceMetadataBy( | ||
did, | ||
this.filterPublicKeyMetadata, | ||
); | ||
|
||
return res?.id; | ||
} | ||
|
||
/** | ||
* Update given witness by downloading necessary blocks and applying the updates if found. Both start and end are inclusive | ||
* @param accumulatorId | ||
* @param member | ||
* @param witness - this will be updated to the latest witness | ||
* @param start - accumulator version id to start from | ||
* @param end - accumulator version id to end at | ||
* @returns {Promise<void>} | ||
*/ | ||
// eslint-disable-next-line sonarjs/cognitive-complexity | ||
async updateVbAccumulatorWitnessFromUpdatesInBlocks( | ||
accumulatorId, | ||
member, | ||
witness, | ||
start, | ||
end, | ||
) { | ||
const [did, name] = CheqdAccumulatorId.from(accumulatorId).value; | ||
const startUUID = String(TypedUUID.from(start)); | ||
const endUUID = String(TypedUUID.from(end)); | ||
|
||
const sortedIDs = new SortedResourceVersions( | ||
await this.resourcesMetadataBy( | ||
did, | ||
this.createAccumulatorMetadataFilter(name), | ||
), | ||
).ids(); | ||
|
||
const startIdx = sortedIDs.findIndex((id) => id === startUUID); | ||
let endIdx; | ||
if (end != null) { | ||
endIdx = sortedIDs.findIndex((id) => id === endUUID); | ||
|
||
if (endIdx === -1) { | ||
throw new Error( | ||
`Accumulator \`${accumulatorId}\` with version \`${end}\` doesn't exist`, | ||
); | ||
} | ||
} else { | ||
endIdx = sortedIDs.length - 1; | ||
} | ||
|
||
const accumulators = await this.resources( | ||
did, | ||
sortedIDs.slice(startIdx, endIdx + 1), | ||
); | ||
|
||
for (const accumulator of new SortedResourceVersions(accumulators)) { | ||
const { additions, removals, witnessUpdateInfo } = CheqdStoredAccumulator.from( | ||
validateResource(accumulator, String(name), Type), | ||
); | ||
|
||
witness.updateUsingPublicInfoPostBatchUpdate( | ||
member, | ||
additions ? [...additions].map((addition) => addition.bytes) : [], | ||
removals ? [...removals].map((removal) => removal.bytes) : [], | ||
new VBWitnessUpdateInfo(witnessUpdateInfo?.bytes || new Uint8Array()), | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.