Skip to content

Commit bf2a801

Browse files
committed
Make it run in a browser
1 parent ef733d7 commit bf2a801

21 files changed

+244
-244
lines changed

1-create-identity.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
1-
import Fs from "node:fs/promises";
21

3-
import DashHd from "dashhd";
4-
import * as DashHdUtils from "./src/dashhd-utils.ts";
52
import * as Bincode from "./src/bincode.ts";
3+
import * as DashBincode from "./2.0.0/generated_bincode.js"
64
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";
125

6+
import * as KeyUtils from "./src/key-utils.js";
137
import { loadWallet } from "./src/cli.ts";
148
import { deriveAllCreateIdentityKeys } from "./src/asset_lock.ts";
159
import { createPlatformAssetLock } from "./src/asset_lock.ts";
1610
import { connectToNode, TRPC } from "./src/rpc.ts"
1711
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"
12+
import { toHex } from "./src/hex.js"
2113
import { base58 } from "./src/util/base58.ts"
2214
import { findExistingIdentity } from "./src/identity.ts"
2315

16+
17+
export async function step1CreateIdentity(walletPhrase: string, walletSalt: string, identityIndex: number) {
2418
const rpc = new TRPC(RPC_AUTH_URL);
2519
const nodeRpc = connectToNode(NODE_ADDRESS)
2620

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-
}
21+
const walletKey = await loadWallet(walletPhrase, walletSalt);
4022

4123
const hdOpts = { version: "testnet" as const }; // TODO
4224

@@ -199,3 +181,28 @@ const identity = base58.encode(identityId);
199181
console.log();
200182
console.log(`https://testnet.platform-explorer.com/identity/${identity}`);
201183
console.log(`https://testnet.platform-explorer.com/transaction/${toHex(transitionHash)}`);
184+
}
185+
186+
if (typeof process === 'object' && process.argv[1] === import.meta.filename) {
187+
188+
import("dotenv").then(dotenv => {
189+
dotenv.default.config({ path: ".env" });
190+
191+
let walletPhrase = process.env.DASH_WALLET_PHRASE!;
192+
let walletSalt = process.env.DASH_WALLET_SALT ?? "";
193+
194+
const identityIndex = parseInt(process.argv[2], 10);
195+
if (isNaN(identityIndex)) {
196+
console.error("");
197+
console.error("USAGE");
198+
console.error(` ${process.argv[0]} ${process.argv[1]} <identity-index>`);
199+
console.error("");
200+
console.error("EXAMPLE");
201+
console.error(` ${process.argv[0]} ${process.argv[1]} 0`);
202+
console.error("");
203+
process.exit(1);
204+
}
205+
206+
step1CreateIdentity(walletPhrase, walletSalt, identityIndex);
207+
})
208+
}

2-create-data-contract.ts

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

33
import { doubleSha256 } from "dashtx"
44
import DashKeys from "dashkeys"
@@ -14,21 +14,12 @@ import { NODE_ADDRESS } from "./src/constants.ts"
1414
import { findExistingIdentity } from "./src/identity.ts"
1515
import { base58 } from "./src/util/base58.ts"
1616

17+
18+
export async function step2CreateDataContract(walletPhrase: string, walletSalt: string, identityIndex: number) {
19+
1720
const nodeRpc = connectToNode(NODE_ADDRESS);
1821

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-
}
22+
const walletKey = await loadWallet(walletPhrase, walletSalt);
3223

3324
let hdOpts = { version: "testnet" } as const; // TODO
3425

@@ -160,7 +151,7 @@ try {
160151
stateTransition: signedBytes,
161152
})
162153
console.log('response', response.status, response.response);
163-
await Fs.writeFile('data-contract-' + newContractIDStr.slice(0, 6) + '.json', JSON.stringify({id: newContractIDStr}));
154+
// await Fs.writeFile('data-contract-' + newContractIDStr.slice(0, 6) + '.json', JSON.stringify({id: newContractIDStr}));
164155

