|
| 1 | +// See https://dashplatform.readme.io/docs/tutorial-update-an-identity |
| 2 | +const Dash = require('dash'); |
| 3 | +const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); |
| 4 | +const dotenv = require('dotenv'); |
| 5 | +dotenv.config(); |
| 6 | + |
| 7 | +const clientOpts = { |
| 8 | + network: process.env.NETWORK, |
| 9 | + wallet: { |
| 10 | + mnemonic: process.env.MNEMONIC, // A Dash wallet mnemonic with testnet funds |
| 11 | + unsafeOptions: { |
| 12 | + skipSynchronizationBeforeHeight: 675000, // only sync from early-2022 |
| 13 | + }, |
| 14 | + }, |
| 15 | +}; |
| 16 | +const client = new Dash.Client(clientOpts); |
| 17 | + |
| 18 | +const updateIdentityAddKey = async () => { |
| 19 | + const identityId = process.env.IDENTITY_ID; |
| 20 | + const existingIdentity = await client.platform.identities.get(identityId); |
| 21 | + const newKeyId = existingIdentity.toJSON().publicKeys.length; |
| 22 | + |
| 23 | + // Get an unused identity index |
| 24 | + const account = await client.platform.client.getWalletAccount(); |
| 25 | + const identityIndex = await account.getUnusedIdentityIndex(); |
| 26 | + |
| 27 | + // Get unused private key and construct new identity public key |
| 28 | + const { privateKey: identityPrivateKey } = |
| 29 | + account.identities.getIdentityHDKeyByIndex(identityIndex, 0); |
| 30 | + |
| 31 | + const identityPublicKey = identityPrivateKey.toPublicKey().toBuffer(); |
| 32 | + |
| 33 | + const newPublicKey = new IdentityPublicKey({ |
| 34 | + id: newKeyId, |
| 35 | + type: IdentityPublicKey.TYPES.ECDSA_SECP256K1, |
| 36 | + purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, |
| 37 | + securityLevel: IdentityPublicKey.SECURITY_LEVELS.HIGH, |
| 38 | + data: identityPublicKey, |
| 39 | + readOnly: false, |
| 40 | + }); |
| 41 | + |
| 42 | + const updateAdd = { |
| 43 | + add: [newPublicKey], |
| 44 | + }; |
| 45 | + |
| 46 | + // Submit the update signed with the new key |
| 47 | + await client.platform.identities.update(existingIdentity, updateAdd, { |
| 48 | + [newPublicKey.getId()]: identityPrivateKey, |
| 49 | + }); |
| 50 | + |
| 51 | + return client.platform.identities.get(identityId); |
| 52 | +}; |
| 53 | + |
| 54 | +updateIdentityAddKey() |
| 55 | + .then((d) => console.log('Identity updated:\n', d.toJSON())) |
| 56 | + .catch((e) => console.error('Something went wrong:\n', e)) |
| 57 | + .finally(() => client.disconnect()); |
0 commit comments