Skip to content

Commit ef733d7

Browse files
committed
merge and rename the steps files to make more sense, fixed bugs
1 parent 87b8191 commit ef733d7

File tree

6 files changed

+265
-420
lines changed

6 files changed

+265
-420
lines changed

1-create-asset-lock.ts

Lines changed: 0 additions & 142 deletions
This file was deleted.

1-create-identity.ts

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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)}`);

3-data-contract.ts renamed to 2-create-data-contract.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ let {
4141
otherKey,
4242
} = await deriveAllCreateIdentityKeys(hdOpts, walletKey, identityIndex);
4343

44-
console.log('masterKey hex', toHex(masterKey.publicKey))
45-
console.log('otherKey hex', toHex(otherKey.publicKey))
44+
// console.log('masterKey hex', toHex(masterKey.publicKey))
45+
// console.log('otherKey hex', toHex(otherKey.publicKey))
4646

47-
console.log('masterKey pkh', toHex(await DashKeys.pubkeyToPkh(masterKey.publicKey)))
48-
console.log('otherKey pkh', toHex(await DashKeys.pubkeyToPkh(otherKey.publicKey)))
47+
// console.log('masterKey pkh', toHex(await DashKeys.pubkeyToPkh(masterKey.publicKey)))
48+
// console.log('otherKey pkh', toHex(await DashKeys.pubkeyToPkh(otherKey.publicKey)))
4949

5050
const pkh = await DashKeys.pubkeyToPkh(masterKey.publicKey)
5151
const existingIdentity = await findExistingIdentity(nodeRpc, pkh)
@@ -106,8 +106,6 @@ documentSchemas.set("note", DashBincode.Value.Map([
106106
const newContractIdBuffer = new Uint8Array(owner_id.length + 8);
107107
newContractIdBuffer.set(owner_id, 0);
108108
new DataView(newContractIdBuffer.buffer).setBigUint64(owner_id.length, identity_nonce);
109-
console.log('owner_id', toHex(owner_id))
110-
console.log('newContractIdBuffer', toHex(newContractIdBuffer))
111109
const newContractID = await doubleSha256(newContractIdBuffer)
112110
const newContractIDStr = base58.encode(newContractID)
113111
console.log('newContractID', newContractIDStr)
@@ -154,16 +152,14 @@ const stateTransition = DashBincode.StateTransition.DataContractCreate(
154152
}
155153

156154
const signedBytes = new Uint8Array(Bincode.encode(DashBincode.StateTransition, stateTransition));
157-
158-
console.log("Signed")
159-
console.log(toHex(signedBytes))
155+
const transitionHash = await KeyUtils.sha256(signedBytes);
160156

161157
console.log("Broadcasting Data Contract Create Transition...")
162158
try {
163159
const response = await nodeRpc.platform.broadcastStateTransition({
164160
stateTransition: signedBytes,
165161
})
166-
console.log('response', response);
162+
console.log('response', response.status, response.response);
167163
await Fs.writeFile('data-contract-' + newContractIDStr.slice(0, 6) + '.json', JSON.stringify({id: newContractIDStr}));
168164

169165
} catch (e) {
@@ -172,4 +168,5 @@ try {
172168

173169
console.log();
174170
console.log('New Contract ID:', newContractIDStr)
175-
console.log("https://testnet.platform-explorer.com/document/" + newContractIDStr)
171+
console.log("https://testnet.platform-explorer.com/dataContract/" + newContractIDStr)
172+
console.log(`https://testnet.platform-explorer.com/transaction/${toHex(transitionHash)}`);

0 commit comments

Comments
 (0)