forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignatureContext.java
More file actions
155 lines (144 loc) · 5.03 KB
/
Copy pathSignatureContext.java
File metadata and controls
155 lines (144 loc) · 5.03 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
155
package DNA.Core;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.bouncycastle.math.ec.ECPoint;
import DNA.Helper;
import DNA.UInt160;
import DNA.Core.Scripts.Program;
import DNA.Core.Scripts.ScriptBuilder;
import DNA.Cryptography.ECC;
import DNA.IO.Json.JArray;
import DNA.IO.Json.JBoolean;
import DNA.IO.Json.JObject;
import DNA.IO.Json.JString;
import DNA.Wallets.Contract;
import DNA.Wallets.ContractParameterType;
/**
* 签名上下文
*
*/
public class SignatureContext {
/**
* 要签名的数据
*/
public final Signable signable;
/**
* 要验证的脚本散列值
*/
public final UInt160[] scriptHashes;
/**
* 合约脚本代码
*/
private final byte[][] redeemScripts;
/**
* 公钥-签名数据
*/
private final Map<ECPoint, byte[]>[] signatures;
private final boolean[] completed;
/**
* 判断签名是否完成
*/
public boolean isCompleted() {
for (boolean b : completed) {
if (!b) {
return false;
}
}
return true;
}
/**
* 对指定的数据构造签名上下文
* <param name="signable">要签名的数据</param>
*/
@SuppressWarnings("unchecked")
public SignatureContext(Signable signable) {
this.signable = signable;
this.scriptHashes = signable.getScriptHashesForVerifying();
this.redeemScripts = new byte[scriptHashes.length][];
this.signatures = (Map<ECPoint, byte[]>[]) Array.newInstance(Map.class, scriptHashes.length);
this.completed = new boolean[scriptHashes.length];
}
/**
* 添加一个签名
* <param name="contract">该签名所对应的合约</param>
* <param name="pubkey">该签名所对应的公钥</param>
* <param name="signature">签名</param>
* <returns>返回签名是否已成功添加</returns>
*/
public boolean add(Contract contract, ECPoint pubkey, byte[] signature) {
for (int i = 0; i < scriptHashes.length; i++) {
if (scriptHashes[i].equals(contract.scriptHash())) {
if (redeemScripts[i] == null) {
redeemScripts[i] = contract.redeemScript;
}
if (signatures[i] == null) {
signatures[i] = new HashMap<ECPoint, byte[]>();
}
signatures[i].put(pubkey, signature);
completed[i] |=
contract.parameterList.length == signatures[i].size()
&& Arrays.stream(contract.parameterList).allMatch(
p -> p == ContractParameterType.Signature);
return true;
}
}
return false;
}
/**
* 从签名上下文中获得完整签名的合约脚本
* <returns>返回合约脚本</returns>
*/
public Program[] getScripts() {
if (!isCompleted()) throw new IllegalStateException();
Program[] scripts = new Program[signatures.length];
for (int i = 0; i < scripts.length; i++) {
try (ScriptBuilder sb = new ScriptBuilder()) {
for (byte[] signature : signatures[i].entrySet().stream()
.sorted((a, b) -> ECC.compare(a.getKey(), b.getKey()))
.map(p -> p.getValue()).toArray(byte[][]::new)) {
sb.push(signature);
}
scripts[i] = new Program();
scripts[i].parameter = sb.toArray(); // sign
scripts[i].code = redeemScripts[i]; // pk
}
}
return scripts;
}
/**
* 把签名上下文转为json对象
* <returns>返回json对象</returns>
*/
public JObject json() {
JObject json = new JObject();
json.set("type", new JString(signable.getClass().getTypeName()));
json.set("hex", new JString(Helper.toHexString(signable.getHashData())));
JArray scripts = new JArray();
for (int i = 0; i < signatures.length; i++) {
if (signatures[i] == null) {
scripts.add(null);
} else {
scripts.add(new JObject());
scripts.get(i).set("redeem_script", new JString(Helper.toHexString(redeemScripts[i])));
JArray sigs = new JArray();
for (Entry<ECPoint, byte[]> pair : signatures[i].entrySet()) {
JObject signature = new JObject();
signature.set("pubkey", new JString(Helper.toHexString(pair.getKey().getEncoded(true))));
signature.set("signature", new JString(Helper.toHexString(pair.getValue())));
sigs.add(signature);
}
scripts.get(i).set("signatures", sigs);
scripts.get(i).set("completed", new JBoolean(completed[i]));
}
}
json.set("scripts", scripts);
return json;
}
@Override
public String toString() {
return json().toString();
}
}