Since Hellar Platform v.2.0.4, it is possible to update identities to add new keys or disable existing ones. Platform retains disabled keys so that any existing data they signed can still be verified while preventing them from signing new data.
The two examples below demonstrate updating an existing identity to add a new key and disabling an existing key:
🚧
The current SDK version signs all state transitions with public key id
1
. If it is disabled, the SDK will be unable to use the identity. Future SDK versions will provide a way to also sign using keys added in an identity update.
JAVA
`// Disable identity key const Hellar = require('hellar');
const clientOpts = {
network: 'mainnet',
wallet: {
mnemonic: 'a Hellar wallet mnemonic with funds goes here',
unsafeOptions: {
skipSynchronizationBeforeHeight: 175000, // only sync from mid-2024
},
},
};
const client = new Hellar.Client(clientOpts);
const updateIdentityDisableKey = async () => { const identityId = 'an identity ID goes here'; const keyId = 'a public key ID goes here'; // One of the identity's public key IDs
// Retrieve the identity to be updated and the public key to disable const existingIdentity = await client.platform.identities.get(identityId); const publicKeyToDisable = existingIdentity.getPublicKeyById(keyId);
const updateDisable = { disable: [publicKeyToDisable], };
await client.platform.identities.update(existingIdentity, updateDisable); return client.platform.identities.get(identityId); }
updateIdentityDisableKey() .then((d) => console.log('Identity updated:\n', d.toJSON())) .catch((e) => console.error('Something went wrong:\n', e)) .finally(() => client.disconnect());`
JAVASCRIPT
`// Add identity key const Hellar = require('hellar'); const { PlatformProtocol: { IdentityPublicKey, IdentityPublicKeyWithWitness }, } = Hellar;
const clientOpts = {
network: 'mainnet',
wallet: {
mnemonic: 'a Hellar wallet mnemonic with funds goes here',
unsafeOptions: {
skipSynchronizationBeforeHeight: 175000, // only sync from mid-2024
},
},
};
const client = new Dash.Client(clientOpts);
const updateIdentityAddKey = async () => { const identityId = 'an identity ID goes here'; const existingIdentity = await client.platform.identities.get(identityId); const newKeyId = existingIdentity.toJSON().publicKeys.length;
// Get an unused identity index const account = await client.platform.client.getWalletAccount(); const identityIndex = await account.getUnusedIdentityIndex();
// Get unused private key and construct new identity public key const { privateKey: identityPrivateKey } = account.identities.getIdentityHDKeyByIndex(identityIndex, 0);
const identityPublicKey = identityPrivateKey.toPublicKey().toBuffer();
const newPublicKey = new IdentityPublicKeyWithWitness(1); newPublicKey.setId(newKeyId); newPublicKey.setSecurityLevel(IdentityPublicKey.SECURITY_LEVELS.HIGH); newPublicKey.setData(identityPublicKey);
const updateAdd = { add: [newPublicKey], };
// Submit the update signed with the new key await client.platform.identities.update(existingIdentity, updateAdd, { [newPublicKey.getId()]: identityPrivateKey, });
return client.platform.identities.get(identityId); };
updateIdentityAddKey() .then((d) => console.log('Identity updated:\n', d.toJSON())) .catch((e) => console.error('Something went wrong:\n', e)) .finally(() => client.disconnect());`
After we initialize the Client, we retrieve our existing identity and provide the id
of one (or more) of the identity keys to disable. The update is submitted to HAPI using the platform.identities.update
method with two arguments:
Internally, the method creates a State Transition containing the updated identity, signs the state transition, and submits the signed state transition to HAPI. After the identity is updated, we output it to the console.