165156
} catch (e) {
166157
console.error("Error: ", decodeURIComponent((e as any).message))
@@ -170,3 +161,29 @@ console.log();
170161
console.log('New Contract ID:', newContractIDStr)
171162
console.log("https://testnet.platform-explorer.com/dataContract/" + newContractIDStr)
172163
console.log(`https://testnet.platform-explorer.com/transaction/${toHex(transitionHash)}`);
164+
165+
}
166+
167+
if (typeof process === 'object' && process.argv[1] === import.meta.filename) {
168+
169+
import("dotenv").then(dotenv => {
170+
dotenv.default.config({ path: ".env" });
171+
172+
let walletPhrase = process.env.DASH_WALLET_PHRASE!;
173+
let walletSalt = process.env.DASH_WALLET_SALT ?? "";
174+
175+
let identityIndex = parseInt(process.argv[2], 10);
176+
if (isNaN(identityIndex)) {
177+
console.error("");
178+
console.error("USAGE");
179+
console.error(` ${process.argv[0]} ${process.argv[1]} <identity-index>`);
180+
console.error("");
181+
console.error("EXAMPLE");
182+
console.error(` ${process.argv[0]} ${process.argv[1]} 0`);
183+
console.error("");
184+
process.exit(1);
185+
}
186+
187+
step2CreateDataContract(walletPhrase, walletSalt, identityIndex);
188+
});
189+
}

3-create-document.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import Fs from "node:fs/promises";
21

3-
// import DashHd from "dashhd";
4-
// import * as DashHdUtils from "./dashhd-utils.ts";
52
import DashKeys from "dashkeys";
6-
// import * as DashTx from "dashtx";
7-
// import * as DashPlatform from "./dashplatform.js";
83
import * as Bincode from "./src/bincode.ts";
94
import * as DashBincode from "./2.0.0/generated_bincode.js";
10-
import * as QRCode from "./src/_qr.js";
115
import * as KeyUtils from "./src/key-utils.js";
126
import { connectToNode } from "./src/rpc.ts"
137
import { NODE_ADDRESS } from "./src/constants.ts"
@@ -18,36 +12,13 @@ import { deriveAllCreateIdentityKeys } from "./src/asset_lock.ts"
1812
import { toHex } from "./src/hex.js"
1913
import { base58 } from "./src/util/base58.ts"
2014

