Skip to content

Commit 7ff4c1d

Browse files
committed
WIP: code organization and scaffolding out the demo
1 parent fb6ba07 commit 7ff4c1d

9 files changed

+971
-817
lines changed

1-create-asset-lock.js

Lines changed: 19 additions & 693 deletions
Large diffs are not rendered by default.

2-create-identity-transition.js

Lines changed: 17 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Fs from "node:fs/promises";
21

32
// import DashKeys from "dashkeys";
43
import * as DashTx from "dashtx/dashtx.js";
@@ -8,40 +7,6 @@ import * as DashBincode from "./1.8.1/generated_bincode.js";
87
import * as KeyUtils from "./key-utils.js";
98
import baseX from "base-x";
109

11-
const ST_CREATE_IDENTITY = 2;
12-
const L2_VERSION_PLATFORM = 1; // actually constant "0" ??
13-
14-
let KEY_LEVELS = {
15-
0: "MASTER",
16-
1: "CRITICAL",
17-
2: "HIGH",
18-
3: "MEDIUM",
19-
MASTER: 0,
20-
CRITICAL: 1,
21-
HIGH: 2,
22-
MEDIUM: 3,
23-
};
24-
25-
let KEY_PURPOSES = {
26-
0: "AUTHENTICATION",
27-
1: "ENCRYPTION",
28-
2: "DECRYPTION",
29-
3: "TRANSFER",
30-
4: "SYSTEM",
31-
5: "VOTING",
32-
AUTHENTICATION: 0,
33-
ENCRYPTION: 1,
34-
DECRYPTION: 2,
35-
TRANSFER: 3,
36-
SYSTEM: 4,
37-
VOTING: 5,
38-
};
39-
40-
let KEY_TYPES = {
41-
0: "ECDSA_SECP256K1",
42-
ECDSA_SECP256K1: 0,
43-
};
44-
4510
const BASE58 = `123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`;
4611
let base58 = baseX(BASE58);
4712

