|
| 1 | +import Fs from "node:fs/promises"; |
| 2 | + |
| 3 | +import DashHd from "dashhd"; |
| 4 | +import * as DashHdUtils from "./src/dashhd-utils.ts"; |
| 5 | +import * as Bincode from "./src/bincode.ts"; |
| 6 | +import DashKeys from "dashkeys"; |
| 7 | +import * as DashTx from "dashtx"; |
| 8 | +import * as DashPlatform from "./src/dashplatform.js"; |
| 9 | +import * as QRCode from "./src/_qr.js"; |
| 10 | +import * as KeyUtils from "./src/key-utils.js"; |
| 11 | +import base64 from "base64-js"; |
| 12 | + |
| 13 | +import { loadWallet } from "./src/cli.ts"; |
| 14 | +import { deriveAllCreateIdentityKeys } from "./src/asset_lock.ts"; |
| 15 | +import { createPlatformAssetLock } from "./src/asset_lock.ts"; |
| 16 | +import { connectToNode, TRPC } from "./src/rpc.ts" |
| 17 | +import { NODE_ADDRESS, RPC_AUTH_URL } from "./src/constants.ts" |
| 18 | +import { fromHex, toHex } from "./src/hex.js" |
| 19 | +import * as BinCode from "./src/bincode.ts" |
| 20 | +import * as DashBincode from "./2.0.0/generated_bincode.js" |
| 21 | +import { base58 } from "./src/util/base58.ts" |
| 22 | +import { findExistingIdentity } from "./src/identity.ts" |
| 23 | + |
| 24 | +const rpc = new TRPC(RPC_AUTH_URL); |
| 25 | +const nodeRpc = connectToNode(NODE_ADDRESS) |
| 26 | + |
| 27 | +const walletKey = await loadWallet(); |
| 28 | + |
| 29 | +const identityIndex = parseInt(process.argv[2], 10); |
| 30 | +if (isNaN(identityIndex)) { |
| 31 | + console.error(""); |
| 32 | + console.error("USAGE"); |
| 33 | + console.error(` ${process.argv[0]} ${process.argv[1]} <identity-index>`); |
| 34 | + console.error(""); |
| 35 | + console.error("EXAMPLE"); |
| 36 | + console.error(` ${process.argv[0]} ${process.argv[1]} 0`); |
| 37 | + console.error(""); |
| 38 | + process.exit(1); |
| 39 | +} |
| 40 | + |
| 41 | +const hdOpts = { version: "testnet" as const }; // TODO |
| 42 | + |
| 43 | +const { |
| 44 | + regFundKey, |
| 45 | + changeKey, |
| 46 | + assetKey, |
| 47 | + masterKey, |
| 48 | + otherKey, |
| 49 | +} = await deriveAllCreateIdentityKeys( |
| 50 | + hdOpts, |
| 51 | + walletKey, |
| 52 | + identityIndex, |
| 53 | +); |
| 54 | + |
| 55 | +// console.log("Asset WIF", assetWif, "(would be ephemeral, non-hd)"); |
| 56 | + |
| 57 | +// console.log( |
| 58 | +// "regFundKey", |
| 59 | +// await DashHd.toAddr(regFundKey.publicKey, { version: "testnet" }), |
| 60 | +// ); |
| 61 | +// console.log( |
| 62 | +// "changeKey", |
| 63 | +// await DashHd.toAddr(changeKey.publicKey, { version: "testnet" }), |
| 64 | +// ); |
| 65 | +// console.log( |
| 66 | +// "assetKey", |
| 67 | +// await DashHd.toAddr(assetKey.publicKey, { version: "testnet" }), |
| 68 | +// ); |
| 69 | +// console.log( |
| 70 | +// "masterKey", |
| 71 | +// await DashHd.toAddr(masterKey.publicKey, { version: "testnet" }), |
| 72 | +// ); |
| 73 | +// console.log( |
| 74 | +// "masterKey priv wif", |
| 75 | +// await DashHd.toWif(masterKey.privateKey!, { version: "testnet" }), |
| 76 | +// ); |
| 77 | +// console.log( |
| 78 | +// "otherKey", |
| 79 | +// await DashHd.toAddr(otherKey.publicKey, { version: "testnet" }), |
| 80 | +// ); |
| 81 | +// console.log(); |
| 82 | + |
| 83 | +const pkh = await DashKeys.pubkeyToPkh(masterKey.publicKey) |
| 84 | +console.log('masterKey pkh', toHex(pkh)) |
| 85 | +const existingIdentity = await findExistingIdentity(nodeRpc, pkh) |
| 86 | +if (existingIdentity) { |
| 87 | + const existingIdentityV0: DashBincode.IdentityV0 = Bincode.match(existingIdentity, { |
| 88 | + V0: i => i[0] |
| 89 | + }); |
| 90 | + const existingIdentityId = existingIdentityV0.id[0][0]; |
| 91 | + console.log('Identity Already Created!', base58.encode(existingIdentityId)) |
| 92 | + process.exit(1); |
| 93 | +} |
| 94 | + |
| 95 | +const { |
| 96 | + identityId, |
| 97 | + txidHex, |
| 98 | + assetLockProof, |
| 99 | +} = await createPlatformAssetLock(hdOpts, regFundKey, changeKey, assetKey, rpc); |
| 100 | + |
| 101 | +console.log(); |
| 102 | +console.log(`identityId:`, base58.encode(identityId)); |
| 103 | + |
| 104 | + |
| 105 | +const identityKeys = [masterKey, otherKey]; |
| 106 | +const identityPublicKeys: DashBincode.IdentityPublicKeyInCreation[] = [ |
| 107 | + DashBincode.IdentityPublicKeyInCreation.V0( |
| 108 | + DashBincode.IdentityPublicKeyInCreationV0({ |
| 109 | + id: 0, |
| 110 | + key_type: DashBincode.KeyType.ECDSA_SECP256K1(), |
| 111 | + purpose: DashBincode.Purpose.AUTHENTICATION(), |
| 112 | + security_level: DashBincode.SecurityLevel.MASTER(), |
| 113 | + contract_bounds: undefined, |
| 114 | + read_only: false, |
| 115 | + data: DashBincode.BinaryData(masterKey.publicKey), |
| 116 | + signature: DashBincode.BinaryData(new Uint8Array()), |
| 117 | + }), |
| 118 | + ), |
| 119 | + DashBincode.IdentityPublicKeyInCreation.V0( |
| 120 | + DashBincode.IdentityPublicKeyInCreationV0({ |
| 121 | + id: 1, |
| 122 | + key_type: DashBincode.KeyType.ECDSA_SECP256K1(), |
| 123 | + purpose: DashBincode.Purpose.AUTHENTICATION(), |
| 124 | + security_level: DashBincode.SecurityLevel.CRITICAL(), |
| 125 | + contract_bounds: undefined, |
| 126 | + read_only: false, |
| 127 | + data: DashBincode.BinaryData(otherKey.publicKey), |
| 128 | + signature: DashBincode.BinaryData(new Uint8Array()), |
| 129 | + }), |
| 130 | + ), |
| 131 | +]; |
| 132 | + |
| 133 | +const identityCreate = DashBincode.IdentityCreateTransitionV0({ |
| 134 | + asset_lock_proof: assetLockProof, |
| 135 | + public_keys: identityPublicKeys, |
| 136 | + identity_id: DashBincode.Identifier( |
| 137 | + DashBincode.IdentifierBytes32(identityId), |
| 138 | + ), |
| 139 | + user_fee_increase: 0, |
| 140 | + signature: DashBincode.BinaryData(new Uint8Array()), |
| 141 | +}); |
| 142 | + |
| 143 | +const stateTransition: DashBincode.StateTransition = |
| 144 | + DashBincode.StateTransition.IdentityCreate( |
| 145 | + DashBincode.IdentityCreateTransition.V0(identityCreate), |
| 146 | + ); |
| 147 | +// console.log(`identityPublicKeys:`, identityPublicKeys); |
| 148 | + |
| 149 | +const signableTransition = new Uint8Array( |
| 150 | + Bincode.encode(DashBincode.StateTransition, stateTransition, { |
| 151 | + signable: true, |
| 152 | + }), |
| 153 | +); |
| 154 | + |
| 155 | +const signableTransitionHash = await KeyUtils.doubleSha256(signableTransition); |
| 156 | + |
| 157 | +{ |
| 158 | + const magicSigBytes = await KeyUtils.magicSign({ |
| 159 | + privKeyBytes: assetKey.privateKey!, |
| 160 | + doubleSha256Bytes: signableTransitionHash, |
| 161 | + }); |
| 162 | + |
| 163 | + identityCreate.signature[0] = magicSigBytes; |
| 164 | +} |
| 165 | + |
| 166 | +for (let i = 0; i < identityKeys.length; i += 1) { |
| 167 | + const key = identityKeys[i]; |
| 168 | + const stPub = identityCreate.public_keys[i]; |
| 169 | + const magicSigBytes = await KeyUtils.magicSign({ |
| 170 | + privKeyBytes: key.privateKey!, |
| 171 | + doubleSha256Bytes: signableTransitionHash, |
| 172 | + }); |
| 173 | + |
| 174 | + Bincode.match(stPub, { |
| 175 | + V0: ({ 0: stPub0 }: { 0: any }) => { |
| 176 | + stPub0.signature[0] = magicSigBytes; |
| 177 | + }, |
| 178 | + }); |
| 179 | +} |
| 180 | + |
| 181 | +const fullSigTransition = new Uint8Array( |
| 182 | + Bincode.encode(DashBincode.StateTransition, stateTransition, { |
| 183 | + signable: false, |
| 184 | + }), |
| 185 | +); |
| 186 | +const transitionHash = await KeyUtils.sha256(fullSigTransition); |
| 187 | + |
| 188 | +console.log("Broadcasting Identity Create Transition...") |
| 189 | +try { |
| 190 | + const response = await nodeRpc.platform.broadcastStateTransition({ |
| 191 | + stateTransition: fullSigTransition, |
| 192 | + }) |
| 193 | + console.log('response', response.status, response.response); |
| 194 | +} catch (e) { |
| 195 | + console.error("Error: ", decodeURIComponent((e as any).message)) |
| 196 | +} |
| 197 | + |
| 198 | +const identity = base58.encode(identityId); |
| 199 | +console.log(); |
| 200 | +console.log(`https://testnet.platform-explorer.com/identity/${identity}`); |
| 201 | +console.log(`https://testnet.platform-explorer.com/transaction/${toHex(transitionHash)}`); |
0 commit comments