21-
const nodeRpc = connectToNode(NODE_ADDRESS);
15+
export async function step3CreateDocument(walletPhrase: string, walletSalt: string, identityIndex: number, dataContractId: string, message: string) {
2216

23-
const walletKey = await loadWallet();
17+
const nodeRpc = connectToNode(NODE_ADDRESS);
2418

25-
function printUsage() {
26-
console.error("");
27-
console.error("USAGE");
28-
console.error(` ${process.argv[0]} ${process.argv[1]} <identity-index> <data-contract-id> [<message>]`);
29-
console.error("");
30-
console.error("EXAMPLE");
31-
console.error(` ${process.argv[0]} ${process.argv[1]} 0 JEJjgpGiLqeH8yaUeAwbCzuhTeL3xBMUAvyRCiGBXkEn "It's working!"`);
32-
console.error("");
33-
}
19+
const walletKey = await loadWallet(walletPhrase, walletSalt);
3420

35-
const identityIndex = parseInt(process.argv[2], 10);
36-
if (isNaN(identityIndex)) {
37-
printUsage();
38-
process.exit(1);
39-
}
40-
41-
const dataContractId = process.argv[3]; // "JEJjgpGiLqeH8yaUeAwbCzuhTeL3xBMUAvyRCiGBXkEn"
42-
console.log('dataContractId', dataContractId, dataContractId.length);
4321
const dataContractIdBytes = base58.decode(dataContractId);
44-
if (dataContractIdBytes.length != 32) {
45-
printUsage();
46-
process.exit(1);
47-
}
48-
49-
const message = process.argv[4] ?? "It's working!";
50-
5122

5223
const hdOpts = { version: "testnet" } as const; // TODO
5324

@@ -199,3 +170,41 @@ try {
199170
console.log("Document ID:" + base58.encode(document_id))
200171
console.log("https://testnet.platform-explorer.com/document/" + base58.encode(document_id))
201172
console.log(`https://testnet.platform-explorer.com/transaction/${toHex(transitionHash)}`);
173+
}
174+
175+
if (typeof process === 'object' && process.argv[1] === import.meta.filename) {
176+
177+
import("dotenv").then(dotenv => {
178+
dotenv.default.config({ path: ".env" });
179+
180+
let walletPhrase = process.env.DASH_WALLET_PHRASE!;
181+
let walletSalt = process.env.DASH_WALLET_SALT ?? "";
182+
183+
function printUsage() {
184+
console.error("");
185+
console.error("USAGE");
186+
console.error(` ${process.argv[0]} ${process.argv[1]} <identity-index> <data-contract-id> [<message>]`);
187+
console.error("");
188+
console.error("EXAMPLE");
189+
console.error(` ${process.argv[0]} ${process.argv[1]} 0 JEJjgpGiLqeH8yaUeAwbCzuhTeL3xBMUAvyRCiGBXkEn "It's working!"`);
190+
console.error("");
191+
}
192+
193+
const identityIndex = parseInt(process.argv[2], 10);
194+
if (isNaN(identityIndex)) {
195+
printUsage();
196+
process.exit(1);
197+
}
198+
199+
const dataContractId = process.argv[3]; // "JEJjgpGiLqeH8yaUeAwbCzuhTeL3xBMUAvyRCiGBXkEn"
200+
console.log('dataContractId', dataContractId, dataContractId.length);
201+
const dataContractIdBytes = base58.decode(dataContractId);
202+
if (dataContractIdBytes.length != 32) {
203+
printUsage();
204+
process.exit(1);
205+
}
206+
207+
const message = process.argv[4] ?? "It's working!";
208+
step3CreateDocument(walletPhrase, walletSalt, identityIndex, dataContractId, message);
209+
});
210+
}

built/DashPlatform.js/1-create-asset-lock.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Fs from "node:fs/promises";
22
import DashHd from "dashhd";
3-
import * as DashHdUtils from "./src/dashhd-utils.js";
3+
import * as DashHdUtils from "./src/dashhd-utils.ts";
44
import DashKeys from "dashkeys";
55
import * as DashTx from "dashtx";
66
import * as DashPlatform from "./src/dashplatform.js";
77
import * as QRCode from "./src/_qr.js";
88
import * as KeyUtils from "./src/key-utils.js";
99
import { createIdentityFromAssetLock } from "./2-create-identity-transition.js";
10-
import { loadWallet } from "./src/cli.js";
10+
import { loadWallet } from "./src/cli.ts";
1111
import { deriveAllCreateIdentityKeys } from "./src/asset_lock.js";
1212
import { createPlatformAssetLock } from "./src/asset_lock.js";
1313
// let DapiGrpc = require("@dashevo/dapi-grpc");

built/DashPlatform.js/3-data-contract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Bincode from "./src/bincode.js";
22
import * as DashBincode from "./1.8.1/generated_bincode.js";
33
import * as KeyUtils from "./src/key-utils.js";
4-
import { loadWallet } from "./src/cli.js";
4+
import { loadWallet } from "./src/cli.ts";
55
import { deriveAllCreateIdentityKeys } from "./src/asset_lock.js";
66
import { toHex } from "./src/hex.js";
77
const walletKey = await loadWallet();

built/DashPlatform.js/4-document.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// import DashHd from "dashhd";
2-
// import * as DashHdUtils from "./dashhd-utils.js";
2+
// import * as DashHdUtils from "./dashhd-utils.ts";
33
// import DashKeys from "dashkeys";
44
// import * as DashTx from "dashtx";
55
// import * as DashPlatform from "./dashplatform.js";
66
import * as Bincode from "./src/bincode.js";
77
import * as DashBincode from "./1.8.1/generated_bincode.js";
88
import * as KeyUtils from "./src/key-utils.js";
9-
import { loadWallet } from "./src/cli.js";
9+
import { loadWallet } from "./src/cli.ts";
1010
import { deriveAllCreateIdentityKeys } from "./src/asset_lock.js";
1111
import { toHex } from "./src/hex.js";
1212
async function main() {

built/DashPlatform.js/demo-static.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
import Dotenv from "dotenv";
33
import DashPhrase from "dashphrase";
4-
import DashHd from "./src/dashhd-utils.js";
4+
import DashHd from "./src/dashhd-utils.ts";
55
import DashKeys from "dashkeys";
66
import * as DashTx from "dashtx";
77
import * as DashPlatform from "./src/dashplatform.js";
@@ -10,7 +10,7 @@ import * as DashBincode from "./1.8.1/generated_bincode.js";
1010
import * as QRCode from "./src/_qr.js";
1111
import * as KeyUtils from "./src/key-utils.js";
1212
Dotenv.config({ path: ".env" });
13-
let rpcAuthUrl = "https://api:[email protected]";
13+
let RPC_AUTH_URL = "https://api:[email protected]";
1414
// const L1_VERSION_PLATFORM = 3;
1515
const L1_VERSION_PLATFORM = 0;
1616
const TYPE_ASSET_LOCK = 8;
@@ -123,10 +123,10 @@ async function main() {
123123
let regFundAddress = await createPlatformIdentity(walletKey, coinType, identityIndex);
124124
let fundingAddress = await DashHd.toAddr(regFundAddress.publicKey, hdOpts);
125125
let [oldDeltas, newDeltas] = await Promise.all([
126-
DashTx.utils.rpc(rpcAuthUrl, "getaddressdeltas", {
126+
DashTx.utils.rpc(RPC_AUTH_URL, "getaddressdeltas", {
127127
addresses: [fundingAddress],
128128
}),
129-
DashTx.utils.rpc(rpcAuthUrl, "getaddressmempool", {
129+
DashTx.utils.rpc(RPC_AUTH_URL, "getaddressmempool", {
130130
addresses: [fundingAddress],
131131
}),
132132
]);
@@ -293,13 +293,13 @@ async function main() {
293293
// console.log(`txSigned:`);
294294
// console.log(txSigned);
295295
// let txid = await DashTx.utils.rpc(
296-
// rpcAuthUrl,
296+
// RPC_AUTH_URL,
297297
// "sendrawtransaction",
298298
// txSigned.transaction,
299299
// );
300300
// const INSTANT_ALP = 0;
301301
// const CHAIN_ALP = 1;
302-
// let blockchaininfo = await DashTx.utils.rpc(rpcAuthUrl, "getblockchaininfo");
302+
// let blockchaininfo = await DashTx.utils.rpc(RPC_AUTH_URL, "getblockchaininfo");
303303
// let nextBlock = blockchaininfo.blocks + 1;
304304
// TODO - AJ is here
305305
console.log(`DEBUG funding outpoint`);

built/DashPlatform.js/demo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let WasmDpp = require("@dashevo/wasm-dpp");
1111
let Dpp = WasmDpp.DashPlatformProtocol;
1212
//@ts-ignore - sssssh, yes Base58 does exist
1313
let b58 = DashKeys.Base58.create();
14-
let rpcAuthUrl = "https://api:[email protected]";
14+
let RPC_AUTH_URL = "https://api:[email protected]";
1515
const L1_VERSION_PLATFORM = 3;
1616
const TYPE_ASSET_LOCK = 8;
1717
const L2_VERSION_PLATFORM = 1;
@@ -112,7 +112,7 @@ async function main() {
112112
privateKey: addressKey.privateKey,
113113
pubKeyHash: pkh,
114114
});
115-
let utxos = await DashTx.utils.rpc(rpcAuthUrl, "getaddressutxos", {
115+
let utxos = await DashTx.utils.rpc(RPC_AUTH_URL, "getaddressutxos", {
116116
addresses: [addr],
117117
});
118118
let total = DashTx.sum(utxos);
@@ -145,13 +145,13 @@ async function main() {
145145
console.log(`txSigned:`);
146146
console.log(txSigned);
147147
// let txid = await DashTx.utils.rpc(
148-
// rpcAuthUrl,
148+
// RPC_AUTH_URL,
149149
// "sendrawtransaction",
150150
// txSigned.transaction,
151151
// );
152152
const INSTANT_ALP = 0;
153153
const CHAIN_ALP = 1;
154-
let blockchaininfo = await DashTx.utils.rpc(rpcAuthUrl, "getblockchaininfo");
154+
let blockchaininfo = await DashTx.utils.rpc(RPC_AUTH_URL, "getblockchaininfo");
155155
let nextBlock = blockchaininfo.blocks + 1;
156156
let fundingOutPointHex = await getFundingOutPointHex(txSigned.transaction, vout);
157157
let identityId = createIdentityId(fundingOutPointHex);

0 commit comments

Comments
 (0)