|
| 1 | +import Fs from "node:fs/promises"; |
| 2 | + |
| 3 | +import DashHd from "dashhd"; |
| 4 | +import * as DashHdUtils from "./src/dashhd-utils.ts"; |
| 5 | +import DashKeys from "dashkeys"; |
| 6 | +import * as DashTx from "dashtx"; |
| 7 | +import * as DashPlatform from "./src/dashplatform.js"; |
| 8 | +import * as QRCode from "./src/_qr.js"; |
| 9 | +import * as KeyUtils from "./src/key-utils.js"; |
| 10 | + |
| 11 | +import { |
| 12 | + createIdentityFromAssetLock, |
| 13 | +} from "./2-create-identity-transition.ts"; |
| 14 | + |
| 15 | +import { loadWallet } from "./src/cli.ts"; |
| 16 | +import { deriveAllCreateIdentityKeys } from "./src/asset_lock.ts"; |
| 17 | +import { createPlatformAssetLock } from "./src/asset_lock.ts"; |
| 18 | +import { connectToNode, TRPC } from "./src/rpc.ts" |
| 19 | +import { NODE_ADDRESS, RPC_AUTH_URL } from "./src/constants.ts" |
| 20 | +import { fromHex, toHex } from "./src/hex.js" |
| 21 | +import * as BinCode from "./src/bincode.ts" |
| 22 | +import * as DashBincode from "./2.0.0/generated_bincode.js" |
| 23 | +import { base58 } from "./src/util/base58.ts" |
| 24 | +import { findExistingIdentity } from "./src/identity.ts" |
| 25 | + |
| 26 | +async function main(): Promise<void> { |
| 27 | + const rpc = new TRPC(RPC_AUTH_URL); |
| 28 | + const nodeRpc = connectToNode(NODE_ADDRESS) |
| 29 | + |
| 30 | + const walletKey = await loadWallet(); |
| 31 | + |
| 32 | + const identityIndex = parseInt(process.argv[2], 10); |
| 33 | + if (isNaN(identityIndex)) { |
| 34 | + console.error(""); |
| 35 | + console.error("USAGE"); |
| 36 | + console.error(` ${process.argv[0]} ${process.argv[1]} <identity-index>`); |
| 37 | + console.error(""); |
| 38 | + console.error("EXAMPLE"); |
| 39 | + console.error(` ${process.argv[0]} ${process.argv[1]} 0`); |
| 40 | + console.error(""); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | + |
| 44 | + const hdOpts = { version: "testnet" as const }; // TODO |
| 45 | + |
| 46 | + const { |
| 47 | + regFundKey, |
| 48 | + changeKey, |
| 49 | + assetKey, |
| 50 | + masterKey, |
| 51 | + otherKey, |
| 52 | + } = await deriveAllCreateIdentityKeys( |
| 53 | + hdOpts, |
| 54 | + walletKey, |
| 55 | + identityIndex, |
| 56 | + ); |
| 57 | + |
| 58 | + // console.log("Asset WIF", assetWif, "(would be ephemeral, non-hd)"); |
| 59 | + |
| 60 | + console.log( |
| 61 | + "regFundKey", |
| 62 | + await DashHd.toAddr(regFundKey.publicKey, { version: "testnet" }), |
| 63 | + ); |
| 64 | + console.log( |
| 65 | + "changeKey", |
| 66 | + await DashHd.toAddr(changeKey.publicKey, { version: "testnet" }), |
| 67 | + ); |
| 68 | + console.log( |
| 69 | + "assetKey", |
| 70 | + await DashHd.toAddr(assetKey.publicKey, { version: "testnet" }), |
| 71 | + ); |
| 72 | + console.log( |
| 73 | + "masterKey", |
| 74 | + await DashHd.toAddr(masterKey.publicKey, { version: "testnet" }), |
| 75 | + ); |
| 76 | + console.log( |
| 77 | + "masterKey priv wif", |
| 78 | + await DashHd.toWif(masterKey.privateKey!, { version: "testnet" }), |
| 79 | + ); |
| 80 | + console.log( |
| 81 | + "otherKey", |
| 82 | + await DashHd.toAddr(otherKey.publicKey, { version: "testnet" }), |
| 83 | + ); |
| 84 | + console.log(); |
| 85 | + |
| 86 | + const pkh = await DashKeys.pubkeyToPkh(masterKey.publicKey) |
| 87 | + console.log('masterKey pkh', toHex(pkh)) |
| 88 | + |
| 89 | + const existingIdentityId = await findExistingIdentity(nodeRpc, pkh) |
| 90 | + if (existingIdentityId) { |
| 91 | + console.log('Identity Already Created!', base58.encode(existingIdentityId)) |
| 92 | + process.exit(1); |
| 93 | + } |
| 94 | + |
| 95 | + const { |
| 96 | + identityId, |
| 97 | + txidHex, |
| 98 | + assetProof, |
| 99 | + } = await createPlatformAssetLock(hdOpts, regFundKey, changeKey, assetKey, rpc); |
| 100 | + |
| 101 | + console.log(); |
| 102 | + console.log(`txidHex: `, txidHex); |
| 103 | + console.log(`identityId:`, base58.encode(identityId)); |
| 104 | + console.log(`assetProof:`, assetProof); |
| 105 | + |
| 106 | + await Fs.writeFile( |
| 107 | + "ready-to-create-identity.json", |
| 108 | + JSON.stringify( |
| 109 | + { |
| 110 | + assetKey, |
| 111 | + masterKey, |
| 112 | + otherKey, |
| 113 | + identityId: toHex(identityId), |
| 114 | + txidHex, |
| 115 | + assetProof, |
| 116 | + }, |
| 117 | + (key, val) => { |
| 118 | + if (val instanceof Uint8Array || val instanceof ArrayBuffer) { |
| 119 | + return { |
| 120 | + "@Uint8Array hex": DashTx.utils.bytesToHex(new Uint8Array(val)), |
| 121 | + }; |
| 122 | + } |
| 123 | + return val; |
| 124 | + }, |
| 125 | + 2, |
| 126 | + ), |
| 127 | + ); |
| 128 | + |
| 129 | + await createIdentityFromAssetLock( |
| 130 | + assetKey, |
| 131 | + masterKey, |
| 132 | + otherKey, |
| 133 | + identityId, |
| 134 | + assetProof, |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +main(); |
0 commit comments