forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignable.java
More file actions
65 lines (56 loc) · 1.99 KB
/
Copy pathSignable.java
File metadata and controls
65 lines (56 loc) · 1.99 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;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.signers.ECDSASigner;
import org.bouncycastle.util.BigIntegers;
import DNA.UInt160;
import DNA.Cryptography.*;
import DNA.IO.*;
import DNA.Wallets.Account;
/**
* 为需要签名的数据提供一个接口
*/
public interface Signable extends Serializable {
/**
* 反序列化未签名的数据
* <param name="reader">数据来源</param>
* @throws IOException
*/
void deserializeUnsigned(BinaryReader reader) throws IOException;
/**
* 序列化未签名的数据
* <param name="writer">存放序列化后的结果</param>
* @throws IOException
*/
void serializeUnsigned(BinaryWriter writer) throws IOException;
/**
* 获得需要校验的脚本Hash值
* <returns>返回需要校验的脚本Hash值</returns>
*/
UInt160[] getScriptHashesForVerifying();
default byte[] getHashData() {
try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) {
try (BinaryWriter writer = new BinaryWriter(ms)) {
serializeUnsigned(writer);
writer.flush();
return ms.toByteArray();
}
} catch (IOException ex) {
throw new UnsupportedOperationException(ex);
}
}
default byte[] sign(Account account) {
ECDSASigner signer = new ECDSASigner();
signer.init(true, new ECPrivateKeyParameters(new BigInteger(1, account.privateKey), ECC.secp256r1));
BigInteger[] bi = signer.generateSignature(Digest.sha256(getHashData()));// dna
byte[] signature = new byte[64];
System.arraycopy(BigIntegers.asUnsignedByteArray(32, bi[0]), 0, signature, 0, 32);
System.arraycopy(BigIntegers.asUnsignedByteArray(32, bi[1]), 0, signature, 32, 32);
return signature;
}
default boolean verifySignature() {
return true;
}
}