forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionCode.java
More file actions
81 lines (70 loc) · 1.88 KB
/
Copy pathFunctionCode.java
File metadata and controls
81 lines (70 loc) · 1.88 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
package DNA.Core;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import DNA.UInt160;
import DNA.Core.Scripts.Program;
import DNA.IO.BinaryReader;
import DNA.IO.BinaryWriter;
import DNA.IO.Serializable;
import DNA.Wallets.ContractParameterType;
public class FunctionCode implements ICode, Serializable{
public byte[] code;
public ContractParameterType[] parameterTypes;
public ContractParameterType[] returnTypes;
public UInt160 scriptHash;
@Override
public void deserialize(BinaryReader reader) throws IOException {
parameterTypes = toEnum(reader.readVarBytes());
code = reader.readVarBytes();
}
@Override
public void serialize(BinaryWriter writer) throws IOException {
writer.writeVarBytes(toByte(parameterTypes));
writer.writeVarBytes(code);
}
private ContractParameterType toEnum(byte bt) {
return Arrays.stream(ContractParameterType.values()).filter(p -> p.ordinal() == bt).findAny().get();
}
private ContractParameterType[] toEnum(byte[] bt) {
if(bt == null) {
return null;
}
List<ContractParameterType> list = new ArrayList<ContractParameterType>();
for(byte b: bt) {
ContractParameterType type = toEnum(b);
list.add(type);
}
return list.stream().toArray(ContractParameterType[]::new);
}
private byte[] toByte(ContractParameterType[] types) {
if(types == null) {
return new byte[0];
}
int len = types.length;
byte[] bt = new byte[len];
for(int i=0; i<len; ++i) {
bt[i] = (byte) types[i].ordinal();
}
return bt;
}
@Override
public byte[] getCode() {
return code;
}
@Override
public ContractParameterType[] getParameterList() {
return parameterTypes;
}
@Override
public ContractParameterType[] getReturnType() {
return returnTypes;
}
public UInt160 getCodeHash() {
if(scriptHash == null) {
scriptHash = Program.toScriptHash(getCode());
}
return scriptHash;
}
}