Skip to content

Commit

Permalink
chore: use serialization for script
Browse files Browse the repository at this point in the history
  • Loading branch information
duanyytop committed Sep 2, 2019
1 parent 66bfdb0 commit bdebebf
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 101 deletions.
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ description 'ckb-sdk-java is a lightweight Java library for integration with ner

dependencies {
compile project(":utils")
compile project(":serialization")
compile "io.reactivex.rxjava2:rxjava:$rxjavaVersion"
compile "com.google.code.gson:gson:$gsonVersion"
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
Expand Down
19 changes: 16 additions & 3 deletions core/src/main/java/org/nervos/ckb/methods/type/Script.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.nervos.ckb.methods.type;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
import org.nervos.ckb.service.CKBService;
import org.nervos.ckb.crypto.Blake2b;
import org.nervos.ckb.type.*;

/** Copyright © 2019 Nervos Foundation. All rights reserved. */
public class Script {
Expand Down Expand Up @@ -32,7 +34,18 @@ public Script(String codeHash, List<String> args, String hashType) {
this.hashType = hashType;
}

public String scriptHash(CKBService ckbService) throws Exception {
return ckbService.computeScriptHash(this).send().getScriptHash();
public String scriptHash() {
ArrayList<Type> types = new ArrayList<>();
types.add(new Byte32(this.codeHash));
types.add(new Byte1(Script.DATA.equals(this.hashType) ? "00" : "0x01"));
List<Bytes> argList = new ArrayList<>();
for (String arg : this.args) {
argList.add(new Bytes(arg));
}
types.add(new DynVec(argList));
byte[] script = new Table(types).toBytes();
Blake2b blake2b = new Blake2b();
blake2b.update(script);
return blake2b.doFinalString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static SystemScriptCell getSystemScriptCell(CKBService ckbService) throws
throw new IOException("Genesis block transactions system script not found");
}
return new SystemScriptCell(
block.transactions.get(0).outputs.get(1).type.scriptHash(ckbService),
block.transactions.get(0).outputs.get(1).type.scriptHash(),
new OutPoint(block.transactions.get(1).hash, "0"));
}
}
19 changes: 3 additions & 16 deletions core/src/test/java/type/ScriptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,20 @@

import java.util.Collections;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.nervos.ckb.crypto.Hash;
import org.nervos.ckb.methods.type.Script;
import org.nervos.ckb.service.CKBService;
import org.nervos.ckb.service.HttpService;

/** Copyright © 2018 Nervos Foundation. All rights reserved. */
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ScriptTest {

private CKBService ckbService;

@BeforeAll
private void init() {
ckbService = CKBService.build(new HttpService("http://localhost:8114"));
}

@Test
public void testScriptHashWithCodeHash() throws Exception {
public void testScriptHashWithCodeHash() {
String codeHash =
Hash.blake2b(
"0x1400000000000e00100000000c000800000004000e0000000c00000014000000740100000000000000000600080004000600000004000000580100007f454c460201010000000000000000000200f3000100000078000100000000004000000000000000980000000000000005000000400038000100400003000200010000000500000000000000000000000000010000000000000001000000000082000000000000008200000000000000001000000000000001459308d00573000000002e7368737472746162002e74657874000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b000000010000000600000000000000780001000000000078000000000000000a000000000000000000000000000000020000000000000000000000000000000100000003000000000000000000000000000000000000008200000000000000110000000000000000000000000000000100000000000000000000000000000000000000");
Script script = new Script(codeHash, Collections.emptyList());
Script script = new Script(codeHash, Collections.emptyList(), Script.DATA);
Assertions.assertEquals(
"0xd0e22f863da970a3ff51a937ae78ba490bbdcede7272d658a053b9f80e30305d",
script.scriptHash(ckbService));
"0xd0e22f863da970a3ff51a937ae78ba490bbdcede7272d658a053b9f80e30305d", script.scriptHash());
}
}
1 change: 0 additions & 1 deletion serialization/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
description 'Minimal set of ckb serialization classes'

dependencies {
compile project(":core")
compile project(":utils")
}

Expand Down
25 changes: 3 additions & 22 deletions serialization/src/main/java/org/nervos/ckb/Encoder.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
package org.nervos.ckb;

import java.util.ArrayList;
import java.util.List;
import org.nervos.ckb.methods.type.Script;
import org.nervos.ckb.type.*;
import org.nervos.ckb.type.Type;

/** Copyright © 2019 Nervos Foundation. All rights reserved. */
public class Encoder {

public static Table toTable(Object object) {
if (object instanceof Script) {
Script script = (Script) object;
List<Type> types = new ArrayList<>();
types.add(new Byte32(script.codeHash));
types.add(Script.DATA.equals(script.hashType) ? new Byte1("00") : new Byte1("01"));
List<Bytes> argList = new ArrayList<>();
for (String arg : script.args) {
argList.add(new Bytes(arg));
}
types.add(new DynVec(argList));
return new Table(types);
}
return null;
}

public static byte[] encode(Table table) {
return table.toBytes();
public static byte[] encode(Type type) {
return type.toBytes();
}
}
7 changes: 1 addition & 6 deletions serialization/src/main/java/org/nervos/ckb/type/Byte32.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ public Byte32(String value) {

@Override
public byte[] toBytes() {
int length = getLength();
byte[] littleEnds = new byte[length];
for (int i = 0; i < length; i++) {
littleEnds[length - 1 - i] = value[i];
}
return littleEnds;
return value;
}

@Override
Expand Down
36 changes: 0 additions & 36 deletions serialization/src/test/java.org.nervos.ckb/EncoderTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void toBytesTest() {
Byte32 byte32 = new Byte32("68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88");
byte[] expected =
Numeric.hexStringToByteArray(
"0x888e7d436797bf8e6dbf1c3c7f2ce837a546795279f8ab84f5d252c98a43d568");
"68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88");
Assertions.assertArrayEquals(expected, byte32.toBytes());
}
}
32 changes: 17 additions & 15 deletions serialization/src/test/java.org.nervos.ckb/type/TableTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package type;

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.nervos.ckb.Encoder;
import org.nervos.ckb.methods.type.Script;
import org.nervos.ckb.type.Table;
import org.nervos.ckb.type.*;
import org.nervos.ckb.utils.Numeric;

/** Copyright © 2019 Nervos Foundation. All rights reserved. */
public class TableTest {

@Test
void toBytesTest() {
Script script =
new Script(
"68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
Collections.singletonList("3954acece65096bfa81258983ddb83915fc56bd8"),
"type");
Table table = Encoder.toTable(script);
ArrayList<Type> types = new ArrayList<>();
types.add(new Byte32("68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"));
types.add(new Byte1("01"));
List<Bytes> argList = new ArrayList<>();
argList.add(new Bytes("3954acece65096bfa81258983ddb83915fc56bd8"));
types.add(new DynVec(argList));
Table table = new Table(types);
byte[] result = Encoder.encode(table);
Assertions.assertEquals(
"5100000010000000300000003100000068d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88012000000008000000140000003954acece65096bfa81258983ddb83915fc56bd8",
Expand All @@ -27,12 +28,13 @@ void toBytesTest() {

@Test
void getLengthTest() {
Script script =
new Script(
"68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88",
Collections.singletonList("3954acece65096bfa81258983ddb83915fc56bd8"),
"type");
Table table = Encoder.toTable(script);
ArrayList<Type> types = new ArrayList<>();
types.add(new Byte32("68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88"));
types.add(new Byte1("01"));
List<Bytes> argList = new ArrayList<>();
argList.add(new Bytes("3954acece65096bfa81258983ddb83915fc56bd8"));
types.add(new DynVec(argList));
Table table = new Table(types);
byte[] result = Encoder.encode(table);
Assertions.assertEquals(81, result.length);
}
Expand Down

0 comments on commit bdebebf

Please sign in to comment.