@@ -64,7 +29,7 @@ let Thingy = {};
6429
* @param {String} [txlocksigHex]
6530
* @param {import('dashtx').TxInfo} [txCore]
6631
*/
67-
Thingy.doStuff = async function (
32+
export async function createIdentityFromAssetLock(
6833
assetKey,
6934
masterKey,
7035
otherKey,
@@ -79,9 +44,9 @@ Thingy.doStuff = async function (
7944
/** @type {DashBincode.AssetLockProof} */
8045
let assetLockProof;
8146
if (txlocksigHex) {
82-
assetLockProof = await getAssetLockInstantProof(txlocksigHex);
47+
assetLockProof = await makeAssetLockInstantProof(txlocksigHex);
8348
} else {
84-
assetLockProof = await getAssetLockChainProof(txidHex, txCore);
49+
assetLockProof = await makeAssetLockChainProof(txidHex, txCore);
8550
}
8651

8752
if (!masterKey.privateKey) {
@@ -206,10 +171,8 @@ Thingy.doStuff = async function (
206171
);
207172
};
208173

209-
export default Thingy;
210-
211174
/** @param {HexString} txlocksigHex */
212-
async function getAssetLockInstantProof(txlocksigHex) {
175+
async function makeAssetLockInstantProof(txlocksigHex) {
213176
{
214177
let len = txlocksigHex.length / 2;
215178
console.log();
@@ -255,7 +218,7 @@ async function getAssetLockInstantProof(txlocksigHex) {
255218
* @param {HexString} txidHex
256219
* @param {any} txInfo - TODO CoreTx
257220
*/
258-
async function getAssetLockChainProof(txidHex, txInfo) {
221+
async function makeAssetLockChainProof(txidHex, txInfo) {
259222
//@ts-expect-error
260223
let vout = txInfo.vout.findIndex(voutInfo =>
261224
voutInfo.scriptPubKey?.hex === "6a00" // TODO match the burn
@@ -339,16 +302,18 @@ async function getKnownIdentityKeys(masterKey, otherKey) {
339302
function getIdentityTransitionKeys(identityKeys) {
340303
let stKeys = [];
341304
for (let key of identityKeys) {
342-
let stKey = DashBincode.IdentityPublicKeyInCreation.V0(DashBincode.IdentityPublicKeyInCreationV0({
343-
id: key.id,
344-
key_type: key.type,
345-
purpose: key.purpose,
346-
security_level: key.securityLevel,
347-
contract_bounds: undefined,
348-
read_only: key.readOnly || false,
349-
data: DashBincode.BinaryData(key.publicKey),
350-
signature: DashBincode.BinaryData(new Uint8Array),
351-
}));
305+
let stKey = DashBincode.IdentityPublicKeyInCreation.V0(
306+
DashBincode.IdentityPublicKeyInCreationV0({
307+
id: key.id,
308+
key_type: key.type,
309+
purpose: key.purpose,
310+
security_level: key.securityLevel,
311+
contract_bounds: undefined,
312+
read_only: key.readOnly || false,
313+
data: DashBincode.BinaryData(key.publicKey),
314+
signature: DashBincode.BinaryData(new Uint8Array),
315+
})
316+
);
352317
stKeys.push(stKey);
353318
}
354319
return stKeys;
@@ -362,34 +327,6 @@ function bytesToBase64(bytes) {
362327
return btoa(String.fromCharCode.apply(null, bytes));
363328
}
364329

365-
/**
366-
* Reads a hex file as text, stripping comments (anything including and after a non-hex character), removing whitespace, and joining as a single string
367-
* @param {String} path
368-
*/
369-
async function readHex(path) {
370-
let text = await Fs.readFile(path, "utf8");
371-
let lines = text.split("\n");
372-
let hexes = [];
373-
for (let line of lines) {
374-
line = line.replace(/\s/g, "");
375-
line = line.replace(/[^0-9a-f].*/i, "");
376-
hexes.push(line);
377-
}
378-
379-
let hex = hexes.join("");
380-
return hex;
381-
}
382-
383-
/**
384-
* @param {String} path
385-
*/
386-
async function readWif(path) {
387-
let wif = await Fs.readFile(path, "utf8");
388-
wif = wif.trim();
389-
390-
return wif;
391-
}
392-
393330
/** @typedef {String} Base58 */
394331
/** @typedef {String} Base64 */
395332
/** @typedef {String} HexString */

3-data-contract.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import Fs from "node:fs/promises";
2+
3+
import DashHd from "dashhd";
4+
import * as DashHdUtils from "./dashhd-utils.js";
5+
import DashKeys from "dashkeys";
6+
import * as DashTx from "dashtx/dashtx.js";
7+
import * as DashPlatform from "./dashplatform.js";
8+
import * as Bincode from "./bincode.ts";
9+
import * as DashBincode from "./1.8.1/generated_bincode.js";
10+
import * as QRCode from "./_qr.js";
11+
import * as KeyUtils from "./key-utils.js";
12+
13+
import { createIdentityFromAssetLock } from "./2-create-identity-transition.js";
14+
15+
import { loadWallet } from "./cli.js"
16+
import { deriveAllCreateIdentityKeys } from "./asset_lock.js"
17+
18+
async function main() {
19+
const walletKey = await loadWallet();
20+
21+
let identityIndex = parseInt(process.argv[2], 10);
22+
if (isNaN(identityIndex)) {
23+
console.error("");
24+
console.error("USAGE");
25+
console.error(` ${process.argv[0]} ${process.argv[1]} <identity-index>`);
26+
console.error("");
27+
console.error("EXAMPLE");
28+
console.error(` ${process.argv[0]} ${process.argv[1]} 0`);
29+
console.error("");
30+
process.exit(1);
31+
}
32+
33+
let hdOpts = { version: "testnet" }; // TODO
34+
35+
let {
36+
regFundKey,
37+
topupKey, // TODO next change key from wallet
38+
assetWif,
39+
assetInfo,
40+
41+
assetKey,
42+
masterKey,
43+
otherKey,
44+
} = await deriveAllCreateIdentityKeys(hdOpts, walletKey, identityIndex);
45+
46+
/** @type {Map<string, DashBincode.Value} */
47+
const documentSchemas = new Map();
48+
documentSchemas.set("x", DashBincode.Value.Map([
49+
// [DashBincode.Value.Text("key"), DashBincode.Value.Text("value")],
50+
]))
51+
52+
const dataContract = DashBincode.DataContractInSerializationFormatV0({
53+
id: DashBincode.Identifier(DashBincode.IdentifierBytes32(new Uint8Array)), // TODO
54+
config: DashBincode.DataContractConfig.V0(
55+
DashBincode.DataContractConfigV0({
56+
can_be_deleted: false,
57+
readonly: false,
58+
keeps_history: false,
59+
documents_keep_history_contract_default: false,
60+
documents_mutable_contract_default: false,
61+
documents_can_be_deleted_contract_default: false
62+
})
63+
),
64+
version: 0,
65+
owner_id: DashBincode.Identifier(DashBincode.IdentifierBytes32(new Uint8Array)), // TODO
66+
document_schemas: documentSchemas,
67+
// schema_defs: ,
68+
})
69+
70+
const createDataContract = DashBincode.DataContractCreateTransitionV0({
71+
data_contract: DashBincode.DataContractInSerializationFormat.V0(dataContract),
72+
identity_nonce: 0n,
73+
user_fee_increase: 0,
74+
signature_public_key_id: 0,
75+
signature: DashBincode.BinaryData(new Uint8Array),
76+
})
77+
78+
const stateTransition = DashBincode.StateTransition.DataContractCreate(
79+
DashBincode.DataContractCreateTransition.V0(createDataContract));
80+
81+
const signableBytes = Bincode.encode(DashBincode.StateTransition, stateTransition, {signable: true});
82+
83+
84+
}
85+
86+
main();

4-document.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import Fs from "node:fs/promises";
2+
3+
import DashHd from "dashhd";
4+
import * as DashHdUtils from "./dashhd-utils.js";
5+
import DashKeys from "dashkeys";
6+
import * as DashTx from "dashtx/dashtx.js";
7+
import * as DashPlatform from "./dashplatform.js";
8+
import * as Bincode from "./bincode.ts";
9+
import * as DashBincode from "./1.8.1/generated_bincode.js";
10+
import * as QRCode from "./_qr.js";
11+
import * as KeyUtils from "./key-utils.js";
12+
13+
import { createIdentityFromAssetLock } from "./2-create-identity-transition.js";
14+
15+
import { loadWallet } from "./cli.js"
16+
import { deriveAllCreateIdentityKeys } from "./asset_lock.js"
17+
18+
async function main() {
19+
const walletKey = await loadWallet();
20+
21+
let identityIndex = parseInt(process.argv[2], 10);
22+
if (isNaN(identityIndex)) {
23+
console.error("");
24+
console.error("USAGE");
25+
console.error(` ${process.argv[0]} ${process.argv[1]} <identity-index>`);
26+
console.error("");
27+
console.error("EXAMPLE");
28+
console.error(` ${process.argv[0]} ${process.argv[1]} 0`);
29+
console.error("");
30+
process.exit(1);
31+
}
32+
33+
let hdOpts = { version: "testnet" }; // TODO
34+
35+
let {
36+
regFundKey,
37+
topupKey, // TODO next change key from wallet
38+
assetWif,
39+
assetInfo,
40+
41+
assetKey,
42+
masterKey,
43+
otherKey,
44+
} = await deriveAllCreateIdentityKeys(hdOpts, walletKey, identityIndex);
45+
46+
const data: Map<string, DashBincode.Value> = new Map();
47+
48+
const documentCreate = DashBincode.DocumentCreateTransitionV0({
49+
base: DashBincode.DocumentBaseTransition.V0(
50+
DashBincode.DocumentBaseTransitionV0({
51+
id: DashBincode.Identifier(DashBincode.IdentifierBytes32(new Uint8Array)), // TODO
52+
identity_contract_nonce: 0n,
53+
document_type_name: "",
54+
data_contract_id: DashBincode.Identifier(DashBincode.IdentifierBytes32(new Uint8Array)), // TODO
55+
})
56+
),
57+
entropy: new Uint8Array, // TODO
58+
data: data,
59+
})
60+
61+
const documentsBatch = DashBincode.DocumentsBatchTransitionV0({
62+
owner_id: DashBincode.Identifier(DashBincode.IdentifierBytes32(new Uint8Array)), // TODO
63+
transitions: [
64+
DashBincode.DocumentTransition.Create(
65+
DashBincode.DocumentCreateTransition.V0(documentCreate)
66+
)
67+
],
68+
user_fee_increase: 0,
69+
signature_public_key_id: 0,
70+
signature: DashBincode.BinaryData(new Uint8Array), // TODO
71+
})
72+
73+
const stateTransition = DashBincode.StateTransition.DocumentsBatch(
74+
DashBincode.DocumentsBatchTransition.V0(documentsBatch));
75+
76+
const signableBytes = Bincode.encode(DashBincode.StateTransition, stateTransition, {signable: true});
77+
78+
79+
}
80+
81+
main();

0 commit comments

Comments
 (0)