diff --git a/.gitignore b/.gitignore index 737778fde738b243fb66a2594658b88f6c774b36..00f15071ea704f1434c8e0baf8d95296d3cdf70b 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,5 @@ out/ *.truststore routers/ logs/ +src/test/resources/application** +db/** \ No newline at end of file diff --git a/build.gradle b/build.gradle index 49fa75c1c7d5017830bd019583cf235bd63ee6af..e519322f3a0234bfbbb6fa4b883e16b1b39ae13b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id 'com.github.sherter.google-java-format' version '0.8' - id 'org.ajoberstar.grgit' version '4.0.1' + id 'org.ajoberstar.grgit' version '4.1.1' } @@ -36,28 +36,42 @@ dependencies { compileOnly 'org.projectlombok:lombok:1.18.8' annotationProcessor 'org.projectlombok:lombok:1.18.8' - compile 'link.luyu:luyu-cross-chain-protocol:1.0.0' + compile ('link.luyu:luyu-cross-chain-protocol:1.0.0'){ + exclude group : 'org.bouncycastle', module: 'bcprov-jdk15on' + } - compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0' + compile 'com.fasterxml.jackson.core:jackson-databind:2.14.3' compile 'javax.xml.bind:jaxb-api:2.3.1' - compile 'com.moandjiezana.toml:toml4j:0.7.2' - compile 'org.springframework.boot:spring-boot-starter-web:2.5.5' - compile 'org.springframework.boot:spring-boot-starter-security:2.5.5' - compile 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.5' - compile 'mysql:mysql-connector-java:8.0.21' - compile 'com.h2database:h2:1.4.196' + compile('com.moandjiezana.toml:toml4j:0.7.2') { + exclude group: "com.google.code.gson" + } + compile 'com.google.code.gson:gson:2.10.1' + compile('org.springframework.boot:spring-boot-starter-web:2.6.15') { + exclude group: 'org.yaml', module: 'snakeyaml' + } + compile ('org.springframework.boot:spring-boot-starter-security:2.6.15'){ + exclude group: 'org.yaml', module: 'snakeyaml' + } + compile ('org.springframework.boot:spring-boot-starter-data-jpa:2.6.15'){ + exclude group: 'org.yaml', module: 'snakeyaml' + } + compile 'mysql:mysql-connector-java:8.0.33' + compile 'com.h2database:h2:2.2.224' compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.17.1' compile 'com.auth0:java-jwt:3.10.3' - compile 'io.netty:netty-all:4.1.47.Final' - compile 'org.bouncycastle:bcprov-jdk15on:1.60' + compile 'io.netty:netty-all:4.1.101.Final' + compile 'org.bouncycastle:bcprov-jdk18on:1.74' compile 'com.alibaba:druid:1.2.4' compile 'commons-codec:commons-codec:1.14' configurations.compile.exclude(group: 'ch.qos.logback') configurations.compile.exclude(group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j') // Use JUnit test framework - testImplementation 'junit:junit:4.12' - testImplementation 'org.springframework.boot:spring-boot-starter-test:2.1.13.RELEASE' + testCompile 'junit:junit:4.13.2' + testCompile('org.springframework.boot:spring-boot-starter-test:2.6.15') { + exclude group: 'org.yaml', module: 'snakeyaml' + exclude group: 'net.minidev', module: 'json-smart' + } } diff --git a/src/main/java/link/luyu/account/manager/ServiceController.java b/src/main/java/link/luyu/account/manager/ServiceController.java index d16f28d8bd940f9cc2858595c8594a19456b4573..7eb7cf11b0dd422747e8fd8ea0c8483c3938c838 100644 --- a/src/main/java/link/luyu/account/manager/ServiceController.java +++ b/src/main/java/link/luyu/account/manager/ServiceController.java @@ -23,8 +23,10 @@ import link.luyu.account.manager.packet.SignRequest; import link.luyu.account.manager.packet.SignResponse; import link.luyu.account.manager.packet.VerifyRequest; import link.luyu.account.manager.packet.VerifyResponse; +import link.luyu.protocol.algorithm.ecdsa.secp256k1.EcdsaSecp256k1WithSHA256; import link.luyu.protocol.common.STATUS; import link.luyu.protocol.network.Account; +import org.bouncycastle.util.encoders.Hex; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestBody; @@ -150,6 +152,18 @@ public class ServiceController { .build(); } else { CompletableFuture future = new CompletableFuture<>(); + if (logger.isTraceEnabled()) { + String address = account.getProperty("address"); + if (type.equals(EcdsaSecp256k1WithSHA256.TYPE)) { + address = EcdsaSecp256k1WithSHA256.getAddress(account.getPubKey()); + } + logger.trace( + "Sign message, type:{}, luyuIdentity:{}, sign pubKey:{}, address:{}", + type, + luyuIdentity, + Hex.toHexString(account.getPubKey()), + address); + } account.sign( message, new Account.SignCallback() { diff --git a/src/main/java/link/luyu/account/manager/account/AlgAccount.java b/src/main/java/link/luyu/account/manager/account/AlgAccount.java index c8ba5673a26349b480e98dd40cd14f8349243eff..c4ecf97d7c32862f163cc6cd3967c4800e23e014 100644 --- a/src/main/java/link/luyu/account/manager/account/AlgAccount.java +++ b/src/main/java/link/luyu/account/manager/account/AlgAccount.java @@ -1,7 +1,6 @@ package link.luyu.account.manager.account; import com.fasterxml.jackson.annotation.JsonGetter; -import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -10,6 +9,7 @@ import link.luyu.account.manager.db.ChainAccountPropertiesTableBean; import link.luyu.account.manager.db.LuyuAccountTableBean; import link.luyu.protocol.algorithm.SignAlgManager; import link.luyu.protocol.algorithm.SignatureAlgorithm; +import link.luyu.protocol.algorithm.ecdsa.secp256k1.EcdsaSecp256k1WithSHA256; import link.luyu.protocol.common.STATUS; import link.luyu.protocol.network.Account; @@ -30,6 +30,12 @@ public class AlgAccount implements Account { algAccount.setPubKey(row.getAlgPub()); algAccount.setSecKey(row.getAlgSec()); algAccount.setDefault(row.isAlgIsDefault()); + if (row.getAlgType().equals(EcdsaSecp256k1WithSHA256.TYPE)) { + String address = EcdsaSecp256k1WithSHA256.getAddress(row.getAlgPub()); + if (algAccount.properties != null) { + algAccount.setProperty("address", address); + } + } return algAccount; } @@ -39,7 +45,7 @@ public class AlgAccount implements Account { Map props = new HashMap<>(); if (propBeans != null) { for (ChainAccountPropertiesTableBean propBean : propBeans) { - props.put(propBean.getKey(), propBean.getValue()); + props.put(propBean.getPropKey(), propBean.getPropValue()); } } @@ -133,7 +139,7 @@ public class AlgAccount implements Account { this.pubKey = pubKey; } - @JsonIgnore + // @JsonIgnore public byte[] getSecKey() { return secKey; } diff --git a/src/main/java/link/luyu/account/manager/account/LuyuAccountManager.java b/src/main/java/link/luyu/account/manager/account/LuyuAccountManager.java index c50b5dfa00dd625bf1cb664adec1785f040765fe..29099fad2df1c2bac92cb0e7b3366b95e3085634 100644 --- a/src/main/java/link/luyu/account/manager/account/LuyuAccountManager.java +++ b/src/main/java/link/luyu/account/manager/account/LuyuAccountManager.java @@ -118,17 +118,17 @@ public class LuyuAccountManager { (key, value) -> { ChainAccountPropertiesTableBean propBean = chainAccountPropertiesTableJPA - .findByIdentityAndAlgKeyIDAndAlgTypeAndKey( + .findByIdentityAndAlgKeyIDAndAlgTypeAndPropKey( luyuIdentity, keyId, type, key); if (propBean != null) { - propBean.setValue(value); + propBean.setPropValue(value); } else { propBean = new ChainAccountPropertiesTableBean(); propBean.setIdentity(luyuIdentity); propBean.setAlgKeyID(keyId); propBean.setAlgType(type); - propBean.setKey(key); - propBean.setValue(value); + propBean.setPropKey(key); + propBean.setPropValue(value); } propBeans.add(propBean); }); @@ -235,17 +235,17 @@ public class LuyuAccountManager { (key, value) -> { ChainAccountPropertiesTableBean propBean = chainAccountPropertiesTableJPA - .findByIdentityAndAlgKeyIDAndAlgTypeAndKey( + .findByIdentityAndAlgKeyIDAndAlgTypeAndPropKey( luyuIdentity, defaultKeyIdFinal, type, key); if (propBean != null) { - propBean.setValue(value); + propBean.setPropValue(value); } else { propBean = new ChainAccountPropertiesTableBean(); propBean.setIdentity(luyuIdentity); propBean.setAlgKeyID(defaultKeyIdFinal); propBean.setAlgType(type); - propBean.setKey(key); - propBean.setValue(value); + propBean.setPropKey(key); + propBean.setPropValue(value); } propBeans.add(propBean); @@ -304,14 +304,14 @@ public class LuyuAccountManager { } ChainAccountPropertiesTableBean propBean = - chainAccountPropertiesTableJPA.findByIdentityAndAlgKeyIDAndAlgTypeAndKey( + chainAccountPropertiesTableJPA.findByIdentityAndAlgKeyIDAndAlgTypeAndPropKey( luyuIdentity, defaultKeyId, type, key); if (propBean == null) { logger.warn("Default algorithm account not found with type: " + type); return null; } - return propBean.getValue(); + return propBean.getPropValue(); } } diff --git a/src/main/java/link/luyu/account/manager/config/ApplicationConfig.java b/src/main/java/link/luyu/account/manager/config/ApplicationConfig.java index ca51b8690d898c97fad15c3657382fe828188936..6f22d1eaf9594fdfc942c931b378ab1f729da67a 100644 --- a/src/main/java/link/luyu/account/manager/config/ApplicationConfig.java +++ b/src/main/java/link/luyu/account/manager/config/ApplicationConfig.java @@ -35,8 +35,8 @@ import org.slf4j.LoggerFactory; [db] # for connect database url = 'jdbc:mysql://localhost:3306/account_manager' - username = 'root' - password = '' + username = '*****' # your database username (if need) + password = '*****' # your database password (if need) [ext] # for image auth code diff --git a/src/main/java/link/luyu/account/manager/db/ChainAccountPropertiesTableBean.java b/src/main/java/link/luyu/account/manager/db/ChainAccountPropertiesTableBean.java index a27cc7f20c2cb7f127b4f8b2ee0471b01db127cc..61f47f2abf73a06057ca52174bed3244ef63d536 100644 --- a/src/main/java/link/luyu/account/manager/db/ChainAccountPropertiesTableBean.java +++ b/src/main/java/link/luyu/account/manager/db/ChainAccountPropertiesTableBean.java @@ -24,8 +24,8 @@ public class ChainAccountPropertiesTableBean { private Integer algKeyID; @Column(nullable = true) - private String key; + private String propKey; @Column(nullable = true, columnDefinition = "text") - private String value; + private String propValue; } diff --git a/src/main/java/link/luyu/account/manager/db/ChainAccountPropertiesTableJPA.java b/src/main/java/link/luyu/account/manager/db/ChainAccountPropertiesTableJPA.java index 63483eab817fe704ac1d3326dc736a2741d3214c..0d28adf189552068ababf54449484279285d14d0 100644 --- a/src/main/java/link/luyu/account/manager/db/ChainAccountPropertiesTableJPA.java +++ b/src/main/java/link/luyu/account/manager/db/ChainAccountPropertiesTableJPA.java @@ -6,8 +6,8 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface ChainAccountPropertiesTableJPA extends JpaRepository { - ChainAccountPropertiesTableBean findByIdentityAndAlgKeyIDAndAlgTypeAndKey( - String identity, Integer algKeyID, String algType, String key); + ChainAccountPropertiesTableBean findByIdentityAndAlgKeyIDAndAlgTypeAndPropKey( + String identity, Integer algKeyID, String algType, String propKey); List findByIdentityAndAlgKeyIDAndAlgType( String identity, Integer algKeyID, String algType); diff --git a/src/main/java/link/luyu/account/manager/packet/AddAlgAccountRequestPacketBuilder.java b/src/main/java/link/luyu/account/manager/packet/AddAlgAccountRequestPacketBuilder.java index 6ab17147aa91aa1a71a5a511ba3879fbebecb29e..4561fb22253a3e85766c2f48ecdc791a599e20a8 100644 --- a/src/main/java/link/luyu/account/manager/packet/AddAlgAccountRequestPacketBuilder.java +++ b/src/main/java/link/luyu/account/manager/packet/AddAlgAccountRequestPacketBuilder.java @@ -24,8 +24,6 @@ public class AddAlgAccountRequestPacketBuilder { String algo = args[1]; String pemPubFilePath = args[2]; String pemSecFilePath = args[3]; - System.out.println("pemPubFilePath: " + pemPubFilePath); - System.out.println("pemSecFilePath: " + pemSecFilePath); System.out.println(build(sender, algo, pemPubFilePath, pemSecFilePath)); } catch (Exception e) { diff --git a/src/main/java/link/luyu/account/manager/utils/Pem.java b/src/main/java/link/luyu/account/manager/utils/Pem.java index 1753833b182a7fa610de3656e63726926cf73254..e9fb014b0ddf0e849c4eeb8fa9825326f38ccced 100644 --- a/src/main/java/link/luyu/account/manager/utils/Pem.java +++ b/src/main/java/link/luyu/account/manager/utils/Pem.java @@ -2,11 +2,9 @@ package link.luyu.account.manager.utils; import static link.luyu.protocol.algorithm.ecdsa.secp256k1.Utils.toBytesPadded; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; -import java.nio.charset.StandardCharsets; import java.security.KeyFactory; import java.security.Security; import java.security.spec.PKCS8EncodedKeySpec; @@ -18,6 +16,9 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; import org.bouncycastle.util.io.pem.PemObject; import org.bouncycastle.util.io.pem.PemReader; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; public class Pem { public static void main(String[] args) { @@ -66,12 +67,10 @@ public class Pem { public static byte[] readSecKeyBytesFromPem(String pemFilePath) throws Exception { - String content = FileUtility.readFileContent(pemFilePath); - PemReader pemReader = - new PemReader( - new InputStreamReader( - new ByteArrayInputStream( - content.getBytes(StandardCharsets.UTF_8)))); + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + Resource pemResources = resolver.getResource(pemFilePath); + + PemReader pemReader = new PemReader(new InputStreamReader(pemResources.getInputStream())); try { PemObject pem = pemReader.readPemObject(); @@ -94,12 +93,10 @@ public class Pem { public static byte[] readPubKeyBytesFromPemUnpadded(String pemFilePath) throws Exception { - String content = FileUtility.readFileContent(pemFilePath); - PemReader pemReader = - new PemReader( - new InputStreamReader( - new ByteArrayInputStream( - content.getBytes(StandardCharsets.UTF_8)))); + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + Resource pemResources = resolver.getResource(pemFilePath); + + PemReader pemReader = new PemReader(new InputStreamReader(pemResources.getInputStream())); try { PemObject pem = pemReader.readPemObject(); diff --git a/src/main/java/link/luyu/account/manager/utils/SM2.java b/src/main/java/link/luyu/account/manager/utils/SM2.java index 62da85f49ff66d07e9be118569fd4205a21434ca..f36515284507cf4a4f06df7a260610caaa676fc9 100644 --- a/src/main/java/link/luyu/account/manager/utils/SM2.java +++ b/src/main/java/link/luyu/account/manager/utils/SM2.java @@ -111,7 +111,7 @@ public class SM2 { private static java.security.spec.ECParameterSpec tryFindNamedCurveSpec( java.security.spec.ECParameterSpec params) { ECParameterSpec bcSpec = - org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params, false); + org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params); for (Object name : Collections.list(org.bouncycastle.jce.ECNamedCurveTable.getNames())) { org.bouncycastle.jce.spec.ECNamedCurveParameterSpec bcNamedSpec = org.bouncycastle.jce.ECNamedCurveTable.getParameterSpec((String) name); @@ -158,8 +158,7 @@ public class SM2 { public key */ ECParameterSpec bcSpec = - org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec( - params, false); + org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params); org.bouncycastle.math.ec.ECPoint q = bcSpec.getG().multiply(privateKey.getS()); org.bouncycastle.math.ec.ECPoint bcW = bcSpec.getCurve().decodePoint(q.getEncoded(false)); diff --git a/src/main/java/link/luyu/account/manager/utils/X509CertificateUtils.java b/src/main/java/link/luyu/account/manager/utils/X509CertificateUtils.java index 7bc81b752b5516f4bce03cb2c0e25439b983250d..ad6815bb35825ac2b745c50778bfd9d7aff106f9 100644 --- a/src/main/java/link/luyu/account/manager/utils/X509CertificateUtils.java +++ b/src/main/java/link/luyu/account/manager/utils/X509CertificateUtils.java @@ -1,21 +1,21 @@ package link.luyu.account.manager.utils; -import java.io.ByteArrayInputStream; -import java.nio.charset.StandardCharsets; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; public class X509CertificateUtils { public static byte[] readPubKeyBytes(String filePath) throws Exception { - String content = FileUtility.readFileContent(filePath); + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + Resource pemResources = resolver.getResource(filePath); CertificateFactory f = CertificateFactory.getInstance("X.509", new BouncyCastleProvider()); X509Certificate certificate = - (X509Certificate) - f.generateCertificate( - new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))); + (X509Certificate) f.generateCertificate(pemResources.getInputStream()); BCECPublicKey publicKey = (BCECPublicKey) certificate.getPublicKey(); byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); diff --git a/src/main/resources/application-sample.toml b/src/main/resources/application-sample.toml index 801e63105df1e5ae58f0e9de06edfd782e246c66..f891ca05a3f8026f24badca7109018cadd3db8e3 100644 --- a/src/main/resources/application-sample.toml +++ b/src/main/resources/application-sample.toml @@ -12,8 +12,8 @@ # url = 'jdbc:mysql://localhost:3306/account_manager?useSSL=false' # for connect database, you can add other connect configs behind '?' # Note: useSSL=false for connect MySQL 8.0+, if you deploy mysql remotely, we recommended you use SSL. - username = 'root' - password = '' + username = '*****' # your database username (if need) + password = '*****' # your database password (if need) # for mysql sensitive fields encrypt # if this field is empty of not configured, the encrypt operation will not take place encryptKey = 'rIBJD38jqSMR@CSM' diff --git a/src/main/resources/mysql_setup.sql b/src/main/resources/mysql_setup.sql index 5e08bbdc3614dc1f54d1d8a71f429d6421f02075..6547a1f69253c5d2e72362ee3e9177f2b8b0fa3c 100644 --- a/src/main/resources/mysql_setup.sql +++ b/src/main/resources/mysql_setup.sql @@ -1 +1 @@ -create database if not exists wecross_account_manager collate=utf8_bin; \ No newline at end of file +create database if not exists account_manager collate=utf8_bin; \ No newline at end of file diff --git a/src/test/java/link/luyu/account/manager/X509Test.java b/src/test/java/link/luyu/account/manager/X509Test.java deleted file mode 100644 index 11e56a34f5d1264d45cc73211a3e729eeea590c2..0000000000000000000000000000000000000000 --- a/src/test/java/link/luyu/account/manager/X509Test.java +++ /dev/null @@ -1,11 +0,0 @@ -package link.luyu.account.manager; - -import link.luyu.account.manager.utils.X509CertificateUtils; -import org.junit.Test; - -public class X509Test { - @Test - public void test() throws Exception { - byte[] bytes = X509CertificateUtils.readPubKeyBytes("classpath:my/account.crt"); - } -} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 06b1754ccd225607a481b2d27cc35d26d261e6e0..3127439816557794bb8687d68fa8ab922aebe5ba 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -9,6 +9,7 @@ spring.datasource.driver-class-name=org.h2.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true spring.h2.console.path=/h2-console +# spring.h2.console.settings.web-allow-others=true # Database creation # spring.datasource.schema=classpath:db_setup.sql diff --git a/src/test/resources/my/account.toml b/src/test/resources/my/account.toml new file mode 100644 index 0000000000000000000000000000000000000000..98d6c0c2f5304f4442412a2963d9b03127cd52a7 --- /dev/null +++ b/src/test/resources/my/account.toml @@ -0,0 +1,5 @@ +[account] + type = 'Fabric1.4' + mspid = 'Org1MSP' + keystore = 'account.key' + signcert = 'account.crt' diff --git a/src/test/resources/mysql_setup.sql b/src/test/resources/mysql_setup.sql index 5e08bbdc3614dc1f54d1d8a71f429d6421f02075..6547a1f69253c5d2e72362ee3e9177f2b8b0fa3c 100644 --- a/src/test/resources/mysql_setup.sql +++ b/src/test/resources/mysql_setup.sql @@ -1 +1 @@ -create database if not exists wecross_account_manager collate=utf8_bin; \ No newline at end of file +create database if not exists account_manager collate=utf8_bin; \ No newline at end of file