-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathindex.tsx
More file actions
154 lines (137 loc) · 4.51 KB
/
Copy pathindex.tsx
File metadata and controls
154 lines (137 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React, { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import {
capacityOf,
generateAccountFromPrivateKey,
createSporeDOB,
showSporeContent,
shannonToCKB,
} from "./lib";
import { hexStringToUint8Array } from "./helper";
import { RawSporeData } from "@spore-sdk/core";
import { Script } from "@ckb-ccc/core";
const container = document.getElementById("root");
const root = createRoot(container)
root.render(<App />);
export function App() {
// default value: first account privkey from offckb
const [privKey, setPrivKey] = useState(
"0x6109170b275a09ad54877b82f7d9930f88cab5717d484fb4741ae9d1dd078cd6"
);
const [fromAddr, setFromAddr] = useState("");
const [fromLock, setFromLock] = useState<Script>();
const [balance, setBalance] = useState("0");
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const [fileContent, setFileContent] = useState<Uint8Array | null>(null);
const [txHash, setTxHash] = useState<string>();
const [outputIndex, setOutputIndex] = useState<number>();
const [rawSporeData, setRawSporeData] = useState<RawSporeData>();
const [imageURL, setImageURL] = useState<string>();
useEffect(() => {
const updateFromInfo = async () => {
const { lockScript, address } = await generateAccountFromPrivateKey(privKey);
const capacity = await capacityOf(address);
setFromAddr(address);
setFromLock(lockScript);
setBalance(shannonToCKB(capacity).toString());
};
if (privKey) {
updateFromInfo();
}
}, [privKey]);
const onInputPrivKey = (e: React.ChangeEvent<HTMLInputElement>) => {
// Regular expression to match a valid private key with "0x" prefix
const priv = e.target.value;
const privateKeyRegex = /^0x[0-9a-fA-F]{64}$/;
const isValid = privateKeyRegex.test(priv);
if (isValid) {
setPrivKey(priv);
} else {
alert(
`Invalid private key: must start with 0x and 32 bytes length. Ensure you're using a valid private key from the offckb accounts list.`
);
}
};
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const files = event.target.files;
if (files && files.length > 0) {
setSelectedFile(files[0]);
const reader = new FileReader();
reader.onload = () => {
// Access the file content here
const content = reader.result;
if (content && content instanceof ArrayBuffer) {
const uint8Array = new Uint8Array(content);
setFileContent(uint8Array);
}
};
// Read the file as ArrayBuffer
reader.readAsArrayBuffer(files[0]);
}
};
const createSpore = async () => {
const { txHash, outputIndex } = await createSporeDOB(privKey, fileContent);
setTxHash(txHash);
setOutputIndex(outputIndex);
};
const renderSpore = async () => {
const res = await showSporeContent(txHash, outputIndex);
if (!res) return;
setRawSporeData(res);
// Create Blob from binary data
const buffer = hexStringToUint8Array(res.content.toString().slice(2));
const blob = new Blob([buffer], { type: res.contentType });
const url = URL.createObjectURL(blob);
setImageURL(url);
};
const enabled = +balance > 0 && !!fileContent;
const enabledRead = !!txHash && outputIndex != null;
return (
<div>
<h1>Create on-chain digital objects</h1>
<label htmlFor="private-key">Private Key: </label>
<input
id="private-key"
type="text"
value={privKey}
onChange={onInputPrivKey}
/>
<ul>
<li>CKB Address: {fromAddr}</li>
<li>
Current lock script:
<pre>{JSON.stringify(fromLock, null, 2)}</pre>
</li>
<li>Total capacity: {balance} CKB</li>
</ul>
<small>Tx fee: 0.001 CKB</small>
<br />
<br />
<div>
<h4>Upload DOB Image File</h4>
<input type="file" onChange={handleFileChange} />
{selectedFile && (
<div>
<p>File Size: {selectedFile.size} bytes</p>
</div>
)}
</div>
<br />
<br />
<button disabled={!enabled} onClick={createSpore}>
Create DOB
</button>
<hr />
{txHash && <li>tx Hash: {txHash}</li>}
<button disabled={!enabledRead} onClick={renderSpore}>
Check Spore Content
</button>
{rawSporeData && (
<div>
<p>contentType: {rawSporeData.contentType}</p>
</div>
)}
{imageURL && <img src={imageURL} />}
</div>
);
}