Skip to content

Commit 87b8191

Browse files
committed
Updates to all, and then making Document create work
1 parent 3f28912 commit 87b8191

File tree

4 files changed

+205
-115
lines changed

4 files changed

+205
-115
lines changed

1-create-asset-lock.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Fs from "node:fs/promises";
22

33
import DashHd from "dashhd";
44
import * as DashHdUtils from "./src/dashhd-utils.ts";
5+
import * as Bincode from "./src/bincode.ts";
56
import DashKeys from "dashkeys";
67
import * as DashTx from "dashtx";
78
import * as DashPlatform from "./src/dashplatform.js";
@@ -85,9 +86,12 @@ async function main(): Promise<void> {
8586

8687
const pkh = await DashKeys.pubkeyToPkh(masterKey.publicKey)
8788
console.log('masterKey pkh', toHex(pkh))
88-
89-
const existingIdentityId = await findExistingIdentity(nodeRpc, pkh)
90-
if (existingIdentityId) {
89+
const existingIdentity = await findExistingIdentity(nodeRpc, pkh)
90+
if (existingIdentity) {
91+
const existingIdentityV0: DashBincode.IdentityV0 = Bincode.match(existingIdentity, {
92+
V0: i => i[0]
93+
});
94+
const existingIdentityId = existingIdentityV0.id[0][0];
9195
console.log('Identity Already Created!', base58.encode(existingIdentityId))
9296
process.exit(1);
9397
}

2-create-identity-transition.ts

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as KeyUtils from "./src/key-utils.js";
55
import base64 from "base64-js";
66
import { base58 } from "./src/util/base58.ts";
77
import type { HDKey } from "dashhd"
8+
import { connectToNode } from "./src/rpc.ts"
9+
import { NODE_ADDRESS } from "./src/constants.ts"
810

911
export interface AssetLockChainProof {
1012
core_chain_locked_height: number;
@@ -41,7 +43,7 @@ export interface STKey {
4143
readOnly: boolean;
4244
}
4345

44-
// --- Main Function ---
46+
const nodeRpc = connectToNode(NODE_ADDRESS);
4547

4648
/**
4749
*
@@ -87,25 +89,18 @@ export async function createIdentityFromAssetLock(
8789
console.log(`stKeys:`);
8890
console.log(stKeys);
8991

90-
const nullSigTransition = new Uint8Array(
92+
const signableTransition = new Uint8Array(
9193
Bincode.encode(DashBincode.StateTransition, stateTransition, {
9294
signable: true,
9395
}),
9496
);
95-
console.log();
96-
console.log(`nullSigTransition (ready-to-sign by identity keys):`);
97-
console.log("(hex)", DashTx.utils.bytesToHex(nullSigTransition));
98-
console.log("(base64)", base64.fromByteArray(nullSigTransition));
9997

100-
const nullSigMagicHash = await KeyUtils.doubleSha256(nullSigTransition);
98+
const signableTransitionHash = await KeyUtils.doubleSha256(signableTransition);
10199

102-
if (!assetKey.privateKey) {
103-
throw new Error("'assetKey' is missing 'privateKey'");
104-
}
105100
{
106101
const magicSigBytes = await KeyUtils.magicSign({
107-
privKeyBytes: assetKey.privateKey,
108-
doubleSha256Bytes: nullSigMagicHash,
102+
privKeyBytes: assetKey.privateKey!,
103+
doubleSha256Bytes: signableTransitionHash,
109104
});
110105

111106
identityCreate.signature[0] = magicSigBytes;
@@ -116,7 +111,7 @@ export async function createIdentityFromAssetLock(
116111
const stPub = identityCreate.public_keys[i];
117112
const magicSigBytes = await KeyUtils.magicSign({
118113
privKeyBytes: key.privateKey,
119-
doubleSha256Bytes: nullSigMagicHash,
114+
doubleSha256Bytes: signableTransitionHash,
120115
});
121116

122117
Bincode.match(stPub, {
@@ -144,25 +139,31 @@ export async function createIdentityFromAssetLock(
144139

145140
let grpcTransition = "";
146141
let transitionHashHex = "";
147-
{
148-
const fullSigTransition = new Uint8Array(
149-
Bincode.encode(DashBincode.StateTransition, stateTransition, {
150-
signable: false,
151-
}),
152-
);
153-
console.log();
154-
console.log(`transition (fully signed):`);
155-
console.log(DashTx.utils.bytesToHex(fullSigTransition));
156-
const transitionHash = await KeyUtils.sha256(fullSigTransition);
157-
transitionHashHex = DashTx.utils.bytesToHex(transitionHash);
158-
grpcTransition = base64.fromByteArray(fullSigTransition);
142+
143+
const fullSigTransition = new Uint8Array(
144+
Bincode.encode(DashBincode.StateTransition, stateTransition, {
145+
signable: false,
146+
}),
147+
);
148+
console.log();
149+
console.log(`transition (fully signed):`);
150+
console.log(DashTx.utils.bytesToHex(fullSigTransition));
151+
const transitionHash = await KeyUtils.sha256(fullSigTransition);
152+
transitionHashHex = DashTx.utils.bytesToHex(transitionHash);
153+
grpcTransition = base64.fromByteArray(fullSigTransition);
154+
155+
156+
157+
console.log("Broadcasting Identity Create Transition...")
158+
try {
159+
const response = await nodeRpc.platform.broadcastStateTransition({
160+
stateTransition: fullSigTransition,
161+
})
162+
console.log('response', response);
163+
} catch (e) {
164+
console.error("Error: ", decodeURIComponent((e as any).message))
159165
}
160166

161-
console.log();
162-
console.log();
163-
console.log(`grpcurl -plaintext -d '{
164-
"stateTransition": "${grpcTransition}"
165-
}' seed-2.testnet.networks.dash.org:1443 org.dash.platform.dapi.v0.Platform.broadcastStateTransition`);
166167
console.log();
167168
const identity = base58.encode(identityId);
168169
console.log(`https://testnet.platform-explorer.com/identity/${identity}`);

3-data-contract.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import Fs from "node:fs/promises";
22

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";
3+
import { doubleSha256 } from "dashtx"
4+
import DashKeys from "dashkeys"
85
import * as Bincode from "./src/bincode.ts";
96
import * as DashBincode from "./2.0.0/generated_bincode.js";
10-
import * as QRCode from "./src/_qr.js";
117
import * as KeyUtils from "./src/key-utils.js";
128

139
import { loadWallet } from "./src/cli.ts"
@@ -16,9 +12,7 @@ import { toHex } from "./src/hex.js"
1612
import { connectToNode } from "./src/rpc.ts"
1713
import { NODE_ADDRESS } from "./src/constants.ts"
1814
import { findExistingIdentity } from "./src/identity.ts"
19-
import DashKeys from "dashkeys"
2015
import { base58 } from "./src/util/base58.ts"
21-
import { doubleSha256 } from "dashtx"
2216

2317
const nodeRpc = connectToNode(NODE_ADDRESS);
2418

@@ -73,8 +67,8 @@ const identityNonceResponse = await nodeRpc.platform.getIdentityNonce({version:
7367
prove: false,
7468
}
7569
}})
76-
console.log('identityNonceResponse', identityNonceResponse)
77-
console.log('identityNonceResponse', identityNonceResponse.response.version.v0.result)
70+
// console.log('identityNonceResponse', identityNonceResponse)
71+
// console.log('identityNonceResponse', identityNonceResponse.response.version.v0.result)
7872

7973
let current_identity_nonce = 0n;
8074
if (identityNonceResponse.response.version.oneofKind === 'v0') {
@@ -93,7 +87,7 @@ console.log('Identity:', base58.encode(existingIdentityV0.id[0][0]))
9387
console.log(existingIdentityV0)
9488

9589
const owner_id = existingIdentityId;
96-
const identity_nonce = current_identity_nonce + 1n; // TODO: Where is this supposed to come from??
90+
const identity_nonce = current_identity_nonce + 1n;
9791

9892
// TODO: check to see if a data contract has already been created
9993

@@ -175,3 +169,7 @@ try {
175169
} catch (e) {
176170
console.error("Error: ", decodeURIComponent((e as any).message))
177171
}
172+
173+
console.log();
174+
console.log('New Contract ID:', newContractIDStr)
175+
console.log("https://testnet.platform-explorer.com/document/" + newContractIDStr)

0 commit comments

Comments
 (0)