forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.java
More file actions
65 lines (56 loc) · 1.6 KB
/
Copy pathScript.java
File metadata and controls
65 lines (56 loc) · 1.6 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
package DNA.Core.Scripts;
import java.io.IOException;
import DNA.Helper;
import DNA.UInt160;
import DNA.Cryptography.Digest;
import DNA.IO.BinaryReader;
import DNA.IO.BinaryWriter;
import DNA.IO.JsonReader;
import DNA.IO.JsonSerializable;
import DNA.IO.JsonWriter;
import DNA.IO.Serializable;
import DNA.IO.Json.JObject;
import DNA.IO.Json.JString;
/**
* 脚本
*/
public class Script implements Serializable, JsonSerializable {
public byte[] stackScript;
public byte[] redeemScript;
@Override
public void deserialize(BinaryReader reader) throws IOException {
stackScript = reader.readVarBytes();
redeemScript = reader.readVarBytes();
}
@Override
public void serialize(BinaryWriter writer) throws IOException {
writer.writeVarBytes(stackScript);
writer.writeVarBytes(redeemScript);
}
/**
* 变成json对象
* <returns>返回json对象</returns>
*/
public JObject json() {
JObject json = new JObject();
json.set("stack", new JString(Helper.toHexString(stackScript)));
json.set("redeem", new JString(Helper.toHexString(redeemScript)));
return json;
}
public static UInt160 toScriptHash(byte[] script) {
return new UInt160(Digest.hash160(script));
}
/**
* byte格式数据反序列化
*/
@Override
public void fromJson(JsonReader reader) {
JObject json = reader.json();
stackScript = Helper.hexToBytes(json.get("Code").asString());
redeemScript = Helper.hexToBytes(json.get("Parameter").asString());
}
@Override
public void toJson(JsonWriter writer) {
// ...
}
}