├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── docs ├── cn │ ├── README.md │ ├── Wallet_File_Specification_cn.md │ ├── asset.md │ ├── attest.md │ ├── auth.md │ ├── basic.md │ ├── errorcode.md │ ├── governance.md │ ├── identity_claim.md │ ├── interface.md │ ├── sdk_get_start.md │ └── smartcontract.md └── en │ ├── README.md │ ├── Wallet_File_Specification_en.md │ ├── asset.md │ ├── attest.md │ ├── auth.md │ ├── basic.md │ ├── errorcode.md │ ├── governance.md │ ├── identity_claim.md │ ├── interface.md │ ├── sdk_get_start.md │ └── smartcontract.md ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── oep8.dat ├── ongx.dat ├── pom.xml ├── settings.gradle └── src ├── META-INF └── MANIFEST.MF ├── main └── java │ ├── META-INF │ └── MANIFEST.MF │ ├── com │ └── github │ │ ├── neo │ │ └── core │ │ │ ├── ContractParameterType.java │ │ │ ├── NeoRpc.java │ │ │ ├── Program.java │ │ │ ├── SmartContract.java │ │ │ ├── TransactionAttribute.java │ │ │ ├── TransactionAttributeUsage.java │ │ │ ├── TransactionInput.java │ │ │ ├── TransactionOutput.java │ │ │ └── transaction │ │ │ ├── InvocationTransaction.java │ │ │ ├── PublishTransaction.java │ │ │ ├── TransactionNeo.java │ │ │ └── TransferTransaction.java │ │ └── ontio │ │ ├── OntSdk.java │ │ ├── account │ │ └── Account.java │ │ ├── common │ │ ├── Address.java │ │ ├── Common.java │ │ ├── ErrorCode.java │ │ ├── Fixed8.java │ │ ├── Helper.java │ │ ├── NotifyEventInfo.java │ │ ├── SmartCodeEvent.java │ │ ├── UInt256.java │ │ ├── UIntBase.java │ │ └── WalletQR.java │ │ ├── core │ │ ├── DataSignature.java │ │ ├── Inventory.java │ │ ├── InventoryType.java │ │ ├── Signable.java │ │ ├── VmType.java │ │ ├── asset │ │ │ ├── Contract.java │ │ │ ├── Sig.java │ │ │ ├── State.java │ │ │ ├── TransferFrom.java │ │ │ └── Transfers.java │ │ ├── block │ │ │ └── Block.java │ │ ├── globalparams │ │ │ ├── Param.java │ │ │ └── Params.java │ │ ├── governance │ │ │ ├── AuthorizeInfo.java │ │ │ ├── Configuration.java │ │ │ ├── GlobalParam.java │ │ │ ├── GlobalParam1.java │ │ │ ├── GlobalParam2.java │ │ │ ├── GovernanceView.java │ │ │ ├── InputPeerPoolMapParam.java │ │ │ ├── NodeToSideChainParams.java │ │ │ ├── PeerPoolItem.java │ │ │ └── SplitCurve.java │ │ ├── ontid │ │ │ ├── Attribute.java │ │ │ ├── Group.java │ │ │ └── Signer.java │ │ ├── payload │ │ │ ├── Bookkeeper.java │ │ │ ├── BookkeeperAction.java │ │ │ ├── Bookkeeping.java │ │ │ ├── DeployCode.java │ │ │ ├── DeployWasmCode.java │ │ │ ├── EIP155.java │ │ │ ├── Enrollment.java │ │ │ ├── InvokeCode.java │ │ │ ├── InvokeWasmCode.java │ │ │ └── Vote.java │ │ ├── program │ │ │ ├── Program.java │ │ │ └── ProgramInfo.java │ │ ├── scripts │ │ │ ├── ScriptBuilder.java │ │ │ ├── ScriptOp.java │ │ │ └── WasmScriptBuilder.java │ │ ├── sidechaingovernance │ │ │ ├── InflationParam.java │ │ │ ├── NodeToSideChainParams.java │ │ │ ├── QuitSideChainParam.java │ │ │ ├── RegisterSideChainParam.java │ │ │ ├── SideChain.java │ │ │ ├── SideChainNodeInfo.java │ │ │ └── SwapParam.java │ │ └── transaction │ │ │ ├── Attribute.java │ │ │ ├── AttributeUsage.java │ │ │ ├── Transaction.java │ │ │ └── TransactionType.java │ │ ├── crypto │ │ ├── AES.java │ │ ├── Base58.java │ │ ├── Curve.java │ │ ├── Digest.java │ │ ├── ECC.java │ │ ├── KeyType.java │ │ ├── MerkleTree.java │ │ ├── MnemonicCode.java │ │ ├── Signature.java │ │ ├── SignatureHandler.java │ │ ├── SignatureScheme.java │ │ └── bip32 │ │ │ ├── Bitcoin.java │ │ │ ├── ByteArrayReader.java │ │ │ ├── ByteArrayWriter.java │ │ │ ├── CKDpriv.java │ │ │ ├── CKDpub.java │ │ │ ├── Derivation.java │ │ │ ├── Deserializer.java │ │ │ ├── HdKey.java │ │ │ ├── HdPrivateKey.java │ │ │ ├── HdPrivateKeyDeserializer.java │ │ │ ├── HdPublicKey.java │ │ │ ├── HdPublicKeyDeserializer.java │ │ │ ├── Network.java │ │ │ ├── Secp256r1SC.java │ │ │ ├── Serializer.java │ │ │ └── derivation │ │ │ ├── CharSequenceDerivation.java │ │ │ ├── CkdFunction.java │ │ │ ├── CkdFunctionDerive.java │ │ │ ├── CkdFunctionResultCacheDecorator.java │ │ │ ├── Derivation.java │ │ │ └── Derive.java │ │ ├── io │ │ ├── BinaryReader.java │ │ ├── BinaryWriter.java │ │ ├── Serializable.java │ │ └── utils.java │ │ ├── merkle │ │ ├── MerkleVerifier.java │ │ └── TreeHasher.java │ │ ├── network │ │ ├── connect │ │ │ ├── AbstractConnector.java │ │ │ └── IConnector.java │ │ ├── exception │ │ │ ├── ConnectorException.java │ │ │ ├── RestfulException.java │ │ │ └── RpcException.java │ │ ├── rest │ │ │ ├── Interfaces.java │ │ │ ├── RestClient.java │ │ │ ├── Result.java │ │ │ ├── UrlConsts.java │ │ │ ├── X509.java │ │ │ └── http.java │ │ ├── rpc │ │ │ ├── Interfaces.java │ │ │ └── RpcClient.java │ │ └── websocket │ │ │ ├── MsgQueue.java │ │ │ ├── Result.java │ │ │ └── WebsocketClient.java │ │ ├── ontid │ │ ├── ALG.java │ │ ├── CredentialStatus.java │ │ ├── CredentialStatusType.java │ │ ├── OntId2.java │ │ ├── OntIdPubKey.java │ │ ├── OntIdSigner.java │ │ ├── Proof.java │ │ ├── ProofPurpose.java │ │ ├── PubKeyType.java │ │ ├── PubKeyTypeFactory.java │ │ ├── README.md │ │ ├── SignRequest.java │ │ ├── Util.java │ │ ├── VerifiableCredential.java │ │ ├── VerifiablePresentation.java │ │ ├── how to use.md │ │ ├── jwt │ │ │ ├── JWTCredential.java │ │ │ ├── JWTHeader.java │ │ │ ├── JWTPayload.java │ │ │ ├── JWTVC.java │ │ │ └── JWTVP.java │ │ └── roles.png │ │ ├── sdk │ │ ├── claim │ │ │ └── Claim.java │ │ ├── exception │ │ │ ├── SDKException.java │ │ │ └── SDKRuntimeException.java │ │ ├── info │ │ │ ├── AccountInfo.java │ │ │ └── IdentityInfo.java │ │ ├── manager │ │ │ ├── ConnectMgr.java │ │ │ ├── ECIES.java │ │ │ ├── SignServer.java │ │ │ └── WalletMgr.java │ │ └── wallet │ │ │ ├── Account.java │ │ │ ├── Control.java │ │ │ ├── Identity.java │ │ │ ├── Scrypt.java │ │ │ └── Wallet.java │ │ ├── sidechain │ │ ├── SidechainVm.java │ │ └── smartcontract │ │ │ ├── governance │ │ │ └── Governance.java │ │ │ └── ongx │ │ │ ├── OngX.java │ │ │ └── Swap.java │ │ └── smartcontract │ │ ├── NativeVm.java │ │ ├── NeoVm.java │ │ ├── Vm.java │ │ ├── WasmVm.java │ │ ├── nativevm │ │ ├── Auth.java │ │ ├── GlobalParams.java │ │ ├── Governance.java │ │ ├── Ong.java │ │ ├── OngV2.java │ │ ├── Ont.java │ │ ├── OntId.java │ │ ├── OntV2.java │ │ ├── SideChainGovernance.java │ │ └── abi │ │ │ ├── AbiEvent.java │ │ │ ├── AbiFunction.java │ │ │ ├── AbiInfo.java │ │ │ ├── NativeBuildParams.java │ │ │ ├── Parameter.java │ │ │ ├── Struct.java │ │ │ └── SubType.java │ │ └── neovm │ │ ├── CredentialRecord.java │ │ ├── Nep5.java │ │ ├── Oep4.java │ │ ├── Oep5.java │ │ ├── Oep8.java │ │ ├── Record.java │ │ ├── abi │ │ ├── AbiEvent.java │ │ ├── AbiFunction.java │ │ ├── AbiInfo.java │ │ ├── BuildParams.java │ │ ├── Parameter.java │ │ └── Struct.java │ │ ├── oep5 │ │ └── Oep5Param.java │ │ └── oep8 │ │ ├── Oep8State.java │ │ └── TransferFrom.java │ └── demo │ ├── AccountDemo.java │ ├── ApiDemo.java │ ├── AuthDemo.java │ ├── ChangellyDemo.java │ ├── ClaimDemo.java │ ├── CreateManyTx.java │ ├── CredentialRecordTxDemo.java │ ├── Demo.java │ ├── DeployCodeDemo.java │ ├── ECIESDemo.java │ ├── ExchangeDemo.java │ ├── GlobalParamDemo.java │ ├── GovernanceDemo.java │ ├── GovernanceDemo2.java │ ├── MakeTxWithJsonDemo.java │ ├── MakeTxWithoutWalletDemo.java │ ├── MnemonicDemo.java │ ├── MutiSignDemo.java │ ├── NativeOntIdDemo.java │ ├── NeoVmDemo.java │ ├── NeoVmDemo3.java │ ├── Nep5Demo.java │ ├── NewOntIdDemo.java │ ├── Oep4Demo.java │ ├── Oep4Demo2.java │ ├── Oep4MultiTheadDemo.java │ ├── Oep5Demo.java │ ├── Oep8Demo.java │ ├── OngDemo.java │ ├── OngDemo2.java │ ├── OngXDemo.java │ ├── OntAssetSM2Demo.java │ ├── OntDemo.java │ ├── OntId2Demo.java │ ├── PayForUserDemo.java │ ├── PunicaInitDemo.java │ ├── QrCodeDemo.java │ ├── RecordTxDemo.java │ ├── SideChainGovernanceDemo.java │ ├── SignServerDemo.java │ ├── SignatureDemo.java │ ├── VerifyTxSignatureDemo.java │ ├── WalletDemo.java │ ├── WebsocketDemo.java │ ├── ledger │ ├── P2pDemo.java │ ├── StoreDemo.java │ ├── common │ │ ├── BlockHeader.java │ │ ├── BookkeeperState.java │ │ ├── DataEntryPrefix.java │ │ ├── ExecuteNotify.java │ │ └── NotifyEventInfo.java │ ├── p2p │ │ ├── BlkHeader.java │ │ ├── DataReq.java │ │ ├── HeadersReq.java │ │ ├── Message.java │ │ ├── MessageHeader.java │ │ ├── PingReq.java │ │ ├── PongRsp.java │ │ └── VersionReq.java │ └── store │ │ ├── BlockStore.java │ │ ├── EventStore.java │ │ ├── LedgerStore.java │ │ └── StateStore.java │ ├── merkleProofDemo.java │ ├── neo │ ├── DeployDemo.java │ ├── NeoTransferDemo.java │ └── Nep5TransferDemo.java │ └── vmtest │ ├── VmDemo.java │ ├── VmDemo2.java │ ├── types │ ├── ArrayItem.java │ ├── BoolItem.java │ ├── ByteArrayItem.java │ ├── IntegerItem.java │ ├── InteropItem.java │ ├── MapItem.java │ ├── StackItems.java │ └── StructItem.java │ ├── utils │ ├── Config.java │ ├── PushData.java │ ├── Service.java │ ├── ServiceMap.java │ └── VmReader.java │ └── vm │ ├── ExecutionContext.java │ ├── ExecutionEngine.java │ ├── OpExec.java │ ├── OpExecList.java │ ├── RandomAccessStack.java │ └── VMState.java └── test └── java └── com └── github └── ontio ├── OntSdkTest.java ├── SmokeTest.java ├── account └── AccountTest.java ├── common ├── AddressTest.java └── HelperTest.java ├── core ├── VmTypeTest.java ├── asset │ ├── ContractTest.java │ └── StateTest.java ├── block │ └── BlockTest.java ├── payload │ ├── DeployCodeTest.java │ ├── InvokeCodeTest.java │ └── InvokeWasmCodeTest.java ├── scripts │ ├── ScriptBuilderTest.java │ └── WasmScriptBuilderTest.java └── transaction │ └── TransactionTest.java ├── crypto ├── HdPrivateKeyTest.java ├── HdPublicKeyTest.java └── MnemonicTest.java ├── io ├── BinaryReaderTest.java └── BinaryWriterTest.java ├── merkle └── MerkleVerifierTest.java ├── ontid ├── OntId2Test.java └── jwt │ └── JWTCredentialTest.java ├── sdk ├── manager │ ├── ConnectMgrTest.java │ ├── OntAssetTxTest.java │ ├── OntIdTxTest.java │ ├── RecordTxTest.java │ ├── SmartcodeTxTest.java │ └── WalletMgrTest.java └── wallet │ └── WalletTest.java └── smartcontract ├── NeoVmTest.java ├── VmTest.java ├── WasmVmTest.java └── nativevm ├── AuthTest.java ├── GovernanceTest.java ├── NativeOntIdTxTest.java ├── OngTest.java ├── OngV2Test.java └── OntV2Test.java /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .classpath 3 | .settings 4 | *.ipr 5 | .idea 6 | *.iws 7 | *.iml 8 | *~ 9 | .DS_Store 10 | /target 11 | test-output 12 | .externalToolBuilders 13 | coverage-error.log 14 | NeoVmTest.json 15 | .gradle 16 | build 17 | wallet.json -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'maven' 3 | 4 | group = 'ontology-sdk-java' 5 | version = '1.0-SNAPSHOT' 6 | 7 | description = "Ontology Java SDK" 8 | 9 | sourceCompatibility = 1.8 10 | targetCompatibility = 1.8 11 | tasks.withType(JavaCompile) { 12 | options.encoding = 'UTF-8' 13 | } 14 | 15 | 16 | repositories { 17 | maven { url "https://jcenter.bintray.com/" } 18 | } 19 | 20 | dependencies { 21 | compile group: 'io.github.novacrypto', name: 'BIP32', version: '0.0.9' 22 | compile group: 'io.github.novacrypto', name: 'BIP39', version: '0.1.9' 23 | compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.6.0' 24 | compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.59' 25 | compile group: 'com.alibaba', name: 'fastjson', version: '1.2.51' 26 | compile group: 'org.fusesource.leveldbjni', name: 'leveldbjni-all', version: '1.8' 27 | testCompile group: 'junit', name: 'junit', version: '4.12' 28 | } 29 | -------------------------------------------------------------------------------- /docs/cn/README.md: -------------------------------------------------------------------------------- 1 |

Ontology Java SDK 介绍

2 | 3 |

Version 1.0.0

4 | 5 | [English](../en/README.md) / 中文 6 | 7 | # 总体介绍 8 | 9 | 该项目是本体官方Java SDK,它是一个综合性SDK,目前支持:本地钱包管理、数字身份管理、数字资产管理、智能合约部署和调用、与节点通信等。未来还将支持更丰富的功能和应用。 10 | 11 | ## 主要功能 12 | 13 | - [介绍](sdk_get_start.md) 14 | - [接口基本信息](interface.md) 15 | - [区块链节点基本操作](basic.md) 16 | - [钱包文件及规范](Wallet_File_Specification_cn.md) 17 | - [数字身份及可信声明管理](identity_claim.md) 18 | - [数字资产](asset.md) 19 | - [数字存证](attest.md) 20 | - [权限管理](auth.md) 21 | - [智能合约部署和调用](smartcontract.md) 22 | - [错误码](errorcode.md) 23 | - [API 文档](https://apidoc.ont.io/javasdk/) 24 | 25 | ## 代码结构说明: 26 | 27 | * acount:账号相关操作,如生成公私钥 28 | * common:通用基础接口 29 | * core:核心层,包括合约、交易、签名等 30 | * crypto:加密相关,如ECC/SM 31 | * io:io操作 32 | * network:restful\rpc\websocket与链通信接口 33 | * sdk:对SDK底层做封装、Info信息、通信管理、Claim管理、钱包管理、异常类。 34 | * ontsdk类:提供管理器和交易实例,管理器包括:walletMgr、connManager。 35 | * walletMgr钱包管理器主要管理数字身份及数字资产账户,用户向链上发送交易需要私钥做签名。 36 | * connManager与链上通信管理。任何发送交易和查询都需要通过连接管理器。 37 | 38 | ## 安装说明 39 | 40 | ### 请配置JDK 8的开发环境 41 | 42 | > **注意:** SDK用的key的长度超过128位,由于java的安全策略文件对key的长度的限制,需要下载local_policy.jar和US_export_policy.jar这两个jar包,替换JRE库${java_home}/jre/lib/security目录下对应的jar包。 43 | 44 | jar包下载地址: 45 | 46 | >http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html 47 | 48 | 49 | ### Build 50 | 51 | ``` 52 | $ mvn clean install 53 | ``` 54 | 55 | ### 引入依赖 56 | 57 | 58 | ``` 59 | 60 | 61 | com.github.ontio 62 | ontology-sdk-java 63 | 1.0.16 64 | 65 | ``` 66 | 67 | ### 预准备 68 | 69 | * 启动[Ontology节点](https://github.com/ontio/ontology/releases),无论是主网、测试网、私网都可以。确保rpc端口可以访问,并且确保SDK可以连接RPC服务器。 70 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ontio/ontology-java-sdk/8a35a0823068bcb035f1229a290cc65acdd49263/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /ongx.dat: -------------------------------------------------------------------------------- 1 | {"accounts":[{"address":"AHX1wzvdw9Yipk7E9MuLY4GGX4Ym9tHeDe","algorithm":"ECDSA","enc-alg":"aes-256-gcm","hash":"sha256","isDefault":true,"key":"8p2q0vLRqyfKmFHhnjUYVWOm12kPm78JWqzkTOi9rrFMBz624KjhHQJpyPmiSSOa","label":"","lock":false,"parameters":{"curve":"P-256"},"publicKey":"02b7dcd1775031ce29f2cf07e51a15ed47debd39f93d3cea1dcb667951ecb834c6","salt":"KbiCUr53CZUfKG1M3Gojjw==","signatureScheme":"SHA256withECDSA"}],"createTime":"","defaultAccountAddress":"","defaultOntid":"did:ont:Abrc5byDEZm1CnQb3XjAosEt34DD4w5Z1o","identities":[{"controls":[{"address":"Abrc5byDEZm1CnQb3XjAosEt34DD4w5Z1o","algorithm":"ECDSA","enc-alg":"aes-256-gcm","hash":"sha256","id":"keys-1","key":"GGbKcJHF+/2o+xAsPINrsT2G6gnGnUX+JFmSRkEy0fOJoQTe//m7ifBqEITQY1pD","parameters":{"curve":"secp256r1"},"publicKey":"028c928fdc51a274d25fbbffb6837160a19d1589c3fce0566b8779bb8c532042de","salt":"3Cheywna04PemNPW7P8Nhw=="}],"isDefault":true,"label":"7b5e54d1","lock":false,"ontid":"did:ont:Abrc5byDEZm1CnQb3XjAosEt34DD4w5Z1o"},{"controls":[{"address":"ARiwjLzjzLKZy8V43vm6yUcRG9b56DnZtY","algorithm":"ECDSA","enc-alg":"aes-256-gcm","hash":"sha256","id":"keys-1","key":"CklRR+FR/apI2nZ3jJ6Adk9Z9E1WOX8XnIbse6akPgxgVNnT07iEYnXcBilJwyCe","parameters":{"curve":"secp256r1"},"publicKey":"03730dc69a695eec02dd0305d21610b94b4bf712c2842e47816c430312a2af0cbc","salt":"Td55sZftThJzZ45aLQhqjg=="}],"isDefault":false,"label":"aa969f6c","lock":false,"ontid":"did:ont:ARiwjLzjzLKZy8V43vm6yUcRG9b56DnZtY"}],"name":"MyWallet","scrypt":{"dkLen":64,"n":16384,"p":8,"r":8},"version":"1.1"} -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'ontology-sdk-java' 2 | -------------------------------------------------------------------------------- /src/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: demo.Demo 3 | 4 | -------------------------------------------------------------------------------- /src/main/java/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: demo.Demo 3 | 4 | -------------------------------------------------------------------------------- /src/main/java/com/github/neo/core/ContractParameterType.java: -------------------------------------------------------------------------------- 1 | package com.github.neo.core; 2 | 3 | /** 4 | * 5 | */ 6 | public enum ContractParameterType { 7 | /** 8 | * 9 | */ 10 | Signature(0x00), 11 | /** 12 | * 13 | */ 14 | Boolean(0x01), 15 | /** 16 | * 17 | */ 18 | Integer(0x02), 19 | /** 20 | * 21 | */ 22 | Hash160(0x03), 23 | /** 24 | * 25 | */ 26 | Hash256(0x04), 27 | /** 28 | * 29 | */ 30 | ByteArray(0x05), 31 | PublicKey(0x06), 32 | String(0x07), 33 | Array(0x10), 34 | InteropInterface(0xf0), 35 | Void(0xff); 36 | 37 | private byte value; 38 | 39 | ContractParameterType(int v) { 40 | value = (byte) v; 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/com/github/neo/core/Program.java: -------------------------------------------------------------------------------- 1 | package com.github.neo.core; 2 | 3 | 4 | import com.github.ontio.common.Common; 5 | import com.github.ontio.common.ErrorCode; 6 | import com.github.ontio.common.Helper; 7 | import com.github.ontio.core.scripts.ScriptBuilder; 8 | import com.github.ontio.core.scripts.ScriptOp; 9 | import com.github.ontio.crypto.ECC; 10 | import com.github.ontio.crypto.KeyType; 11 | import com.github.ontio.io.BinaryReader; 12 | import com.github.ontio.io.BinaryWriter; 13 | import com.github.ontio.io.Serializable; 14 | import com.github.ontio.sdk.exception.SDKException; 15 | import org.bouncycastle.math.ec.ECPoint; 16 | 17 | import java.io.IOException; 18 | import java.math.BigInteger; 19 | import java.util.Arrays; 20 | 21 | /** 22 | * 23 | */ 24 | public class Program implements Serializable { 25 | public byte[] parameter; 26 | public byte[] code; 27 | public Program(){} 28 | @Override 29 | public void deserialize(BinaryReader reader) throws IOException { 30 | parameter = reader.readVarBytes(); // sign data 31 | code = reader.readVarBytes(); // pubkey 32 | } 33 | 34 | @Override 35 | public void serialize(BinaryWriter writer) throws IOException { 36 | writer.writeVarBytes(parameter); 37 | writer.writeVarBytes(code); 38 | } 39 | public static byte[] ProgramFromParams(byte[][] sigData) throws IOException { 40 | return com.github.ontio.core.program.Program.ProgramFromParams(sigData); 41 | } 42 | public static byte[] ProgramFromPubKey(byte[] publicKey) throws Exception { 43 | return com.github.ontio.core.program.Program.ProgramFromPubKey(publicKey); 44 | } 45 | public static byte[] ProgramFromMultiPubKey(int m, byte[]... publicKeys) throws Exception { 46 | return com.github.ontio.core.program.Program.ProgramFromMultiPubKey(m,publicKeys); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/neo/core/TransactionAttribute.java: -------------------------------------------------------------------------------- 1 | package com.github.neo.core; 2 | 3 | 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.io.Serializable; 7 | 8 | import java.io.IOException; 9 | import java.util.Arrays; 10 | 11 | /** 12 | * 13 | */ 14 | public class TransactionAttribute implements Serializable { 15 | /** 16 | * 17 | */ 18 | public TransactionAttributeUsage usage; 19 | /** 20 | * 21 | */ 22 | public byte[] data; 23 | 24 | /** 25 | * 26 | */ 27 | @Override 28 | public void serialize(BinaryWriter writer) throws IOException { 29 | // usage 30 | writer.writeByte(usage.value()); 31 | // data 32 | if (usage == TransactionAttributeUsage.Script){ 33 | writer.write(data); 34 | }else if( usage == TransactionAttributeUsage.DescriptionUrl 35 | || usage == TransactionAttributeUsage.Description 36 | || usage == TransactionAttributeUsage.Nonce) { 37 | writer.writeVarBytes(data); 38 | } else { 39 | throw new IOException(); 40 | } 41 | } 42 | 43 | @Override 44 | public void deserialize(BinaryReader reader) throws IOException { 45 | // usage 46 | usage = TransactionAttributeUsage.valueOf(reader.readByte()); 47 | // data 48 | if (usage == TransactionAttributeUsage.Script){ 49 | data = reader.readBytes(20); 50 | }else if(usage == TransactionAttributeUsage.DescriptionUrl 51 | || usage == TransactionAttributeUsage.Description 52 | || usage == TransactionAttributeUsage.Nonce) { 53 | data = reader.readVarBytes(255); 54 | } else { 55 | throw new IOException(); 56 | } 57 | } 58 | 59 | 60 | @Override 61 | public String toString() { 62 | return "TransactionAttribute [usage=" + usage + ", data=" 63 | + Arrays.toString(data) + "]"; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/github/neo/core/TransactionAttributeUsage.java: -------------------------------------------------------------------------------- 1 | package com.github.neo.core; 2 | 3 | /** 4 | * 5 | */ 6 | public enum TransactionAttributeUsage { 7 | 8 | Nonce(0x00), 9 | /** 10 | * 11 | */ 12 | Script(0x20), 13 | 14 | DescriptionUrl(0x81), 15 | Description(0x90), 16 | 17 | ; 18 | private byte value; 19 | 20 | TransactionAttributeUsage(int v) { 21 | value = (byte)v; 22 | } 23 | 24 | public byte value() { 25 | return value; 26 | } 27 | 28 | public static TransactionAttributeUsage valueOf(byte v) { 29 | for (TransactionAttributeUsage e : TransactionAttributeUsage.values()) { 30 | if (e.value == v) { 31 | return e; 32 | } 33 | } 34 | throw new IllegalArgumentException(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/github/neo/core/TransactionInput.java: -------------------------------------------------------------------------------- 1 | package com.github.neo.core; 2 | 3 | 4 | import com.github.ontio.common.UInt256; 5 | import com.github.ontio.io.BinaryReader; 6 | import com.github.ontio.io.BinaryWriter; 7 | import com.github.ontio.io.Serializable; 8 | 9 | import java.io.IOException; 10 | 11 | /** 12 | * 13 | */ 14 | public class TransactionInput implements Serializable { 15 | /** 16 | * 17 | */ 18 | public UInt256 prevHash; 19 | /** 20 | * 21 | */ 22 | public short prevIndex; 23 | 24 | public TransactionInput() { 25 | } 26 | 27 | public TransactionInput(UInt256 prevHash, int prevIndex) { 28 | this.prevHash = prevHash; 29 | this.prevIndex = (short) prevIndex; 30 | } 31 | 32 | @Override 33 | public boolean equals(Object obj) { 34 | if (this == obj) { 35 | return true; 36 | } 37 | if (null == obj) { 38 | return false; 39 | } 40 | if (!(obj instanceof TransactionInput)) { 41 | return false; 42 | } 43 | TransactionInput other = (TransactionInput) obj; 44 | return prevHash.equals(other.prevHash) && prevIndex == other.prevIndex; 45 | } 46 | 47 | @Override 48 | public int hashCode() { 49 | return prevHash.hashCode() + prevIndex; 50 | } 51 | 52 | /** 53 | * 54 | */ 55 | @Override 56 | public void deserialize(BinaryReader reader) throws IOException { 57 | try { 58 | prevHash = reader.readSerializable(UInt256.class); 59 | prevIndex = reader.readShort(); 60 | // prevIndex = (short) reader.readVarInt(); 61 | } catch (InstantiationException | IllegalAccessException e) { 62 | } 63 | } 64 | @Override 65 | public void serialize(BinaryWriter writer) throws IOException { 66 | writer.writeSerializable(prevHash); 67 | writer.writeShort(prevIndex); 68 | // writer.writeVarInt(prevIndex); 69 | } 70 | 71 | 72 | @Override 73 | public String toString() { 74 | return "TransactionInput [prevHash=" + prevHash + ", prevIndex=" 75 | + prevIndex + "]"; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/github/neo/core/TransactionOutput.java: -------------------------------------------------------------------------------- 1 | package com.github.neo.core; 2 | 3 | 4 | 5 | import com.github.ontio.common.Address; 6 | import com.github.ontio.common.Fixed8; 7 | import com.github.ontio.common.UInt256; 8 | import com.github.ontio.io.BinaryReader; 9 | import com.github.ontio.io.BinaryWriter; 10 | import com.github.ontio.io.Serializable; 11 | 12 | import java.io.IOException; 13 | 14 | /** 15 | * 16 | */ 17 | public class TransactionOutput implements Serializable { 18 | /** 19 | * 20 | */ 21 | public UInt256 assetId; 22 | /** 23 | * 24 | */ 25 | public Fixed8 value; 26 | /** 27 | * 28 | */ 29 | public Address scriptHash; 30 | 31 | /** 32 | * byte 33 | */ 34 | @Override 35 | public void serialize(BinaryWriter writer) throws IOException { 36 | writer.writeSerializable(assetId); 37 | writer.writeSerializable(value); 38 | writer.writeSerializable(scriptHash); 39 | } 40 | 41 | @Override 42 | public void deserialize(BinaryReader reader) throws IOException { 43 | try { 44 | assetId = reader.readSerializable(UInt256.class); 45 | value = reader.readSerializable(Fixed8.class); 46 | scriptHash = reader.readSerializable(Address.class); 47 | } catch (InstantiationException | IllegalAccessException e) { 48 | throw new IOException(); 49 | } 50 | } 51 | 52 | 53 | @Override 54 | public String toString() { 55 | return "TransactionOutput [assetId=" + assetId + ", value=" + value 56 | + ", scriptHash=" + scriptHash + "]"; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/github/neo/core/transaction/InvocationTransaction.java: -------------------------------------------------------------------------------- 1 | package com.github.neo.core.transaction; 2 | 3 | 4 | 5 | import com.github.ontio.common.Address; 6 | import com.github.ontio.common.Fixed8; 7 | import com.github.ontio.core.transaction.TransactionType; 8 | import com.github.ontio.io.BinaryReader; 9 | import com.github.ontio.io.BinaryWriter; 10 | 11 | 12 | import java.io.IOException; 13 | 14 | public class InvocationTransaction extends TransactionNeo { 15 | public byte[] script; 16 | public Fixed8 gas; 17 | 18 | public InvocationTransaction() { 19 | super(TransactionType.InvokeCode); 20 | } 21 | 22 | @Override 23 | protected void deserializeExclusiveData(BinaryReader reader) throws IOException { 24 | try { 25 | script = reader.readVarBytes(); 26 | gas = reader.readSerializable(Fixed8.class); 27 | } catch (Exception e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | @Override 32 | protected void serializeExclusiveData(BinaryWriter writer) throws IOException { 33 | writer.writeVarBytes(script); 34 | writer.writeSerializable(gas); 35 | } 36 | @Override 37 | public Address[] getAddressU160ForVerifying() { 38 | return null; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/neo/core/transaction/TransferTransaction.java: -------------------------------------------------------------------------------- 1 | package com.github.neo.core.transaction; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.core.transaction.TransactionType; 5 | 6 | public class TransferTransaction extends TransactionNeo { 7 | 8 | public TransferTransaction() { 9 | super(TransactionType.TransferTransaction); 10 | } 11 | @Override 12 | public Address[] getAddressU160ForVerifying() { 13 | return null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/common/Common.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.common; 21 | 22 | import com.github.ontio.crypto.ECC; 23 | 24 | import java.io.FileWriter; 25 | import java.io.IOException; 26 | import java.io.PrintWriter; 27 | import java.text.SimpleDateFormat; 28 | import java.util.Date; 29 | 30 | 31 | public abstract class Common implements AutoCloseable { 32 | public static String didont = "did:ont:"; 33 | public static final int MULTI_SIG_MAX_PUBKEY_SIZE = 16; 34 | public static final int TX_MAX_SIG_SIZE = 16; 35 | 36 | public static byte[] generateKey64Bit() { 37 | return ECC.generateKey(64); 38 | } 39 | 40 | public static String currentTime() { 41 | return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); 42 | } 43 | 44 | private static String now() { 45 | return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/common/NotifyEventInfo.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.common; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.annotation.JSONField; 5 | import demo.vmtest.types.StackItems; 6 | 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | public class NotifyEventInfo { 12 | public List States; 13 | public String ContractAddress; 14 | public List getStates() { 15 | return States; 16 | } 17 | 18 | public void setStates(List states) { 19 | States = states; 20 | } 21 | 22 | public String getContractAddress() { 23 | return ContractAddress; 24 | } 25 | 26 | public void setContractAddress(String contractAddress) { 27 | ContractAddress = contractAddress; 28 | } 29 | 30 | public String toJson(){ 31 | Map map = new HashMap(); 32 | map.put("States", States); 33 | map.put("ContractAddress", ContractAddress); 34 | return JSON.toJSONString(map); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/common/SmartCodeEvent.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.common; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | public class SmartCodeEvent { 8 | public String TxHash; 9 | public int State; 10 | public long GasConsumed; 11 | public List Notify; 12 | 13 | public String getTxHash() { 14 | return TxHash; 15 | } 16 | 17 | public void setTxHash(String txHash) { 18 | TxHash = txHash; 19 | } 20 | 21 | public int getState() { 22 | return State; 23 | } 24 | 25 | public void setState(int state) { 26 | State = state; 27 | } 28 | 29 | public long getGasConsumed() { 30 | return GasConsumed; 31 | } 32 | 33 | public void setGasConsumed(long gasConsumed) { 34 | GasConsumed = gasConsumed; 35 | } 36 | 37 | public List getNotify() { 38 | return Notify; 39 | } 40 | 41 | public void setNotify(List notify) { 42 | Notify = notify; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/Inventory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core; 21 | 22 | import com.github.ontio.common.UInt256; 23 | import com.github.ontio.crypto.Digest; 24 | 25 | public abstract class Inventory implements Signable { 26 | //[NonSerialized] 27 | private UInt256 _hash = null; 28 | 29 | public UInt256 hash() { 30 | if (_hash == null) { 31 | _hash = new UInt256(Digest.hash256(getHashData())); 32 | } 33 | return _hash; 34 | } 35 | 36 | public abstract InventoryType inventoryType(); 37 | 38 | public abstract boolean verify(); 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/InventoryType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core; 21 | 22 | 23 | public enum InventoryType { 24 | TX(0x01), 25 | Block(0x02), 26 | Consensus(0xe0), 27 | ; 28 | private byte value; 29 | private InventoryType(int v) { 30 | value = (byte)v; 31 | } 32 | public int value() { 33 | return value; 34 | } 35 | 36 | public static InventoryType from(byte b) { 37 | for(InventoryType t: InventoryType.values()) { 38 | if(t.value() == b) { 39 | return t; 40 | } 41 | } 42 | throw new IllegalArgumentException(); 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/Signable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core; 21 | 22 | import java.io.ByteArrayOutputStream; 23 | import java.io.IOException; 24 | import com.github.ontio.account.Account; 25 | import com.github.ontio.crypto.Digest; 26 | import com.github.ontio.crypto.SignatureScheme; 27 | import com.github.ontio.io.BinaryReader; 28 | import com.github.ontio.io.BinaryWriter; 29 | import com.github.ontio.io.Serializable; 30 | 31 | import com.github.ontio.common.Address; 32 | 33 | 34 | public interface Signable extends Serializable { 35 | 36 | void deserializeUnsigned(BinaryReader reader) throws IOException; 37 | 38 | void serializeUnsigned(BinaryWriter writer) throws IOException; 39 | 40 | Address[] getAddressU160ForVerifying(); 41 | 42 | default byte[] getHashData() { 43 | try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) { 44 | try (BinaryWriter writer = new BinaryWriter(ms)) { 45 | serializeUnsigned(writer); 46 | writer.flush(); 47 | return ms.toByteArray(); 48 | } 49 | } catch (IOException ex) { 50 | throw new UnsupportedOperationException(ex); 51 | } 52 | } 53 | default byte[] sign(Account account, SignatureScheme scheme) throws Exception { 54 | return account.generateSignature(Digest.hash256(getHashData()), scheme,null); 55 | } 56 | default boolean verifySignature(Account account, byte[] data, byte[] signature) throws Exception { 57 | return account.verifySignature(Digest.hash256(Digest.sha256(data)), signature); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/VmType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core; 21 | 22 | /** 23 | * list vm types 24 | */ 25 | public enum VmType { 26 | 27 | NEOVM(0x01), 28 | WASMVM(0x03); 29 | 30 | private byte value; 31 | VmType(int v) { 32 | value = (byte)v; 33 | } 34 | public byte value() { 35 | return value; 36 | } 37 | 38 | public static VmType valueOf(byte v) { 39 | for (VmType e : VmType.values()) { 40 | if (e.value == v) { 41 | return e; 42 | } 43 | } 44 | throw new IllegalArgumentException(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/globalparams/Param.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.globalparams; 2 | 3 | 4 | public class Param { 5 | public String key; 6 | public String value; 7 | public Param(String key,String value){ 8 | this.key = key; 9 | this.value = value; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/globalparams/Params.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.globalparams; 2 | 3 | import com.github.ontio.common.Helper; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.io.Serializable; 7 | 8 | import java.io.IOException; 9 | import java.math.BigInteger; 10 | 11 | public class Params implements Serializable { 12 | public Param[] params; 13 | public Params(Param[] params) { 14 | this.params = params; 15 | } 16 | 17 | @Override 18 | public void deserialize(BinaryReader reader) throws IOException { 19 | 20 | } 21 | 22 | @Override 23 | public void serialize(BinaryWriter writer) throws IOException { 24 | long l = params.length; 25 | byte[] aa = Helper.BigIntToNeoBytes(BigInteger.valueOf(l)); 26 | String bb = Helper.toHexString(aa); 27 | writer.writeVarBytes(aa); 28 | for(int i=0;i< params.length;i++) { 29 | writer.writeVarString(params[i].key); 30 | writer.writeVarString(params[i].value); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/governance/Configuration.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.governance; 2 | 3 | import com.github.ontio.common.Helper; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.io.Serializable; 7 | import com.github.ontio.io.utils; 8 | 9 | import java.io.IOException; 10 | 11 | public class Configuration implements Serializable { 12 | public int N; 13 | public int C; 14 | public int K; 15 | public int L; 16 | public int BlockMsgDelay; 17 | public int HashMsgDelay; 18 | public int PeerHandshakeTimeout; 19 | public int MaxBlockChangeView; 20 | public Configuration(){ 21 | 22 | } 23 | public Configuration(int N,int C,int K,int L,int BlockMsgDelay, int HashMsgDelay,int PeerHandshakeTimeout, int MaxBlockChangeView){ 24 | this.N = N; 25 | this.C = C; 26 | this.K = K; 27 | this.L = L; 28 | this.BlockMsgDelay = BlockMsgDelay; 29 | this.HashMsgDelay = HashMsgDelay; 30 | this.PeerHandshakeTimeout = PeerHandshakeTimeout; 31 | this.MaxBlockChangeView = MaxBlockChangeView; 32 | } 33 | 34 | @Override 35 | public void deserialize(BinaryReader reader) throws IOException { 36 | 37 | this.N = (int)utils.readVarInt(reader); 38 | this.C = (int)utils.readVarInt(reader); 39 | this.K = (int)utils.readVarInt(reader); 40 | this.L = (int)utils.readVarInt(reader); 41 | this.BlockMsgDelay = (int)utils.readVarInt(reader); 42 | this.HashMsgDelay = (int)utils.readVarInt(reader); 43 | this.PeerHandshakeTimeout = (int)utils.readVarInt(reader); 44 | this.MaxBlockChangeView = (int)utils.readVarInt(reader); 45 | } 46 | 47 | @Override 48 | public void serialize(BinaryWriter binaryWriter) throws IOException { 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/governance/GlobalParam.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.governance; 2 | 3 | import com.github.ontio.io.BinaryReader; 4 | import com.github.ontio.io.BinaryWriter; 5 | import com.github.ontio.io.Serializable; 6 | import com.github.ontio.io.utils; 7 | 8 | import java.io.IOException; 9 | 10 | public class GlobalParam implements Serializable { 11 | public int candidateFeeSplitNum; 12 | public int A; 13 | public int B; 14 | public int yita; 15 | public GlobalParam(){} 16 | public GlobalParam(int CandidateFeeSplitNum, int A, int B, int Yita){ 17 | this.candidateFeeSplitNum = CandidateFeeSplitNum; 18 | this.A = A; 19 | this.B = B; 20 | this.yita = Yita; 21 | } 22 | 23 | @Override 24 | public void deserialize(BinaryReader reader) throws IOException { 25 | this.candidateFeeSplitNum = (int)utils.readVarInt(reader); 26 | this.A = (int)utils.readVarInt(reader); 27 | this.B = (int)utils.readVarInt(reader); 28 | this.yita = (int)utils.readVarInt(reader); 29 | } 30 | 31 | @Override 32 | public void serialize(BinaryWriter binaryWriter) throws IOException { 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/governance/GlobalParam1.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.governance; 2 | 3 | import com.github.ontio.io.BinaryReader; 4 | import com.github.ontio.io.BinaryWriter; 5 | import com.github.ontio.io.Serializable; 6 | import com.github.ontio.io.utils; 7 | 8 | import java.io.IOException; 9 | 10 | public class GlobalParam1 implements Serializable { 11 | public long candidateFee; 12 | public int minInitStake; 13 | public int candidateNum; 14 | public int posLimit; 15 | public int A; 16 | public int B; 17 | public int yita; 18 | public int penalty; 19 | public GlobalParam1(){} 20 | public GlobalParam1(int CandidateFeeSplitNum, int A, int B, int Yita){ 21 | this.candidateFee = CandidateFeeSplitNum; 22 | this.A = A; 23 | this.B = B; 24 | this.yita = Yita; 25 | } 26 | 27 | @Override 28 | public void deserialize(BinaryReader reader) throws IOException { 29 | this.candidateFee = utils.readVarInt(reader); 30 | this.minInitStake = (int)utils.readVarInt(reader); 31 | this.candidateNum = (int)utils.readVarInt(reader); 32 | this.posLimit = (int)utils.readVarInt(reader); 33 | this.A = (int)utils.readVarInt(reader); 34 | this.B = (int)utils.readVarInt(reader); 35 | this.yita = (int)utils.readVarInt(reader); 36 | this.penalty = (int)utils.readVarInt(reader); 37 | } 38 | 39 | @Override 40 | public void serialize(BinaryWriter binaryWriter) throws IOException { 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/governance/GlobalParam2.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.governance; 2 | 3 | import com.github.ontio.io.BinaryReader; 4 | import com.github.ontio.io.BinaryWriter; 5 | import com.github.ontio.io.Serializable; 6 | import com.github.ontio.io.utils; 7 | 8 | import java.io.IOException; 9 | 10 | public class GlobalParam2 implements Serializable { 11 | public int minAuthorizePos; 12 | public int candidateFeeSplitNum; 13 | public byte[] field1; 14 | public byte[] field2; 15 | public byte[] field3; 16 | public byte[] field4; 17 | public byte[] field5; 18 | public byte[] field6; 19 | public GlobalParam2(){ 20 | 21 | } 22 | public GlobalParam2(int minAuthorizePos, int candidateFeeSplitNum, byte[] field1, byte[] field2, byte[] field3, byte[] field4, 23 | byte[] field5, byte[] field6){ 24 | this.minAuthorizePos = minAuthorizePos; 25 | this.candidateFeeSplitNum = candidateFeeSplitNum; 26 | this.field1 = field1; 27 | this.field2 = field2; 28 | this.field3 = field3; 29 | this.field4 = field4; 30 | this.field5 = field5; 31 | this.field6 = field6; 32 | 33 | } 34 | @Override 35 | public void deserialize(BinaryReader reader) throws IOException { 36 | this.minAuthorizePos = (int)utils.readVarInt(reader); 37 | this.candidateFeeSplitNum = (int)utils.readVarInt(reader); 38 | this.field1 = reader.readVarBytes(); 39 | this.field2 = reader.readVarBytes(); 40 | this.field3 = reader.readVarBytes(); 41 | this.field4 = reader.readVarBytes(); 42 | this.field5 = reader.readVarBytes(); 43 | this.field6 = reader.readVarBytes(); 44 | 45 | } 46 | 47 | @Override 48 | public void serialize(BinaryWriter writer) throws IOException { 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/governance/GovernanceView.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.governance; 2 | 3 | import com.github.ontio.common.UInt256; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.io.Serializable; 7 | 8 | import java.io.IOException; 9 | 10 | public class GovernanceView implements Serializable { 11 | public int view; 12 | public int height; 13 | public UInt256 txhash; 14 | public GovernanceView(){ 15 | } 16 | GovernanceView(int view,int height,UInt256 txhash){ 17 | this.view = view; 18 | this.height = height; 19 | this.txhash = txhash; 20 | } 21 | @Override 22 | public void deserialize(BinaryReader reader) throws IOException { 23 | this.view = reader.readInt(); 24 | this.height = reader.readInt(); 25 | try { 26 | this.txhash = reader.readSerializable(UInt256.class); 27 | } catch (InstantiationException e) { 28 | e.printStackTrace(); 29 | } catch (IllegalAccessException e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | 34 | @Override 35 | public void serialize(BinaryWriter writer) throws IOException { 36 | writer.writeInt(view); 37 | writer.writeInt(height); 38 | writer.writeSerializable(txhash); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/governance/InputPeerPoolMapParam.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.governance; 2 | 3 | import com.github.ontio.core.sidechaingovernance.NodeToSideChainParams; 4 | 5 | import java.util.Map; 6 | 7 | public class InputPeerPoolMapParam { 8 | public Map peerPoolMap; 9 | public Map nodeInfoMap; 10 | public InputPeerPoolMapParam(Map peerPoolMap, Map nodeInfoMap){ 11 | this.peerPoolMap = peerPoolMap; 12 | this.nodeInfoMap = nodeInfoMap; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/governance/NodeToSideChainParams.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.governance; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.io.Serializable; 7 | import com.github.ontio.io.utils; 8 | 9 | import java.io.IOException; 10 | 11 | public class NodeToSideChainParams implements Serializable { 12 | public String peerPubkey; 13 | public Address address; 14 | public String sideChainId; 15 | public NodeToSideChainParams(){} 16 | 17 | @Override 18 | public void deserialize(BinaryReader reader) throws IOException { 19 | this.peerPubkey = reader.readVarString(); 20 | this.address = utils.readAddress(reader); 21 | this.sideChainId = reader.readVarString(); 22 | } 23 | 24 | @Override 25 | public void serialize(BinaryWriter binaryWriter) throws IOException { 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/governance/SplitCurve.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.governance; 2 | 3 | import com.github.ontio.common.Helper; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.io.Serializable; 7 | 8 | import java.io.IOException; 9 | import java.math.BigInteger; 10 | 11 | public class SplitCurve implements Serializable { 12 | public int[] Yi; 13 | public SplitCurve(){} 14 | public SplitCurve(int[] Yi){ 15 | this.Yi = Yi; 16 | } 17 | 18 | @Override 19 | public void deserialize(BinaryReader reader) throws IOException { 20 | byte[] nBytes = reader.readVarBytes(); 21 | BigInteger b = Helper.BigIntFromNeoBytes(nBytes); 22 | int n = b.intValue(); 23 | this.Yi = new int[n]; 24 | for(int i=0; i. 17 | * 18 | */ 19 | 20 | package com.github.ontio.core.ontid; 21 | 22 | import com.github.ontio.io.BinaryReader; 23 | import com.github.ontio.io.BinaryWriter; 24 | import com.github.ontio.io.Serializable; 25 | 26 | import java.io.IOException; 27 | 28 | public class Attribute implements Serializable { 29 | public byte[] key; 30 | public byte[] valueType; 31 | public byte[] value; 32 | public Attribute(){} 33 | public Attribute(byte[] key,byte[] valueType,byte[] value){ 34 | this.key = key; 35 | this.valueType = valueType; 36 | this.value = value; 37 | } 38 | @Override 39 | public void deserialize(BinaryReader reader) throws IOException { 40 | this.key = reader.readVarBytes(); 41 | this.valueType = reader.readVarBytes(); 42 | this.value = reader.readVarBytes(); 43 | } 44 | 45 | @Override 46 | public void serialize(BinaryWriter writer) throws IOException { 47 | writer.writeVarBytes(key); 48 | writer.writeVarBytes(valueType); 49 | writer.writeVarBytes(value); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/ontid/Group.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.ontid; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.github.ontio.common.Helper; 5 | import com.github.ontio.io.BinaryReader; 6 | import com.github.ontio.io.BinaryWriter; 7 | import com.github.ontio.io.Serializable; 8 | 9 | import java.io.ByteArrayOutputStream; 10 | import java.io.IOException; 11 | import java.math.BigInteger; 12 | 13 | public class Group implements Serializable { 14 | Object[] members; 15 | int threshold; 16 | 17 | 18 | public Group(Object[] members, int threshold) { 19 | this.members = members; 20 | this.threshold = threshold; 21 | } 22 | 23 | public String toJson() { 24 | return JSONObject.toJSONString(this); 25 | } 26 | 27 | @Override 28 | public void deserialize(BinaryReader reader) throws IOException { 29 | 30 | } 31 | 32 | @Override 33 | public void serialize(BinaryWriter writer) throws IOException { 34 | BigInteger l = BigInteger.valueOf(this.members.length); 35 | writer.writeVarBytes(Helper.BigIntToNeoBytes(l)); 36 | for (Object obj : this.members) { 37 | if (obj instanceof byte[]) { 38 | writer.writeVarBytes((byte[])obj); 39 | } else if (obj instanceof Group) { 40 | ByteArrayOutputStream ms = new ByteArrayOutputStream(); 41 | BinaryWriter writer2 = new BinaryWriter(ms); 42 | ((Group) obj).serialize(writer2); 43 | writer.writeVarBytes(ms.toByteArray()); 44 | } 45 | } 46 | BigInteger th = BigInteger.valueOf(this.threshold); 47 | writer.writeVarBytes(Helper.BigIntToNeoBytes(th)); 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/ontid/Signer.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.ontid; 2 | 3 | import com.github.ontio.common.Helper; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.io.Serializable; 7 | 8 | import java.io.IOException; 9 | import java.math.BigInteger; 10 | 11 | public class Signer implements Serializable { 12 | public byte[] id; 13 | public int index; 14 | 15 | 16 | public Signer(byte[] id, int index) { 17 | this.index = index; 18 | this.id = id; 19 | } 20 | 21 | @Override 22 | public void deserialize(BinaryReader reader) throws IOException { 23 | 24 | } 25 | 26 | @Override 27 | public void serialize(BinaryWriter writer) throws IOException { 28 | writer.writeVarBytes(this.id); 29 | BigInteger index = BigInteger.valueOf(this.index); 30 | writer.writeVarBytes(Helper.BigIntToNeoBytes(index)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/payload/BookkeeperAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core.payload; 21 | 22 | public enum BookkeeperAction { 23 | BookKeeperAction_ADD(0x00), 24 | BookKeeperAction_SUB(0x01),; 25 | private byte value; 26 | 27 | BookkeeperAction(int v) { 28 | value = (byte) v; 29 | } 30 | 31 | 32 | public static BookkeeperAction valueOf(byte v) { 33 | for (BookkeeperAction e : BookkeeperAction.values()) { 34 | if (e.value == v) { 35 | return e; 36 | } 37 | } 38 | throw new IllegalArgumentException(); 39 | } 40 | 41 | public byte value() { 42 | return value; 43 | } 44 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/payload/Bookkeeping.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core.payload; 21 | 22 | import java.io.IOException; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | import com.github.ontio.common.Address; 27 | import com.github.ontio.core.transaction.TransactionType; 28 | import com.github.ontio.core.transaction.Transaction; 29 | import com.github.ontio.io.BinaryReader; 30 | import com.github.ontio.io.BinaryWriter; 31 | 32 | public class Bookkeeping extends Transaction { 33 | private long nonce; 34 | 35 | public Bookkeeping() { 36 | super(TransactionType.Bookkeeping); 37 | } 38 | 39 | @Override 40 | protected void deserializeExclusiveData(BinaryReader reader) throws IOException { 41 | nonce = reader.readLong(); 42 | } 43 | 44 | @Override 45 | public Address[] getAddressU160ForVerifying() { 46 | return null; 47 | } 48 | 49 | @Override 50 | protected void serializeExclusiveData(BinaryWriter writer) throws IOException { 51 | writer.writeLong(nonce); 52 | } 53 | 54 | @Override 55 | public Object json() { 56 | Map obj = (Map) super.json(); 57 | Map payload = new HashMap(); 58 | payload.put("Nonce", nonce); 59 | obj.put("Payload", payload); 60 | return obj; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/payload/Enrollment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core.payload; 21 | 22 | import com.github.ontio.common.Address; 23 | import com.github.ontio.common.Helper; 24 | import com.github.ontio.core.transaction.TransactionType; 25 | import com.github.ontio.io.BinaryWriter; 26 | import com.github.ontio.core.transaction.Transaction; 27 | import com.github.ontio.crypto.ECC; 28 | import com.github.ontio.io.BinaryReader; 29 | import org.bouncycastle.math.ec.ECPoint; 30 | 31 | import java.io.IOException; 32 | import java.math.BigInteger; 33 | 34 | /** 35 | * 36 | */ 37 | public class Enrollment extends Transaction { 38 | public ECPoint pubKey; 39 | 40 | public Enrollment() { 41 | super(TransactionType.Enrollment); 42 | } 43 | 44 | @Override 45 | protected void deserializeExclusiveData(BinaryReader reader) throws IOException { 46 | try { 47 | pubKey = ECC.secp256r1.getCurve().createPoint( 48 | new BigInteger(1, reader.readVarBytes()), new BigInteger(1, reader.readVarBytes())); 49 | } catch (Exception e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | 54 | @Override 55 | public Address[] getAddressU160ForVerifying() { 56 | return null; 57 | } 58 | 59 | @Override 60 | protected void serializeExclusiveData(BinaryWriter writer) throws IOException { 61 | writer.writeVarBytes(Helper.removePrevZero(pubKey.getXCoord().toBigInteger().toByteArray())); 62 | writer.writeVarBytes(Helper.removePrevZero(pubKey.getYCoord().toBigInteger().toByteArray())); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/payload/InvokeCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core.payload; 21 | 22 | import com.github.ontio.common.Helper; 23 | import com.github.ontio.core.transaction.Transaction; 24 | import com.github.ontio.core.transaction.TransactionType; 25 | import com.github.ontio.io.BinaryReader; 26 | import com.github.ontio.io.BinaryWriter; 27 | import com.github.ontio.common.Address; 28 | import org.bouncycastle.math.ec.ECPoint; 29 | 30 | import java.io.IOException; 31 | import java.util.Arrays; 32 | import java.util.HashMap; 33 | import java.util.HashSet; 34 | import java.util.Map; 35 | 36 | public class InvokeCode extends Transaction { 37 | public byte[] code; 38 | 39 | public InvokeCode() { 40 | super(TransactionType.InvokeCode); 41 | } 42 | 43 | @Override 44 | protected void deserializeExclusiveData(BinaryReader reader) throws IOException { 45 | try { 46 | code = reader.readVarBytes(); 47 | } catch (Exception e) { 48 | e.printStackTrace(); 49 | } 50 | } 51 | 52 | @Override 53 | protected void serializeExclusiveData(BinaryWriter writer) throws IOException { 54 | writer.writeVarBytes(code); 55 | } 56 | 57 | @Override 58 | public Address[] getAddressU160ForVerifying() { 59 | return null; 60 | } 61 | 62 | @Override 63 | public Object json() { 64 | Map obj = (Map) super.json(); 65 | Map payload = new HashMap(); 66 | payload.put("Code", Helper.toHexString(code)); 67 | obj.put("Payload", payload); 68 | return obj; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/payload/InvokeWasmCode.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.payload; 2 | 3 | 4 | import com.github.ontio.common.Helper; 5 | import com.github.ontio.core.transaction.Transaction; 6 | import com.github.ontio.core.transaction.TransactionType; 7 | import com.github.ontio.io.BinaryReader; 8 | import com.github.ontio.io.BinaryWriter; 9 | 10 | import java.io.IOException; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | public class InvokeWasmCode extends Transaction { 15 | 16 | public byte[] invokeCode; 17 | 18 | public InvokeWasmCode() { 19 | super(TransactionType.InvokeWasmCode); 20 | } 21 | 22 | public InvokeWasmCode(byte[] invokeCode) { 23 | super(TransactionType.InvokeWasmCode); 24 | this.invokeCode = invokeCode; 25 | } 26 | 27 | @Override 28 | protected void deserializeExclusiveData(BinaryReader reader) throws IOException { 29 | try { 30 | invokeCode = reader.readVarBytes(); 31 | } catch (Exception e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | 36 | @Override 37 | protected void serializeExclusiveData(BinaryWriter writer) throws IOException { 38 | writer.writeVarBytes(invokeCode); 39 | } 40 | 41 | @Override 42 | public Object json() { 43 | Map obj = (Map) super.json(); 44 | Map payload = new HashMap(); 45 | payload.put("Code", Helper.toHexString(invokeCode)); 46 | obj.put("Payload", payload); 47 | return obj; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/program/ProgramInfo.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.program; 2 | 3 | public class ProgramInfo { 4 | public byte[][] publicKey; 5 | public short m; 6 | public ProgramInfo(){} 7 | public ProgramInfo(byte[][] publicKey,short m){ 8 | this.publicKey = publicKey; 9 | this.m = m; 10 | } 11 | 12 | public byte[][] getPublicKey() { 13 | return publicKey; 14 | } 15 | 16 | public void setPublicKey(byte[][] publicKey) { 17 | this.publicKey = publicKey; 18 | } 19 | 20 | public void setM(short m) { 21 | this.m = m; 22 | } 23 | public short getM() { 24 | return m; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/sidechaingovernance/InflationParam.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.sidechaingovernance; 2 | 3 | 4 | import com.github.ontio.common.Address; 5 | 6 | public class InflationParam { 7 | public String sideChainId; 8 | public Address address; 9 | public long depositAdd; 10 | public long ongPoolAdd; 11 | public InflationParam(String sideChainId, Address address, long depositAdd, long ongPoolAdd){ 12 | this.sideChainId = sideChainId; 13 | this.address = address; 14 | this.depositAdd = depositAdd; 15 | this.ongPoolAdd = ongPoolAdd; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/sidechaingovernance/NodeToSideChainParams.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.sidechaingovernance; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.io.Serializable; 7 | import com.github.ontio.io.utils; 8 | 9 | import java.io.IOException; 10 | 11 | public class NodeToSideChainParams implements Serializable { 12 | public String peerPubkey; 13 | public Address address; 14 | public String sideChainId; 15 | public NodeToSideChainParams(){} 16 | public NodeToSideChainParams(String peerPubkey, Address address, String sideChainId){ 17 | this.peerPubkey = peerPubkey; 18 | this.address = address; 19 | this.sideChainId = sideChainId; 20 | } 21 | 22 | @Override 23 | public void deserialize(BinaryReader reader) throws IOException { 24 | this.peerPubkey = reader.readVarString(); 25 | this.address = utils.readAddress(reader); 26 | this.sideChainId = reader.readVarString(); 27 | } 28 | 29 | @Override 30 | public void serialize(BinaryWriter writer) throws IOException { 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/sidechaingovernance/QuitSideChainParam.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.sidechaingovernance; 2 | 3 | 4 | import com.github.ontio.common.Address; 5 | 6 | public class QuitSideChainParam { 7 | public String sideChainID; 8 | public Address address; 9 | public QuitSideChainParam(String sideChainID, Address address){ 10 | this.sideChainID = sideChainID; 11 | this.address = address; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/sidechaingovernance/RegisterSideChainParam.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.sidechaingovernance; 2 | 3 | 4 | import com.github.ontio.common.Address; 5 | 6 | public class RegisterSideChainParam { 7 | public String sideChainID; 8 | public Address address; 9 | public int ratio; 10 | public long deposit; 11 | public long ongPool; 12 | public byte[] caller; 13 | public int keyNo; 14 | public RegisterSideChainParam(String sideChainID, Address address, int ratio, long deposit, long ongPool, byte[] caller, int keyNo){ 15 | this.sideChainID = sideChainID; 16 | this.address = address; 17 | this.ratio = ratio; 18 | this.deposit = deposit; 19 | this.ongPool = ongPool; 20 | this.caller = caller; 21 | this.keyNo = keyNo; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/sidechaingovernance/SideChain.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.sidechaingovernance; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.github.ontio.common.Address; 5 | import com.github.ontio.io.BinaryReader; 6 | import com.github.ontio.io.BinaryWriter; 7 | import com.github.ontio.io.Serializable; 8 | 9 | import java.io.IOException; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | public class SideChain implements Serializable { 14 | public String sideChainId; 15 | public Address address; 16 | public long ratio; 17 | public long deposit; 18 | public long ongNum; 19 | public long ongPool; 20 | public byte status; 21 | 22 | public SideChain(){} 23 | @Override 24 | public void deserialize(BinaryReader reader) throws IOException { 25 | this.sideChainId = reader.readVarString(); 26 | try { 27 | this.address = reader.readSerializable(Address.class); 28 | } catch (InstantiationException e) { 29 | e.printStackTrace(); 30 | } catch (IllegalAccessException e) { 31 | e.printStackTrace(); 32 | } 33 | this.ratio = reader.readLong(); 34 | this.deposit = reader.readLong(); 35 | this.ongNum = reader.readLong(); 36 | this.ongPool = reader.readLong(); 37 | this.status = reader.readByte(); 38 | } 39 | 40 | @Override 41 | public void serialize(BinaryWriter binaryWriter) throws IOException { 42 | 43 | } 44 | public String toJson(){ 45 | Map map = new HashMap<>(); 46 | map.put("sideChainId",this.sideChainId); 47 | map.put("address", this.address.toBase58()); 48 | map.put("ratio", this.ratio); 49 | map.put("deposit", this.deposit); 50 | map.put("ongNum", this.ongNum); 51 | map.put("ongPool", this.ongPool); 52 | map.put("status", this.status); 53 | return JSON.toJSONString(map); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/sidechaingovernance/SideChainNodeInfo.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.sidechaingovernance; 2 | 3 | import com.github.ontio.io.BinaryReader; 4 | import com.github.ontio.io.BinaryWriter; 5 | import com.github.ontio.io.Serializable; 6 | 7 | import java.io.IOException; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | public class SideChainNodeInfo implements Serializable { 12 | public String sideChainId; 13 | public Map nodeInfoMap; 14 | 15 | public SideChainNodeInfo(){ 16 | this.nodeInfoMap = new HashMap(); 17 | } 18 | 19 | @Override 20 | public void deserialize(BinaryReader reader) throws IOException { 21 | this.sideChainId = reader.readVarString(); 22 | int n = reader.readInt(); 23 | for(int i=0;i(); 21 | map.put("sideChainId", sideChainId); 22 | map.put("address", address.toBase58()); 23 | map.put("ongXAccount",ongXAccount); 24 | return JSON.toJSONString(map); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/transaction/AttributeUsage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core.transaction; 21 | 22 | 23 | public enum AttributeUsage { 24 | 25 | Nonce(0x00), 26 | Script(0x20), 27 | DescriptionUrl(0x81), 28 | Description(0x90), 29 | 30 | ; 31 | private byte value; 32 | 33 | AttributeUsage(int v) { 34 | value = (byte)v; 35 | } 36 | 37 | public byte value() { 38 | return value; 39 | } 40 | 41 | public static AttributeUsage valueOf(byte v) { 42 | for (AttributeUsage e : AttributeUsage.values()) { 43 | if (e.value == v) { 44 | return e; 45 | } 46 | } 47 | throw new IllegalArgumentException(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/core/transaction/TransactionType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core.transaction; 21 | 22 | /** 23 | * list transaction types 24 | */ 25 | public enum TransactionType { 26 | Bookkeeping(0x00), 27 | Bookkeeper(0x02), 28 | Claim(0x03), 29 | Enrollment(0x04), 30 | Vote(0x05), 31 | DeployCode(0xd0), 32 | InvokeCode(0xd1), 33 | InvokeWasmCode(0xd2), 34 | TransferTransaction(0x80), 35 | EIP155(0xd3); 36 | 37 | private byte value; 38 | 39 | TransactionType(int v) { 40 | value = (byte) v; 41 | } 42 | 43 | public byte value() { 44 | return value; 45 | } 46 | 47 | public static TransactionType valueOf(byte v) { 48 | for (TransactionType e : TransactionType.values()) { 49 | if (e.value == v) { 50 | return e; 51 | } 52 | } 53 | throw new IllegalArgumentException(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/Curve.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.crypto; 2 | 3 | import com.github.ontio.common.ErrorCode; 4 | import com.github.ontio.sdk.exception.SDKException; 5 | import org.bouncycastle.jce.ECNamedCurveTable; 6 | import org.bouncycastle.math.ec.ECCurve; 7 | 8 | public enum Curve { 9 | P224(1, "P-224"), 10 | P256(2, "P-256"), 11 | P384(3, "P-384"), 12 | P521(4, "P-521"), 13 | SM2P256V1(20, "sm2p256v1"), 14 | ED25519(25, "ED25519"); 15 | 16 | private int label; 17 | private String name; 18 | 19 | private Curve(int v0, String v1) { 20 | label = v0; 21 | name = v1; 22 | } 23 | 24 | public int getLabel() { 25 | return label; 26 | } 27 | @Override 28 | public String toString() { 29 | return name; 30 | } 31 | 32 | public static Curve valueOf(ECCurve v) throws Exception { 33 | for (Curve c : Curve.values()) { 34 | if (ECNamedCurveTable.getParameterSpec(c.toString()).getCurve().equals(v)) { 35 | return c; 36 | } 37 | } 38 | 39 | throw new Exception(ErrorCode.UnknownCurve); 40 | } 41 | 42 | public static Curve fromLabel(int v) throws Exception { 43 | for (Curve c : Curve.values()) { 44 | if (c.label == v) { 45 | return c; 46 | } 47 | } 48 | 49 | throw new SDKException(ErrorCode.UnknownCurveLabel); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/ECC.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.crypto; 21 | 22 | import java.security.*; 23 | 24 | import com.github.ontio.common.Helper; 25 | import org.bouncycastle.asn1.x9.*; 26 | import org.bouncycastle.crypto.params.ECDomainParameters; 27 | import org.bouncycastle.math.ec.ECPoint; 28 | 29 | public class ECC { 30 | private static final X9ECParameters secp256r1nc = ECNamedCurveTable.getByName("secp256r1"); 31 | public static final ECDomainParameters secp256r1 = new ECDomainParameters(secp256r1nc.getCurve(), secp256r1nc.getG(), secp256r1nc.getN(), secp256r1nc.getH(), secp256r1nc.getSeed()); 32 | private static final X9ECParameters sm2p256v1nc = ECNamedCurveTable.getByName("sm2p256v1"); 33 | public static final ECDomainParameters sm2p256v1 = new ECDomainParameters(sm2p256v1nc.getCurve(), sm2p256v1nc.getG(), sm2p256v1nc.getN(), sm2p256v1nc.getH(), sm2p256v1nc.getSeed()); 34 | 35 | public static int compare(ECPoint a, ECPoint b) { 36 | if (a == b) { 37 | return 0; 38 | } 39 | int result = a.getXCoord().toBigInteger().compareTo(b.getXCoord().toBigInteger()); 40 | if (result != 0) { 41 | return result; 42 | } 43 | return a.getYCoord().toBigInteger().compareTo(b.getYCoord().toBigInteger()); 44 | } 45 | 46 | 47 | public static String toString(ECPoint p) { 48 | return Helper.toHexString(p.getEncoded(true)); 49 | } 50 | 51 | 52 | public static byte[] generateKey(int len) { 53 | byte[] key = new byte[len]; 54 | SecureRandom sr = new SecureRandom(); 55 | sr.nextBytes(key); 56 | return key; 57 | } 58 | 59 | public static byte[] generateKey() { 60 | return generateKey(32); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/KeyType.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.crypto; 2 | 3 | import com.github.ontio.common.ErrorCode; 4 | 5 | public enum KeyType { 6 | ECDSA(0x12), 7 | SM2(0x13), 8 | EDDSA(0x14); 9 | 10 | 11 | private int label; 12 | 13 | private KeyType(int b) { 14 | this.label = b; 15 | } 16 | 17 | 18 | // get the crypto.KeyType according to the input label 19 | public static KeyType fromLabel(byte label) throws Exception { 20 | for (KeyType k : KeyType.values()) { 21 | if (k.label == label) { 22 | return k; 23 | } 24 | } 25 | throw new Exception(ErrorCode.UnknownAsymmetricKeyType); 26 | } 27 | public static KeyType fromPubkey(byte[] pubkey) { 28 | try { 29 | if(pubkey.length == 33){ 30 | return KeyType.ECDSA; 31 | }else { 32 | return KeyType.fromLabel(pubkey[0]); 33 | } 34 | } catch (Exception e) { 35 | e.printStackTrace(); 36 | } 37 | return null; 38 | } 39 | 40 | public int getLabel() { 41 | return label; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/SignatureScheme.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.crypto; 2 | 3 | import com.github.ontio.common.ErrorCode; 4 | 5 | public enum SignatureScheme { 6 | SHA224WITHECDSA("SHA224withECDSA"), 7 | SHA256WITHECDSA("SHA256withECDSA"), 8 | SHA384WITHECDSA("SHA384withECDSA"), 9 | SHA512WITHECDSA("SHA512withECDSA"), 10 | SHA3_224WITHECDSA("SHA3-224withECDSA"), 11 | SHA3_256WITHECDSA("SHA3-256withECDSA"), 12 | SHA3_384WITHECDSA("SHA3-384withECDSA"), 13 | SHA3_512WITHECDSA("SHA3-512withECDSA"), 14 | RIPEMD160WITHECDSA("RIPEMD160withECDSA"), 15 | 16 | SM3WITHSM2("SM3withSM2"); 17 | 18 | private String name; 19 | 20 | private SignatureScheme(String v) { 21 | name = v; 22 | } 23 | @Override 24 | public String toString() { 25 | return name; 26 | } 27 | 28 | public static SignatureScheme fromScheme(String name) throws Exception { 29 | for (SignatureScheme k : SignatureScheme.values()) { 30 | if (k.name().equals(name.toUpperCase())) { 31 | return k; 32 | } 33 | } 34 | throw new Exception(ErrorCode.UnknownAsymmetricKeyType); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/Bitcoin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018-2019 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.crypto.bip32; 21 | 22 | public enum Bitcoin implements Network { 23 | MAIN_NET { 24 | @Override 25 | public int getPrivateVersion() { 26 | return 0x488ade4; 27 | } 28 | 29 | @Override 30 | public int getPublicVersion() { 31 | return 0x0488b21e; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/ByteArrayReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32 library, a Java implementation of BIP32 3 | * Copyright (C) 2017 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32; 23 | 24 | import java.util.Arrays; 25 | 26 | final class ByteArrayReader { 27 | 28 | private final byte[] bytes; 29 | private int idx = 0; 30 | 31 | ByteArrayReader(final byte[] source) { 32 | this.bytes = source; 33 | } 34 | 35 | byte[] readRange(final int length) { 36 | final byte[] range = Arrays.copyOfRange(this.bytes, idx, idx + length); 37 | idx += length; 38 | return range; 39 | } 40 | 41 | /** 42 | * deserialize a 32-bit unsigned integer i as a 4-byte sequence, most significant byte first. 43 | */ 44 | int readSer32() { 45 | int result = read(); 46 | result <<= 8; 47 | result |= read(); 48 | result <<= 8; 49 | result |= read(); 50 | result <<= 8; 51 | result |= read(); 52 | return result; 53 | } 54 | 55 | int read() { 56 | return 0xff & bytes[idx++]; 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/ByteArrayWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32 library, a Java implementation of BIP32 3 | * Copyright (C) 2017 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32; 23 | 24 | import java.util.Arrays; 25 | 26 | final class ByteArrayWriter { 27 | 28 | private final byte[] bytes; 29 | private int idx = 0; 30 | 31 | ByteArrayWriter(final byte[] target) { 32 | this.bytes = target; 33 | } 34 | 35 | void concat(final byte[] bytesSource, final int length) { 36 | System.arraycopy(bytesSource, 0, bytes, idx, length); 37 | idx += length; 38 | } 39 | 40 | void concat(final byte[] bytesSource) { 41 | concat(bytesSource, bytesSource.length); 42 | } 43 | 44 | /** 45 | * ser32(i): serialize a 32-bit unsigned integer i as a 4-byte sequence, most significant byte first. 46 | * 47 | * @param i a 32-bit unsigned integer 48 | */ 49 | void concatSer32(final int i) { 50 | concat((byte) (i >> 24)); 51 | concat((byte) (i >> 16)); 52 | concat((byte) (i >> 8)); 53 | concat((byte) (i)); 54 | } 55 | 56 | void concat(final byte b) { 57 | bytes[idx++] = b; 58 | } 59 | 60 | static byte[] tail32(final byte[] bytes64) { 61 | final byte[] ir = new byte[bytes64.length - 32]; 62 | System.arraycopy(bytes64, 32, ir, 0, ir.length); 63 | return ir; 64 | } 65 | 66 | static byte[] head32(final byte[] bytes64) { 67 | return Arrays.copyOf(bytes64, 32); 68 | } 69 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/CKDpriv.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018-2019 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.crypto.bip32; 21 | 22 | public interface CKDpriv { 23 | 24 | /** 25 | * Calculates the private key of the child at index. 26 | * 27 | * @param index The child index to calculate. 28 | * @return The private key of the child. 29 | */ 30 | HdPrivateKey cKDpriv(final int index); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/CKDpub.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018-2019 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.crypto.bip32; 21 | 22 | public interface CKDpub { 23 | HdPublicKey cKDpub(int var1); 24 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/Derivation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018-2019 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.crypto.bip32; 21 | 22 | import com.github.ontio.crypto.bip32.derivation.CkdFunction; 23 | 24 | public interface Derivation { 25 | 26 | /** 27 | * Traverse the nodes from the root key node to find the node referenced by the path. 28 | * 29 | * @param rootKey The root of the path 30 | * @param path The path to follow 31 | * @param ckdFunction Allows you to follow one link 32 | * @param The type of node we are visiting 33 | * @return The final node found at the end of the path 34 | */ 35 | Key derive(final Key rootKey, final Path path, final CkdFunction ckdFunction); 36 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/Deserializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018-2019 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.crypto.bip32; 21 | 22 | import com.github.ontio.sdk.exception.SDKException; 23 | 24 | public interface Deserializer { 25 | 26 | /** 27 | * Deserializes the data into a {@link T}. 28 | * 29 | * @param extendedBase58Key Base58 CharSequence containing the serialized extended key. 30 | * @return The {@link T} 31 | */ 32 | T deserialize(CharSequence extendedBase58Key) throws SDKException; 33 | 34 | /** 35 | * Deserializes the data into a {@link T}. 36 | * 37 | * @param extendedKeyData Byte array containing the serialized extended key. 38 | * @return The {@link T} 39 | */ 40 | T deserialize(byte[] extendedKeyData) throws SDKException; 41 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/Network.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32 library, a Java implementation of BIP32 3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32; 23 | 24 | /** 25 | * Network represents the network to use. 26 | */ 27 | public interface Network { 28 | 29 | int getPrivateVersion(); 30 | 31 | int getPublicVersion(); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/Secp256r1SC.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32 library, a Java implementation of BIP32 3 | * Copyright (C) 2017 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32; 23 | 24 | import org.spongycastle.asn1.x9.X9ECParameters; 25 | import org.spongycastle.crypto.ec.CustomNamedCurves; 26 | import org.spongycastle.math.ec.ECPoint; 27 | 28 | import java.math.BigInteger; 29 | 30 | final class Secp256r1SC { 31 | 32 | static final X9ECParameters CURVE = CustomNamedCurves.getByName("secp256r1"); 33 | 34 | static BigInteger n() { 35 | return CURVE.getN(); 36 | } 37 | 38 | static byte[] pointSerP(final ECPoint point) { 39 | return point.getEncoded(true); 40 | } 41 | 42 | static byte[] pointSerP_gMultiply(final BigInteger p) { 43 | return pointSerP(gMultiply(p)); 44 | } 45 | 46 | static ECPoint gMultiplyAndAddPoint(final BigInteger p, final byte[] toAdd) { 47 | return gMultiply(p).add(decode(toAdd)); 48 | } 49 | 50 | private static ECPoint decode(final byte[] toAdd) { 51 | return CURVE.getCurve().decodePoint(toAdd); 52 | } 53 | 54 | private static ECPoint gMultiply(BigInteger p) { 55 | return CURVE.getG() 56 | .multiply(p); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/derivation/CkdFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32derivation 3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32derivation 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32.derivation; 23 | 24 | public interface CkdFunction { 25 | /** 26 | * Derives the child at the given index on the parent. 27 | * 28 | * @param parent The parent to find the child of 29 | * @param childIndex The index of the child 30 | * @return the {@link KeyNode} for the child 31 | */ 32 | KeyNode deriveChildKey(final KeyNode parent, final int childIndex); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/derivation/CkdFunctionDerive.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32derivation 3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32derivation 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32.derivation; 23 | 24 | public final class CkdFunctionDerive implements Derive { 25 | 26 | private final CkdFunction standardCkdFunction; 27 | private final Node rootNode; 28 | 29 | public CkdFunctionDerive(final CkdFunction standardCkdFunction, final Node rootNode) { 30 | this.standardCkdFunction = standardCkdFunction; 31 | this.rootNode = rootNode; 32 | } 33 | 34 | @Override 35 | public Node fromPath(final CharSequence derivationPath) { 36 | return fromPath(derivationPath, CharSequenceDerivation.INSTANCE); 37 | } 38 | 39 | @Override 40 | public Node fromPath(final Path derivationPath, final Derivation derivation) { 41 | return derivation.derive(rootNode, derivationPath, standardCkdFunction); 42 | } 43 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/derivation/CkdFunctionResultCacheDecorator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32derivation 3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32derivation 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32.derivation; 23 | 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | /** 28 | * Non-thread safe result cache for ckd functions. 29 | *

30 | * If the same child of the same parent is requested a second time, the original result will be returned. 31 | * 32 | * @param Key Node type. 33 | */ 34 | public final class CkdFunctionResultCacheDecorator implements CkdFunction { 35 | 36 | private final CkdFunction decoratedCkdFunction; 37 | 38 | private final Map> cache = new HashMap<>(); 39 | 40 | private CkdFunctionResultCacheDecorator(final CkdFunction decoratedCkdFunction) { 41 | this.decoratedCkdFunction = decoratedCkdFunction; 42 | } 43 | 44 | @Override 45 | public Key deriveChildKey(final Key parent, final int childIndex) { 46 | final Map mapForParent = getMapOf(parent); 47 | Key child = mapForParent.get(childIndex); 48 | if (child == null) { 49 | child = decoratedCkdFunction.deriveChildKey(parent, childIndex); 50 | mapForParent.put(childIndex, child); 51 | } 52 | return child; 53 | } 54 | 55 | private Map getMapOf(final Key parentKey) { 56 | HashMap mapForParent = cache.get(parentKey); 57 | if (mapForParent == null) { 58 | mapForParent = new HashMap<>(); 59 | cache.put(parentKey, mapForParent); 60 | } 61 | return mapForParent; 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/derivation/Derivation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32 library, a Java implementation of BIP32 3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32.derivation; 23 | 24 | import com.github.ontio.crypto.bip32.HdKey; 25 | 26 | public interface Derivation { 27 | 28 | /** 29 | * Traverse the nodes from the root key node to find the node referenced by the path. 30 | * 31 | * @param rootKey The root of the path 32 | * @param path The path to follow 33 | * @param ckdFunction Allows you to follow one link 34 | * @param The type of node we are visiting 35 | * @return The final node found at the end of the path 36 | */ 37 | Key derive(final Key rootKey,final Path path, final CkdFunction ckdFunction); 38 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/crypto/bip32/derivation/Derive.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BIP32 library, a Java implementation of BIP32 3 | * Copyright (C) 2017-2019 Alan Evans, NovaCrypto 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Original source: https://github.com/NovaCrypto/BIP32 19 | * You can contact the authors via github issues. 20 | */ 21 | 22 | package com.github.ontio.crypto.bip32.derivation; 23 | 24 | public interface Derive { 25 | 26 | /** 27 | * Derive from a string path such as m/44'/0'/0'/0/1 28 | * 29 | * @param derivationPath Path 30 | * @return Key at the path 31 | */ 32 | Key fromPath(final CharSequence derivationPath); 33 | 34 | /** 35 | * Derive from a generic path using the {@link Derivation} supplied to extract the child indexes 36 | * 37 | * @param derivationPath Path 38 | * @param derivation The class that extracts the path elements 39 | * @param The generic type of the path 40 | * @return Key at the path 41 | */ 42 | Key fromPath(final Path derivationPath, final Derivation derivation); 43 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/io/Serializable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.io; 21 | 22 | import com.github.ontio.common.Helper; 23 | 24 | import java.io.*; 25 | 26 | /** 27 | * Serialize interface 28 | */ 29 | public interface Serializable { 30 | static T from(byte[] value, Class t) throws InstantiationException, IllegalAccessException { 31 | try (ByteArrayInputStream ms = new ByteArrayInputStream(value)) { 32 | try (BinaryReader reader = new BinaryReader(ms)) { 33 | return reader.readSerializable(t); 34 | } 35 | } catch (IOException ex) { 36 | throw new IllegalArgumentException(ex); 37 | } 38 | } 39 | 40 | /** 41 | * @param reader 42 | * @throws IOException 43 | */ 44 | void deserialize(BinaryReader reader) throws IOException; 45 | 46 | /** 47 | * @param writer 48 | * @throws IOException 49 | */ 50 | void serialize(BinaryWriter writer) throws IOException; 51 | 52 | default byte[] toArray() { 53 | try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) { 54 | try (BinaryWriter writer = new BinaryWriter(ms)) { 55 | serialize(writer); 56 | writer.flush(); 57 | return ms.toByteArray(); 58 | } 59 | } catch (IOException ex) { 60 | throw new UnsupportedOperationException(ex); 61 | } 62 | } 63 | 64 | default String toHexString() { 65 | return Helper.toHexString(toArray()); 66 | } 67 | 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/io/utils.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.io; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.common.Helper; 5 | 6 | import java.io.IOException; 7 | import java.math.BigInteger; 8 | 9 | public class utils { 10 | 11 | public static long readVarInt(BinaryReader reader) throws IOException { 12 | byte[] r = reader.readVarBytes(); 13 | BigInteger b = Helper.BigIntFromNeoBytes(r); 14 | return b.longValue(); 15 | } 16 | public static Address readAddress(BinaryReader reader) throws IOException { 17 | byte[] r = reader.readVarBytes(); 18 | return new Address(r); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/network/connect/AbstractConnector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.network.connect; 21 | 22 | 23 | public abstract class AbstractConnector implements IConnector { 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/network/exception/ConnectorException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.network.exception; 21 | 22 | 23 | public class ConnectorException extends Exception { 24 | private static final long serialVersionUID = 1110342144692879043L; 25 | 26 | public ConnectorException(String message) { 27 | super(message); 28 | } 29 | 30 | public ConnectorException(String message, Throwable ex) { 31 | super(message, ex); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/network/exception/RestfulException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.network.exception; 21 | 22 | public class RestfulException extends ConnectorException { 23 | private static final long serialVersionUID = -8558006777817318117L; 24 | 25 | public RestfulException(String message) { 26 | super(message); 27 | } 28 | 29 | public RestfulException(String message, Throwable ex) { 30 | super(message, ex); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/network/exception/RpcException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.network.exception; 21 | 22 | /** 23 | * 24 | */ 25 | public class RpcException extends ConnectorException { 26 | private static final long serialVersionUID = -8558006777817318117L; 27 | 28 | public final int code; 29 | 30 | public RpcException(int code,String message) { 31 | super(message); 32 | this.code = code; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/network/rest/Result.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.network.rest; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | 24 | public class Result { 25 | public String Action; 26 | public long Error; 27 | public String Desc; 28 | public Object Result; 29 | public String Version; 30 | @Override 31 | public String toString() { 32 | return JSON.toJSONString(this); 33 | } 34 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/network/rest/X509.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.network.rest; 21 | 22 | import javax.net.ssl.X509TrustManager; 23 | import java.security.cert.CertificateException; 24 | import java.security.cert.X509Certificate; 25 | 26 | /** 27 | * 28 | */ 29 | public class X509 implements X509TrustManager { 30 | @Override 31 | public X509Certificate[] getAcceptedIssuers() { 32 | return null; 33 | } 34 | @Override 35 | public void checkClientTrusted(X509Certificate[] chain, String authType) 36 | throws CertificateException { 37 | } 38 | @Override 39 | public void checkServerTrusted(X509Certificate[] chain, String authType) 40 | throws CertificateException { 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/network/websocket/MsgQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.network.websocket; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | 24 | import java.util.*; 25 | 26 | /** 27 | * 28 | */ 29 | public class MsgQueue { 30 | 31 | private static Set resultSet = new HashSet(); 32 | public static void addResult(Result obj) { 33 | resultSet.add(JSON.toJSONString(obj)); 34 | } 35 | public static Set getResultSet(){ 36 | Set rt = new HashSet(); 37 | rt.addAll(resultSet); 38 | return rt; 39 | } 40 | 41 | public static void removeResult(String ele){ 42 | resultSet.remove(ele); 43 | } 44 | public static void clear(){ 45 | resultSet.clear(); 46 | } 47 | public static int size() { 48 | return resultSet.size(); 49 | } 50 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/network/websocket/Result.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.network.websocket; 21 | 22 | /** 23 | * 24 | */ 25 | public class Result { 26 | public String Action; 27 | public long Error; 28 | public String Desc; 29 | public Object Result; 30 | public String Version; 31 | public Object Id; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/CredentialStatus.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | 4 | import com.alibaba.fastjson.annotation.JSONType; 5 | 6 | @JSONType(orders = {"id", "type"}) 7 | public class CredentialStatus { 8 | 9 | public String id; // should be CredentialRecord contract address 10 | public CredentialStatusType type; 11 | 12 | public CredentialStatus() { 13 | } 14 | 15 | public CredentialStatus(String scriptHash, CredentialStatusType type) { 16 | this.id = scriptHash; 17 | this.type = type; 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/CredentialStatusType.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | public enum CredentialStatusType { 4 | AttestContract, 5 | RevocationList 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/OntIdPubKey.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | public class OntIdPubKey { 4 | public String id; // pubkey URI 5 | public PubKeyType type; // pubkey type, for example: EcdsaSecp256r1VerificationKey2019 6 | public String controller; 7 | public String publicKeyHex; 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/OntIdSigner.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | import com.github.ontio.account.Account; 4 | 5 | public class OntIdSigner { 6 | String ontId; 7 | OntIdPubKey pubKey; 8 | Account signer; 9 | 10 | public OntIdSigner(String ontId, OntIdPubKey pubKey, Account signer) { 11 | this.ontId = ontId; 12 | this.pubKey = pubKey; 13 | this.signer = signer; 14 | } 15 | 16 | // public byte[] hash(byte[] msg) throws Exception { 17 | // return pubKey.type.getAlg().hash(msg); 18 | // } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/Proof.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | import com.alibaba.fastjson.annotation.JSONType; 4 | import com.github.ontio.account.Account; 5 | import com.github.ontio.common.Helper; 6 | 7 | @JSONType(orders = {"type", "created", "challenge", "domain", "proofPurpose", "verificationMethod", "hex", "jws"}) 8 | public class Proof { 9 | public PubKeyType type; 10 | public String created; // time stamp 11 | public String challenge; 12 | public Object domain; 13 | public ProofPurpose proofPurpose; 14 | public String verificationMethod; // pubkey uri 15 | public String hex; 16 | public String jws; 17 | 18 | public Proof() { 19 | } 20 | 21 | public Proof(String publicKeyURI, String created, PubKeyType type, ProofPurpose proofPurpose) { 22 | this.type = type; 23 | this.created = created; 24 | if (proofPurpose == null) { 25 | proofPurpose = ProofPurpose.assertionMethod; 26 | } 27 | this.proofPurpose = proofPurpose; 28 | this.verificationMethod = publicKeyURI; 29 | } 30 | 31 | public Proof(String publicKeyURI, String created, PubKeyType type, ProofPurpose proofPurpose, 32 | String challenge, Object domain) { 33 | this(publicKeyURI, created, type, proofPurpose); 34 | this.challenge = challenge; 35 | this.domain = domain; 36 | } 37 | 38 | public Proof genNeedSignProof() { 39 | return new Proof(verificationMethod, created, type, proofPurpose, challenge, domain); 40 | } 41 | 42 | public Proof genJWTProof() { 43 | Proof proof = new Proof(); 44 | proof.created = created; 45 | proof.proofPurpose = proofPurpose; 46 | proof.hex = hex; 47 | return proof; 48 | } 49 | 50 | public void fillHexSignature(Account account, byte[] needSignData) throws Exception { 51 | byte[] sig = account.generateSignature(needSignData, account.getSignatureScheme(), null); 52 | hex = Helper.toHexString(sig); 53 | } 54 | 55 | public byte[] parseHexSignature() { 56 | return Helper.hexToBytes(hex); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/ProofPurpose.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | public enum ProofPurpose { 4 | assertionMethod 5 | } 6 | // TODO: authentication 7 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/PubKeyType.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | import com.github.ontio.crypto.SignatureScheme; 4 | 5 | public enum PubKeyType { 6 | EcdsaSecp224r1VerificationKey2019(ALG.ES224, ALG.TYPE_ECDSA, ALG.CURVE_P224, ALG.HASH_224, SignatureScheme.SHA224WITHECDSA), 7 | EcdsaSecp256r1VerificationKey2019(ALG.ES256, ALG.TYPE_ECDSA, ALG.CURVE_P256, ALG.HASH_256, SignatureScheme.SHA256WITHECDSA), 8 | EcdsaSecp384r1VerificationKey2019(ALG.ES384, ALG.TYPE_ECDSA, ALG.CURVE_P384, ALG.HASH_384, SignatureScheme.SHA384WITHECDSA), 9 | EcdsaSecp521r1VerificationKey2019(ALG.ES512, ALG.TYPE_ECDSA, ALG.CURVE_P521, ALG.HASH_512, SignatureScheme.SHA512WITHECDSA), 10 | EcdsaSecp256k1VerificationKey2019(ALG.ES256K, ALG.TYPE_ECDSA, ALG.CURVE_secp256k1, ALG.HASH_256, SignatureScheme.SHA256WITHECDSA), 11 | Ed25519VerificationKey2018(ALG.EdDSA, ALG.TYPE_EDDSA, ALG.CURVE_Curve25519, ALG.HASH_256, null), 12 | SM2VerificationKey2019(ALG.SM, ALG.TYPE_SM2, ALG.CURVE_SM2P256V1, ALG.HASH_SM3, SignatureScheme.SM3WITHSM2); 13 | 14 | private ALG alg; 15 | private String algType; 16 | private String curve; 17 | private String hashMethod; 18 | private SignatureScheme signatureScheme; 19 | 20 | PubKeyType(ALG alg, String algType, String curve, String hashMethod, SignatureScheme scheme) { 21 | this.alg = alg; 22 | this.algType = algType; 23 | this.curve = curve; 24 | this.hashMethod = hashMethod; 25 | this.signatureScheme = scheme; 26 | 27 | // inject self to alg pub key type 28 | alg.setProofPubKeyType(this); 29 | } 30 | 31 | public ALG getAlg() { 32 | return alg; 33 | } 34 | 35 | public String getAlgType() { 36 | return algType; 37 | } 38 | 39 | public String getCurve() { 40 | return curve; 41 | } 42 | 43 | public String getHashMethod() { 44 | return hashMethod; 45 | } 46 | 47 | public SignatureScheme getSignatureScheme() { 48 | return signatureScheme; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/PubKeyTypeFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | import com.github.ontio.sdk.exception.SDKException; 4 | 5 | public class PubKeyTypeFactory { 6 | public static PubKeyType genPubKeyType(String pubKeyType) throws Exception { 7 | if ("EcdsaSecp224r1VerificationKey2019".equals(pubKeyType)) { 8 | return PubKeyType.EcdsaSecp224r1VerificationKey2019; 9 | } else if ("EcdsaSecp256r1Signature2019".equals(pubKeyType)) { 10 | return PubKeyType.EcdsaSecp256r1VerificationKey2019; 11 | } else if ("EcdsaSecp384r1VerificationKey2019".equals(pubKeyType)) { 12 | return PubKeyType.EcdsaSecp384r1VerificationKey2019; 13 | } else if ("EcdsaSecp521r1VerificationKey2019".equals(pubKeyType)) { 14 | return PubKeyType.EcdsaSecp521r1VerificationKey2019; 15 | } else if ("EcdsaSecp256k1VerificationKey2019".equals(pubKeyType)) { 16 | return PubKeyType.EcdsaSecp256k1VerificationKey2019; 17 | } else if ("Ed25519VerificationKey2018".equals(pubKeyType)) { 18 | return PubKeyType.Ed25519VerificationKey2018; 19 | } else if ("SM2VerificationKey2019".equals(pubKeyType)) { 20 | return PubKeyType.SM2VerificationKey2019; 21 | } else { 22 | throw new SDKException("un support pub key type"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/SignRequest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.annotation.JSONType; 5 | import com.alibaba.fastjson.serializer.SerializerFeature; 6 | 7 | @JSONType(orders = {"credentialSubject", "ontId", "proof"}) 8 | public class SignRequest { 9 | Object credentialSubject; 10 | String ontId; 11 | Proof proof; 12 | 13 | public SignRequest(Object credentialSubject, String ontId, Proof proof) { 14 | this.credentialSubject = credentialSubject; 15 | this.ontId = ontId; 16 | this.proof = proof; 17 | } 18 | 19 | public byte[] genNeedSignData() { 20 | Proof proof = this.proof; 21 | this.proof = this.proof.genNeedSignProof(); 22 | String jsonStr = JSON.toJSONString(this, SerializerFeature.MapSortField); 23 | this.proof = proof; 24 | return jsonStr.getBytes(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/Util.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONArray; 5 | import com.alibaba.fastjson.JSONObject; 6 | import com.github.ontio.sdk.exception.SDKException; 7 | 8 | import java.util.Set; 9 | import java.util.TreeSet; 10 | 11 | public class Util { 12 | 13 | public static int getIndexFromPubKeyURI(String pubKeyURI) throws Exception { 14 | String[] keyInfo = pubKeyURI.split("#keys-"); 15 | if (keyInfo.length != 2) { 16 | throw new SDKException(String.format("invalid pubKeyURI %s", pubKeyURI)); 17 | } 18 | return Integer.parseInt(keyInfo[1]); 19 | } 20 | 21 | public static String getOntIdFromPubKeyURI(String pubKeyURI) throws Exception { 22 | String[] keyInfo = pubKeyURI.split("#keys-"); 23 | if (keyInfo.length != 2) { 24 | throw new SDKException(String.format("invalid pubKeyURI %s", pubKeyURI)); 25 | } 26 | return keyInfo[0]; 27 | } 28 | 29 | // fetch "id" field of object 30 | // if object doesn't contain "id" field, return "" 31 | // if object is array, return "" 32 | public static String fetchId(Object object) { 33 | if (object == null) { 34 | return ""; 35 | } 36 | if (object instanceof String) { 37 | return (String) object; 38 | } 39 | if (object.getClass().isPrimitive()) { 40 | return ""; 41 | } 42 | if (object instanceof JSONArray) { 43 | return ""; 44 | } 45 | if (object.getClass().isArray()) { 46 | return ""; 47 | } 48 | JSONObject jsonObject = (JSONObject) JSONObject.toJSON(object); 49 | String id = jsonObject.getString("id"); 50 | if (id == null) { 51 | return ""; 52 | } 53 | return id; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/jwt/JWTHeader.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid.jwt; 2 | 3 | import com.alibaba.fastjson.annotation.JSONType; 4 | import com.github.ontio.ontid.*; 5 | 6 | @JSONType(orders = {"alg", "kid", "typ"}) 7 | public class JWTHeader { 8 | public ALG alg; 9 | public String kid; // VerifiableCredential issuer 10 | public String typ = "JWT"; 11 | 12 | public JWTHeader() { 13 | } 14 | 15 | public JWTHeader(ALG alg, String kid) { 16 | this.alg = alg; 17 | this.kid = kid; 18 | } 19 | 20 | public JWTHeader(PubKeyType pubKeyType, String kid) { 21 | this.alg = pubKeyType.getAlg(); 22 | this.kid = kid; 23 | } 24 | 25 | public JWTHeader(VerifiableCredential credential) { 26 | this.alg = credential.proof.type.getAlg(); 27 | this.kid = credential.proof.verificationMethod; 28 | } 29 | 30 | public JWTHeader(Proof proof) throws Exception { 31 | this.alg = proof.type.getAlg(); 32 | this.kid = proof.verificationMethod; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/jwt/JWTVC.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid.jwt; 2 | 3 | import com.alibaba.fastjson.JSONArray; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.alibaba.fastjson.annotation.JSONField; 6 | import com.alibaba.fastjson.annotation.JSONType; 7 | import com.github.ontio.ontid.CredentialStatus; 8 | import com.github.ontio.ontid.Proof; 9 | import com.github.ontio.ontid.VerifiableCredential; 10 | import com.github.ontio.sdk.exception.SDKException; 11 | 12 | @JSONType(orders = {"@context", "type", "issuer", "credentialSubject", "credentialStatus", "proof"}) 13 | public class JWTVC { 14 | @JSONField(name = "@context") 15 | public String[] context; 16 | public String[] type; 17 | public Object issuer; 18 | public Object credentialSubject; 19 | public CredentialStatus credentialStatus; 20 | public Proof proof; 21 | 22 | public JWTVC() { 23 | } 24 | 25 | public JWTVC(VerifiableCredential credential) throws Exception { 26 | this.context = credential.context; 27 | this.type = credential.type; 28 | this.credentialStatus = credential.credentialStatus; 29 | if (credential.proof != null) { 30 | this.proof = credential.proof.genJWTProof(); 31 | } 32 | if (credential.issuer.getClass().isPrimitive() || credential.issuer.getClass().isArray() || 33 | credential.issuer instanceof JSONArray) { 34 | throw new SDKException("illegal credential issuer"); 35 | } 36 | if (!(credential.issuer instanceof String)) { 37 | JSONObject jsonObject = (JSONObject) JSONObject.toJSON(credential.issuer); 38 | jsonObject.remove("id"); 39 | if (jsonObject.size() > 0) { 40 | this.issuer = jsonObject; 41 | } 42 | } 43 | // remove id attribute 44 | if (credential.credentialSubject != null && !credential.credentialSubject.getClass().isArray() 45 | && !(credential.credentialSubject instanceof JSONArray)) { 46 | JSONObject credentialSubject = (JSONObject) JSONObject.toJSON(credential.credentialSubject); 47 | credentialSubject.remove("id"); 48 | if (credentialSubject.size() > 0) { 49 | this.credentialSubject = credentialSubject; 50 | } 51 | } else { 52 | this.credentialSubject = credential.credentialSubject; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/jwt/JWTVP.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.ontid.jwt; 2 | 3 | import com.alibaba.fastjson.JSONArray; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.alibaba.fastjson.annotation.JSONField; 6 | import com.alibaba.fastjson.annotation.JSONType; 7 | import com.github.ontio.ontid.Proof; 8 | import com.github.ontio.ontid.VerifiablePresentation; 9 | import com.github.ontio.sdk.exception.SDKException; 10 | 11 | @JSONType(orders = {"@context", "type", "challenge", "verifiableCredential", "holder", "proof"}) 12 | public class JWTVP { 13 | @JSONField(name = "@context") 14 | public String[] context; 15 | public String[] type; 16 | public String[] verifiableCredential; // base64url encoded JWTVC as string 17 | public Object holder; 18 | public Proof proof; 19 | 20 | public JWTVP() { 21 | } 22 | 23 | public JWTVP(VerifiablePresentation presentation, Proof proof) throws Exception { 24 | if (presentation.holder.getClass().isPrimitive() || presentation.holder.getClass().isArray() || 25 | presentation.holder instanceof JSONArray) { 26 | throw new SDKException("illegal presentation holder"); 27 | } 28 | if (!(presentation.holder instanceof String)) { 29 | JSONObject jsonObject = (JSONObject) JSONObject.toJSON(presentation.holder); 30 | jsonObject.remove("id"); 31 | if (jsonObject.size() > 0) { 32 | this.holder = jsonObject; 33 | } 34 | } 35 | this.context = presentation.context; 36 | this.type = presentation.type; 37 | if (presentation.verifiableCredential != null) { 38 | String[] verifiableCredential = new String[presentation.verifiableCredential.length]; 39 | for (int i = 0; i < presentation.verifiableCredential.length; i++) { 40 | JWTCredential jwtCred = new JWTCredential(presentation.verifiableCredential[i]); 41 | verifiableCredential[i] = jwtCred.toString(); 42 | } 43 | this.verifiableCredential = verifiableCredential; 44 | } 45 | this.proof = proof.genJWTProof(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/ontid/roles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ontio/ontology-java-sdk/8a35a0823068bcb035f1229a290cc65acdd49263/src/main/java/com/github/ontio/ontid/roles.png -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/sdk/exception/SDKException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.sdk.exception; 21 | 22 | 23 | import com.alibaba.fastjson.JSON; 24 | 25 | public class SDKException extends Exception { 26 | 27 | private static final long serialVersionUID = -3056715808373341597L; 28 | 29 | public SDKException(String message) { 30 | super(message); 31 | initExMsg(message); 32 | } 33 | public SDKException(String message, Throwable ex) { 34 | super(message, ex); 35 | initExMsg(message); 36 | } 37 | public SDKException(Throwable ex) { 38 | super(ex); 39 | } 40 | 41 | private void initExMsg(String message) { 42 | } 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/sdk/exception/SDKRuntimeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.sdk.exception; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | 24 | public class SDKRuntimeException extends RuntimeException { 25 | 26 | private static final long serialVersionUID = 2005335065357755315L; 27 | public SDKRuntimeException(String message) { 28 | super(message); 29 | initExMsg(message); 30 | } 31 | 32 | public SDKRuntimeException(String message, Throwable ex) { 33 | super(message, ex); 34 | initExMsg(message); 35 | } 36 | 37 | private void initExMsg(String message) { 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/sdk/info/AccountInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.sdk.info; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | 24 | /** 25 | * 26 | */ 27 | public class AccountInfo { 28 | public String addressBase58; 29 | public String pubkey; 30 | 31 | public String encryptedPrikey; 32 | public String addressU160; 33 | private String prikey; 34 | private String prikeyWif; 35 | 36 | public void setPrikey(String prikey) { 37 | //this.prikey = prikey; 38 | } 39 | 40 | public void setPriwif(String priwif) { 41 | //this.prikeyWif = priwif; 42 | } 43 | 44 | public String getPrikeyWif() { 45 | return prikeyWif; 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return JSON.toJSONString(this); 51 | } 52 | 53 | 54 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/sdk/info/IdentityInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.sdk.info; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | 24 | /** 25 | * 26 | */ 27 | public class IdentityInfo { 28 | public String ontid; 29 | public String pubkey; 30 | 31 | public String encryptedPrikey; 32 | public String addressU160; 33 | private String prikey; 34 | private String prikeyWif; 35 | 36 | public void setPrikey(String prikey) { 37 | //this.prikey = prikey; 38 | } 39 | 40 | public void setPriwif(String priwif) { 41 | //this.prikeyWif = priwif; 42 | } 43 | 44 | public String getPrikeyWif() { 45 | return prikeyWif; 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return JSON.toJSONString(this); 51 | } 52 | 53 | 54 | } -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/sdk/wallet/Identity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.sdk.wallet; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | /** 28 | * 29 | */ 30 | public class Identity { 31 | public String label = ""; 32 | public String ontid = ""; 33 | public boolean isDefault = false; 34 | public boolean lock = false; 35 | public List controls = new ArrayList(); 36 | public Object extra = null; 37 | 38 | public Object getExtra(){ 39 | return extra; 40 | } 41 | public void setExtra(Object extra){ 42 | this.extra = extra; 43 | } 44 | @Override 45 | public String toString() { 46 | return JSON.toJSONString(this); 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/sdk/wallet/Scrypt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.sdk.wallet; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | 24 | /** 25 | */ 26 | public class Scrypt implements Cloneable{ 27 | private int n = 16384; 28 | private int r = 8; 29 | private int p = 8; 30 | private int DkLen = 64; 31 | private String Salt; 32 | 33 | public int getDkLen() { 34 | return DkLen; 35 | } 36 | 37 | public void setDkLen(int dkLen) { 38 | DkLen = dkLen; 39 | } 40 | 41 | 42 | public Scrypt() { 43 | } 44 | 45 | public Scrypt(int n, int r, int p) { 46 | this.n = n; 47 | this.r = r; 48 | this.p = p; 49 | } 50 | 51 | public int getN() { 52 | return n; 53 | } 54 | 55 | public void setN(int n) { 56 | this.n = n; 57 | } 58 | 59 | public int getR() { 60 | return r; 61 | } 62 | 63 | public void setR(int r) { 64 | this.r = r; 65 | } 66 | 67 | public int getP() { 68 | return p; 69 | } 70 | 71 | public void setP(int p) { 72 | this.p = p; 73 | } 74 | 75 | @Override 76 | public Scrypt clone() { 77 | Scrypt o = null; 78 | try { 79 | o = (Scrypt) super.clone(); 80 | } catch (CloneNotSupportedException e) { 81 | e.printStackTrace(); 82 | } 83 | return o; 84 | } 85 | @Override 86 | public String toString() { 87 | return JSON.toJSONString(this); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/sidechain/SidechainVm.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.sidechain; 2 | 3 | import com.github.ontio.OntSdk; 4 | import com.github.ontio.sidechain.smartcontract.governance.Governance; 5 | import com.github.ontio.sidechain.smartcontract.ongx.OngX; 6 | 7 | public class SidechainVm { 8 | private Governance governance; 9 | private OngX ongX; 10 | private OntSdk sdk; 11 | public SidechainVm(OntSdk sdk){ 12 | this.sdk = sdk; 13 | } 14 | 15 | public Governance governance() { 16 | if (governance == null){ 17 | governance = new Governance(sdk); 18 | } 19 | return governance; 20 | } 21 | 22 | public OngX ongX() { 23 | if (ongX == null){ 24 | ongX = new OngX(sdk); 25 | } 26 | return ongX; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/sidechain/smartcontract/ongx/Swap.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.sidechain.smartcontract.ongx; 2 | 3 | import com.github.ontio.common.Address; 4 | 5 | public class Swap { 6 | public Address address; 7 | public long value; 8 | public Swap(Address address, long value){ 9 | this.address = address; 10 | this.value = value; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/smartcontract/nativevm/abi/AbiEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.smartcontract.nativevm.abi; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | import com.github.ontio.common.ErrorCode; 24 | import com.github.ontio.sdk.exception.SDKException; 25 | 26 | import java.util.List; 27 | 28 | /** 29 | * smartcode abi event 30 | */ 31 | public class AbiEvent { 32 | public String name; 33 | public String returntype; 34 | public List parameters; 35 | 36 | public String getName() { 37 | return name; 38 | } 39 | 40 | public List getParameters() { 41 | return parameters; 42 | } 43 | public void setParamsValue(Object... objs) throws Exception{ 44 | if(objs.length != parameters.size()){ 45 | throw new SDKException(ErrorCode.ParamError); 46 | } 47 | for (int i = 0; i < objs.length; i++) { 48 | parameters.get(i).setValue(objs[i]); 49 | } 50 | } 51 | public Parameter getParameter(String name) { 52 | for (Parameter e : parameters) { 53 | if (e.getName().equals(name)) { 54 | return e; 55 | } 56 | } 57 | return null; 58 | } 59 | public void clearParamsValue() { 60 | for (Parameter e : parameters) { 61 | e.setValue(null); 62 | } 63 | } 64 | @Override 65 | public String toString() { 66 | return JSON.toJSONString(this); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/smartcontract/nativevm/abi/AbiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.smartcontract.nativevm.abi; 21 | 22 | 23 | import com.alibaba.fastjson.JSON; 24 | import com.github.ontio.common.ErrorCode; 25 | import com.github.ontio.sdk.exception.SDKException; 26 | 27 | import java.util.List; 28 | 29 | /** 30 | * smartcode abi function 31 | */ 32 | public class AbiFunction { 33 | public String name; 34 | public String returntype; 35 | public List parameters; 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public List getParameters() { 42 | return parameters; 43 | } 44 | public void setParamsValue(Object... objs) throws Exception{ 45 | if(objs.length != parameters.size()){ 46 | throw new SDKException(ErrorCode.ParamError); 47 | } 48 | for (int i = 0; i < objs.length; i++) { 49 | parameters.get(i).setValue(objs[i]); 50 | } 51 | } 52 | public Parameter getParameter(String name) { 53 | for (Parameter e : parameters) { 54 | if (e.getName().equals(name)) { 55 | return e; 56 | } 57 | } 58 | return null; 59 | } 60 | public void clearParamsValue() { 61 | for (Parameter e : parameters) { 62 | e.setValue(null); 63 | } 64 | } 65 | @Override 66 | public String toString() { 67 | return JSON.toJSONString(this); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/smartcontract/nativevm/abi/Struct.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.smartcontract.nativevm.abi; 2 | 3 | import com.github.ontio.common.Address; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | 9 | public class Struct { 10 | public List list = new ArrayList(); 11 | public Struct(){ 12 | 13 | } 14 | public Struct add(Object... objs){ 15 | for(int i=0;i. 17 | * 18 | */ 19 | 20 | package com.github.ontio.smartcontract.nativevm.abi; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | import com.github.ontio.common.ErrorCode; 24 | import com.github.ontio.sdk.exception.SDKException; 25 | 26 | import java.util.List; 27 | 28 | public class SubType { 29 | public List parameters; 30 | 31 | public List getParameters() { 32 | return parameters; 33 | } 34 | public void setParamsValue(Object... objs) throws Exception{ 35 | if(objs.length != parameters.size()){ 36 | throw new SDKException(ErrorCode.ParamError); 37 | } 38 | for (int i = 0; i < objs.length; i++) { 39 | parameters.get(i).setValue(objs[i]); 40 | } 41 | } 42 | public Parameter getParameter(String name) { 43 | for (Parameter e : parameters) { 44 | if (e.getName().equals(name)) { 45 | return e; 46 | } 47 | } 48 | return null; 49 | } 50 | public void clearParamsValue() { 51 | for (Parameter e : parameters) { 52 | e.setValue(null); 53 | } 54 | } 55 | @Override 56 | public String toString() { 57 | return JSON.toJSONString(this); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/smartcontract/neovm/abi/AbiEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.smartcontract.neovm.abi; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | import com.github.ontio.common.ErrorCode; 24 | import com.github.ontio.sdk.exception.SDKException; 25 | import java.util.List; 26 | 27 | /** 28 | * smartcode abi event 29 | */ 30 | public class AbiEvent { 31 | public String name; 32 | public String returntype; 33 | public List parameters; 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public List getParameters() { 40 | return parameters; 41 | } 42 | public void setParamsValue(Object... objs) throws Exception{ 43 | if(objs.length != parameters.size()){ 44 | throw new SDKException(ErrorCode.ParamError); 45 | } 46 | for (int i = 0; i < objs.length; i++) { 47 | parameters.get(i).setValue(objs[i]); 48 | } 49 | } 50 | public Parameter getParameter(String name) { 51 | for (Parameter e : parameters) { 52 | if (e.getName().equals(name)) { 53 | return e; 54 | } 55 | } 56 | return null; 57 | } 58 | public void clearParamsValue() { 59 | for (Parameter e : parameters) { 60 | e.setValue(null); 61 | } 62 | } 63 | @Override 64 | public String toString() { 65 | return JSON.toJSONString(this); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/github/ontio/smartcontract/neovm/abi/Struct.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.smartcontract.neovm.abi; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | 7 | public class Struct { 8 | public List list = new ArrayList(); 9 | public Struct(){ 10 | 11 | } 12 | public Struct add(Object... objs){ 13 | for(int i=0;i States; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/demo/ledger/p2p/BlkHeader.java: -------------------------------------------------------------------------------- 1 | package demo.ledger.p2p; 2 | 3 | import com.github.ontio.common.Helper; 4 | import com.github.ontio.io.BinaryReader; 5 | import demo.ledger.common.BlockHeader; 6 | 7 | import java.io.ByteArrayInputStream; 8 | import java.io.IOException; 9 | 10 | /** 11 | * 12 | * 13 | */ 14 | public class BlkHeader { 15 | public BlockHeader[] headers; 16 | public BlkHeader(){ 17 | 18 | } 19 | public void deserialization(byte[] data){ 20 | ByteArrayInputStream ms = new ByteArrayInputStream(data); 21 | BinaryReader reader = new BinaryReader(ms); 22 | try { 23 | int count = reader.readInt(); 24 | headers = new BlockHeader[count]; 25 | for(int i=0;i map = new HashMap<>(); 12 | 13 | public MapItem() { 14 | } 15 | 16 | public void Add(StackItems key, StackItems value) { 17 | map.put(key, value); 18 | } 19 | 20 | public void Clear() { 21 | map.clear(); 22 | } 23 | 24 | public boolean ContainsKey(StackItems item) { 25 | return map.get(item) != null; 26 | } 27 | 28 | public void Remove(StackItems item) { 29 | map.remove(item); 30 | } 31 | 32 | @Override 33 | public boolean Equals(StackItems item) { 34 | return this.equals(item); 35 | } 36 | 37 | @Override 38 | public BigInteger GetBigInteger() { 39 | return null; 40 | } 41 | 42 | @Override 43 | public boolean GetBoolean() { 44 | return true; 45 | } 46 | 47 | @Override 48 | public byte[] GetByteArray() { 49 | return null; 50 | } 51 | 52 | @Override 53 | public InteropItem GetInterface() { 54 | return null; 55 | } 56 | 57 | @Override 58 | public StackItems[] GetArray() { 59 | return null; 60 | } 61 | 62 | @Override 63 | public StackItems[] GetStruct() { 64 | return null; 65 | } 66 | 67 | @Override 68 | public Map GetMap() { 69 | return map; 70 | } 71 | 72 | public StackItems TryGetValue(StackItems key) { 73 | for (Map.Entry e : map.entrySet()) { 74 | if (e.getKey() instanceof ByteArrayItem) { 75 | if (key instanceof ByteArrayItem) { 76 | if (Helper.toHexString(e.getKey().GetByteArray()).equals(Helper.toHexString(key.GetByteArray()))) { 77 | return e.getValue(); 78 | } 79 | } else if (key instanceof IntegerItem) { 80 | if (e.getKey().GetBigInteger().compareTo(key.GetBigInteger()) > 0) { 81 | return e.getValue(); 82 | } 83 | } 84 | 85 | } 86 | } 87 | return null; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/demo/vmtest/types/StackItems.java: -------------------------------------------------------------------------------- 1 | package demo.vmtest.types; 2 | 3 | import java.math.BigInteger; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public class StackItems { 8 | public boolean Equals(StackItems other) { 9 | return false; 10 | } 11 | 12 | public BigInteger GetBigInteger() { 13 | return new BigInteger(""); 14 | } 15 | 16 | public boolean GetBoolean() { 17 | return false; 18 | } 19 | 20 | public byte[] GetByteArray() { 21 | return new byte[]{}; 22 | } 23 | 24 | public InteropItem GetInterface() { 25 | return null; 26 | } 27 | 28 | public StackItems[] GetArray() { 29 | return new StackItems[0]; 30 | } 31 | 32 | public StackItems[] GetStruct() { 33 | return new StackItems[0]; 34 | } 35 | 36 | public Map GetMap() { 37 | return new HashMap<>(); 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/java/demo/vmtest/types/StructItem.java: -------------------------------------------------------------------------------- 1 | package demo.vmtest.types; 2 | 3 | import java.math.BigInteger; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class StructItem extends StackItems { 9 | public List stackItems = new ArrayList<>(); 10 | 11 | public StructItem(List stackItems) { 12 | this.stackItems = stackItems; 13 | } 14 | 15 | @Override 16 | public boolean Equals(StackItems item) { 17 | return this.equals(item); 18 | } 19 | 20 | @Override 21 | public BigInteger GetBigInteger() { 22 | return null; 23 | } 24 | 25 | @Override 26 | public boolean GetBoolean() { 27 | return true; 28 | } 29 | 30 | @Override 31 | public byte[] GetByteArray() { 32 | return null; 33 | } 34 | 35 | @Override 36 | public InteropItem GetInterface() { 37 | return null; 38 | } 39 | 40 | @Override 41 | public StackItems[] GetArray() { 42 | return null; 43 | } 44 | 45 | @Override 46 | public StackItems[] GetStruct() { 47 | return stackItems.toArray(new StackItems[stackItems.size()]); 48 | } 49 | 50 | @Override 51 | public Map GetMap() { 52 | return null; 53 | } 54 | 55 | public void Add(StackItems items) { 56 | stackItems.add(items); 57 | } 58 | 59 | public int Count() { 60 | return stackItems.size(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/demo/vmtest/utils/Config.java: -------------------------------------------------------------------------------- 1 | package demo.vmtest.utils; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.core.payload.InvokeCode; 5 | import com.github.ontio.core.transaction.Transaction; 6 | 7 | import java.util.ArrayList; 8 | import java.util.HashMap; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | public class Config { 13 | public String ContractAddress = "ContractAddress"; 14 | public Map storageMap = new HashMap<>(); 15 | public Transaction tx = new InvokeCode(); 16 | 17 | public Map getStorageMap() { 18 | return storageMap; 19 | } 20 | 21 | public List

GetSignatureAddresses() { 22 | if (tx.sigs == null) { 23 | return null; 24 | } 25 | List
list = new ArrayList(); 26 | for (int i = 0; i < tx.sigs.length; i++) { 27 | for (int j = 0; j < tx.sigs[i].pubKeys.length; j++) { 28 | if (tx.sigs[i].M == 1) { 29 | Address address = Address.addressFromPubKey(tx.sigs[i].pubKeys[0]); 30 | list.add(address); 31 | } 32 | } 33 | } 34 | return list; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/demo/vmtest/vm/ExecutionContext.java: -------------------------------------------------------------------------------- 1 | package demo.vmtest.vm; 2 | 3 | import com.github.ontio.core.scripts.ScriptOp; 4 | import demo.vmtest.utils.VmReader; 5 | 6 | public class ExecutionContext { 7 | public byte[] Code; 8 | public VmReader OpReader; 9 | public int InstructionPointer; 10 | public ExecutionEngine engine; 11 | 12 | public ExecutionContext(ExecutionEngine engine, byte[] code) { 13 | this.engine = engine; 14 | Code = code; 15 | OpReader = new VmReader(code); 16 | InstructionPointer = 0; 17 | } 18 | 19 | public int GetInstructionPointer() { 20 | return OpReader.Position(); 21 | } 22 | 23 | public long SetInstructionPointer(long offset) { 24 | return OpReader.Seek(offset); 25 | } 26 | 27 | public ScriptOp NextInstruction() { 28 | return ScriptOp.valueOf(Code[OpReader.Position()]); 29 | } 30 | 31 | public ExecutionContext Clone() { 32 | ExecutionContext executionContext = new ExecutionContext(engine, Code); 33 | executionContext.InstructionPointer = this.InstructionPointer; 34 | executionContext.SetInstructionPointer(this.GetInstructionPointer()); 35 | return executionContext; 36 | } 37 | } -------------------------------------------------------------------------------- /src/main/java/demo/vmtest/vm/OpExec.java: -------------------------------------------------------------------------------- 1 | package demo.vmtest.vm; 2 | 3 | import com.github.ontio.core.scripts.ScriptOp; 4 | import demo.vmtest.utils.PushData; 5 | 6 | import java.lang.reflect.InvocationTargetException; 7 | import java.lang.reflect.Method; 8 | 9 | public class OpExec { 10 | public ScriptOp Opcode; 11 | public String Name; 12 | public Method ExecFunc; 13 | public Method ValidatorFunc; 14 | 15 | public OpExec(ScriptOp opcode, String name, Method execMethod, Method validatorMethod) throws Exception { 16 | Opcode = opcode; 17 | Name = name; 18 | ExecFunc = execMethod; 19 | ValidatorFunc = validatorMethod; 20 | } 21 | 22 | public VMState Exec(ExecutionEngine engine) { 23 | try { 24 | ExecFunc.invoke(PushData.class.newInstance(), engine); 25 | } catch (IllegalAccessException e) { 26 | e.printStackTrace(); 27 | System.exit(0); 28 | } catch (InvocationTargetException e) { 29 | e.printStackTrace(); 30 | System.exit(0); 31 | } catch (InstantiationException e) { 32 | e.printStackTrace(); 33 | System.exit(0); 34 | } 35 | return VMState.NONE; 36 | } 37 | 38 | boolean Validator(ExecutionEngine engine) { 39 | try { 40 | if (ValidatorFunc == null) { 41 | return true; 42 | } 43 | ValidatorFunc.invoke(OpExecList.class.newInstance(), engine); 44 | } catch (IllegalAccessException e) { 45 | e.printStackTrace(); 46 | System.exit(0); 47 | } catch (InvocationTargetException e) { 48 | e.printStackTrace(); 49 | System.exit(0); 50 | } catch (InstantiationException e) { 51 | e.printStackTrace(); 52 | System.exit(0); 53 | } 54 | return false; 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /src/main/java/demo/vmtest/vm/VMState.java: -------------------------------------------------------------------------------- 1 | package demo.vmtest.vm; 2 | 3 | import com.github.ontio.common.ErrorCode; 4 | 5 | public enum VMState { 6 | NONE(0x00), 7 | HALT(0x01), 8 | FAULT(0x02), 9 | BREAK(0x04), 10 | INSUFFICIENT_RESOURCE(0x10); 11 | public int value; 12 | 13 | private VMState(int b) { 14 | this.value = b; 15 | } 16 | 17 | public int getValue() { 18 | return value; 19 | } 20 | 21 | public static VMState valueOf(int b) throws Exception { 22 | for (VMState k : VMState.values()) { 23 | if (k.value == b) { 24 | return k; 25 | } 26 | } 27 | throw new Exception(ErrorCode.ParamError); 28 | } 29 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/OntSdkTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio; 2 | 3 | import org.junit.After; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | public class OntSdkTest { 10 | private OntSdk ontSdk; 11 | // public static String URL = "http://polaris1.ont.io:20334"; 12 | // public static String URL = "http://139.219.129.26:20334"; 13 | public static String URL = "http://127.0.0.1:20334"; 14 | 15 | public static String PRIVATEKEY3 = "c19f16785b8f3543bbaf5e1dbb5d398dfa6c85aaad54fc9d71203ce83e505c07";//有钱的账号的私钥 16 | public static String PRIVATEKEY2 = "f1442d5e7f4e2061ff9a6884d6d05212e2aa0f6a6284f0a28ae82a29cdb3d656";//有钱的账号的私钥 17 | public static String PRIVATEKEY = "75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf"; 18 | 19 | public static String PASSWORD = "111111";//有钱账号的密码 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | ontSdk = OntSdk.getInstance(); 24 | } 25 | 26 | @After 27 | public void tearDown() throws Exception { 28 | } 29 | 30 | @Test 31 | public void getInstance() { 32 | OntSdk ontSdk = OntSdk.getInstance(); 33 | assertNotNull(ontSdk); 34 | assertSame(ontSdk,this.ontSdk); 35 | } 36 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/account/AccountTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.account; 2 | 3 | import com.github.ontio.common.Helper; 4 | import com.github.ontio.crypto.SignatureScheme; 5 | import org.junit.Test; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | public class AccountTest { 10 | 11 | 12 | @Test 13 | public void generateSignature() throws Exception { 14 | Account account = new Account(SignatureScheme.SHA256WITHECDSA); 15 | byte[] signature = account.generateSignature("hello".getBytes(),SignatureScheme.SHA256WITHECDSA,null); 16 | boolean b = account.verifySignature("hello".getBytes(),signature); 17 | assertTrue(b); 18 | } 19 | 20 | @Test 21 | public void serializePublicKey() throws Exception { 22 | Account account = new Account(SignatureScheme.SHA256WITHECDSA); 23 | byte[] publickey = account.serializePublicKey(); 24 | assertNotNull(publickey); 25 | } 26 | 27 | @Test 28 | public void serializePrivateKey() throws Exception { 29 | Account account = new Account(SignatureScheme.SHA256WITHECDSA); 30 | byte[] privateKey = account.serializePrivateKey(); 31 | assertNotNull(privateKey); 32 | } 33 | 34 | @Test 35 | public void compareTo() throws Exception { 36 | Account account1 = new Account(SignatureScheme.SHA256WITHECDSA); 37 | Account account2 = new Account(SignatureScheme.SHA256WITHECDSA); 38 | int res = account1.compareTo(account2); 39 | assertNotNull(res); 40 | } 41 | 42 | @Test 43 | public void exportCtrEncryptedPrikey1() throws Exception { 44 | Account account = new Account(SignatureScheme.SHA256WITHECDSA); 45 | String encruPri = account.exportCtrEncryptedPrikey("111111",16384); 46 | String privateKey = Account.getCtrDecodedPrivateKey(encruPri,"111111",account.getAddressU160().toBase58(),16384,SignatureScheme.SHA256WITHECDSA); 47 | assertEquals(privateKey,Helper.toHexString(account.serializePrivateKey())); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/common/AddressTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.common; 2 | 3 | import com.github.ontio.account.Account; 4 | import com.github.ontio.crypto.SignatureScheme; 5 | import com.github.ontio.sdk.exception.SDKException; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import static org.junit.Assert.*; 10 | 11 | public class AddressTest { 12 | Account account; 13 | @Before 14 | public void setUp() throws Exception { 15 | account = new Account(SignatureScheme.SHA256WITHECDSA); 16 | } 17 | 18 | @Test 19 | public void compareTo() throws Exception { 20 | Account account2 = new Account(SignatureScheme.SHA256WITHECDSA); 21 | int res = account2.getAddressU160().compareTo(account.getAddressU160()); 22 | assertNotNull(res); 23 | } 24 | 25 | @Test 26 | public void parse() { 27 | // Address address = Address.parse(account.getAddressU160().toHexString()); 28 | // assertEquals(address,account.getAddressU160()); 29 | } 30 | 31 | @Test 32 | public void addressFromPubKey() { 33 | Address address = Address.addressFromPubKey(account.serializePublicKey()); 34 | assertEquals(address,account.getAddressU160()); 35 | } 36 | 37 | @Test 38 | public void addressFromPubKey1() { 39 | Address address = Address.addressFromPubKey(Helper.toHexString(account.serializePublicKey())); 40 | assertEquals(address,account.getAddressU160()); 41 | } 42 | 43 | @Test 44 | public void addressFromMultiPubKeys() throws Exception { 45 | Account account2 = new Account(SignatureScheme.SHA256WITHECDSA); 46 | Address res = Address.addressFromMultiPubKeys(2,account.serializePublicKey(),account2.serializePublicKey()); 47 | assertNotNull(res); 48 | } 49 | 50 | @Test 51 | public void toBase58() throws SDKException { 52 | String res = account.getAddressU160().toBase58(); 53 | Address addr = Address.decodeBase58(res); 54 | assertEquals(addr,account.getAddressU160()); 55 | } 56 | 57 | @Test 58 | public void toScriptHash() { 59 | Address addr = Address.toScriptHash(Helper.hexToBytes("12a67b")); 60 | assertNotNull(addr); 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/VmTypeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.core; 21 | 22 | import org.junit.Assert; 23 | import org.junit.Test; 24 | 25 | public class VmTypeTest { 26 | 27 | @Test 28 | public void valueOf() throws IllegalArgumentException { 29 | Assert.assertEquals(VmType.NEOVM, VmType.valueOf((byte) 0x01)); 30 | Assert.assertEquals(VmType.WASMVM, VmType.valueOf((byte) 0x03)); 31 | } 32 | 33 | @Test 34 | public void value() { 35 | Assert.assertEquals(1, VmType.NEOVM.value()); 36 | Assert.assertEquals(3, VmType.WASMVM.value()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/asset/ContractTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.asset; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import com.github.ontio.sdk.exception.SDKException; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import java.io.ByteArrayInputStream; 11 | import java.io.ByteArrayOutputStream; 12 | import java.io.DataInputStream; 13 | import java.io.IOException; 14 | 15 | import static org.junit.Assert.*; 16 | 17 | public class ContractTest { 18 | 19 | Address address; 20 | 21 | @Before 22 | public void setUp() throws SDKException { 23 | // address = Address.decodeBase58("TA6nRD9DqGkE8xRJaB37bW2KQEz59ovKRH"); 24 | } 25 | 26 | @Test 27 | public void serialize() throws IOException { 28 | // Contract contract = new Contract((byte)1,address,"test","t".getBytes()); 29 | // ByteArrayOutputStream bs = new ByteArrayOutputStream(); 30 | // BinaryWriter binaryWriter = new BinaryWriter(bs); 31 | // contract.serialize(binaryWriter); 32 | // binaryWriter.flush(); 33 | // byte[] seril = bs.toByteArray(); 34 | // assertNotNull(seril); 35 | // 36 | // Contract contract1 = new Contract((byte)1,address,"test2","t2".getBytes()); 37 | // ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(seril); 38 | // BinaryReader binaryReader = new BinaryReader(byteArrayInputStream); 39 | // contract1.deserialize(binaryReader); 40 | // assertNotNull(binaryReader); 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/asset/StateTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.asset; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.common.Helper; 5 | import com.github.ontio.io.BinaryReader; 6 | import com.github.ontio.io.BinaryWriter; 7 | import com.github.ontio.sdk.exception.SDKException; 8 | import org.junit.Test; 9 | 10 | import java.io.ByteArrayInputStream; 11 | import java.io.ByteArrayOutputStream; 12 | import java.io.IOException; 13 | 14 | import static org.junit.Assert.*; 15 | 16 | public class StateTest { 17 | @Test 18 | public void deserialize() throws SDKException, IOException { 19 | // State state = new State(Address.decodeBase58("TRj9g9kvq8pL3L8M1F2KpsqWQHiC7u4xLP"),Address.decodeBase58("TRj9g9kvq8pL3L8M1F2KpsqWQHiC7u4xLP"),1000L); 20 | // ByteArrayOutputStream bais = new ByteArrayOutputStream(); 21 | // BinaryWriter bw = new BinaryWriter(bais); 22 | // Transfers transfers = new Transfers(new State[]{state}); 23 | // System.out.println(Helper.toHexString(transfers.toArray())); 24 | // 25 | // 26 | // state.serialize(bw); 27 | // 28 | // State state1 = new State(); 29 | // ByteArrayInputStream baos = new ByteArrayInputStream(bais.toByteArray()); 30 | // BinaryReader br = new BinaryReader(baos); 31 | // state1.deserialize(br); 32 | } 33 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/block/BlockTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.block; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.common.Helper; 5 | import com.github.ontio.common.UInt256; 6 | import com.github.ontio.core.transaction.Transaction; 7 | import com.github.ontio.io.BinaryReader; 8 | import com.github.ontio.io.BinaryWriter; 9 | import com.github.ontio.sdk.exception.SDKException; 10 | import org.junit.Test; 11 | 12 | import java.io.ByteArrayInputStream; 13 | import java.io.ByteArrayOutputStream; 14 | import java.io.IOException; 15 | 16 | import static org.junit.Assert.*; 17 | 18 | public class BlockTest { 19 | 20 | @Test 21 | public void deserialize() { 22 | } 23 | 24 | 25 | @Test 26 | public void serialize() throws IOException, SDKException { 27 | // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 28 | // BinaryWriter binaryWriter = new BinaryWriter(byteArrayOutputStream); 29 | // Block block = new Block(); 30 | // block.version = 1; 31 | // block.height = 1; 32 | // block.consensusPayload = "test".getBytes(); 33 | // block.prevBlockHash = new UInt256(Helper.hexToBytes("1d46ec977e10d297a53d77dfcb5fe5904734f2c62e156d0e893d1b7c050524a2")); 34 | // block.blockRoot = new UInt256(Helper.hexToBytes("37614956f598e0b3c4c3105d9e94c8cf4aa0ac6adce4eff4189dd348d1f3dac2")); 35 | // block.consensusData = 111; 36 | // block.nextBookkeeper = Address.decodeBase58("TAERF2G54oJxN4U52CVns7kTYAMFxAFsGg"); 37 | // block.bookkeepers = new byte[][]{"test".getBytes()}; 38 | // block.transactionsRoot = new UInt256(Helper.hexToBytes("b5b8cb62d5c1ccea510ce1c268259fab33069c343532c804743bd4c6029dbd35")); 39 | // block.sigData = new String[]{"123ab2"}; 40 | // block.transactions = new Transaction[]{}; 41 | // block.serialize(binaryWriter); 42 | // binaryWriter.flush(); 43 | // byte[] seril = byteArrayOutputStream.toByteArray(); 44 | // Block block1 = new Block(); 45 | // 46 | // ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(seril); 47 | // block1.deserializeUnsigned(new BinaryReader(byteArrayInputStream)); 48 | // assertEquals(block,block1); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/payload/DeployCodeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.payload; 2 | 3 | import com.github.ontio.core.VmType; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import org.junit.Test; 7 | 8 | import java.io.ByteArrayInputStream; 9 | import java.io.ByteArrayOutputStream; 10 | import java.io.DataOutputStream; 11 | import java.io.IOException; 12 | 13 | import static org.junit.Assert.*; 14 | 15 | public class DeployCodeTest { 16 | 17 | @Test 18 | public void serializeExclusiveData() throws IOException { 19 | DeployCode deployCode = new DeployCode(); 20 | deployCode.version = "1"; 21 | deployCode.author = "sss"; 22 | deployCode.name = "sss"; 23 | deployCode.code = "test".getBytes(); 24 | deployCode.description = "test"; 25 | deployCode.email = "test"; 26 | deployCode.needStorage = true; 27 | 28 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 29 | BinaryWriter binaryWriter = new BinaryWriter(byteArrayOutputStream); 30 | deployCode.serializeExclusiveData(binaryWriter); 31 | 32 | byte[] selr = byteArrayOutputStream.toByteArray(); 33 | 34 | DeployCode deployCode1 = new DeployCode(); 35 | deployCode1.deserializeExclusiveData(new BinaryReader(new ByteArrayInputStream(selr))); 36 | assertEquals(deployCode.version,deployCode1.version); 37 | } 38 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/payload/InvokeCodeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.payload; 2 | 3 | import com.github.ontio.core.VmType; 4 | import com.github.ontio.io.BinaryReader; 5 | import com.github.ontio.io.BinaryWriter; 6 | import org.junit.Test; 7 | 8 | import java.io.ByteArrayInputStream; 9 | import java.io.ByteArrayOutputStream; 10 | import java.io.IOException; 11 | 12 | import static org.junit.Assert.*; 13 | 14 | public class InvokeCodeTest { 15 | 16 | @Test 17 | public void serializeExclusiveData() throws IOException { 18 | InvokeCode invokeCode = new InvokeCode(); 19 | invokeCode.code = "test".getBytes(); 20 | 21 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 22 | BinaryWriter binaryWriter = new BinaryWriter(byteArrayOutputStream); 23 | invokeCode.serializeExclusiveData(binaryWriter); 24 | 25 | assertNotNull(byteArrayOutputStream); 26 | 27 | InvokeCode invokeCode1 = new InvokeCode(); 28 | invokeCode1.deserializeExclusiveData(new BinaryReader(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))); 29 | } 30 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/payload/InvokeWasmCodeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.payload; 2 | 3 | import com.github.ontio.common.Helper; 4 | import com.github.ontio.core.transaction.Transaction; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | public class InvokeWasmCodeTest { 9 | @Test 10 | public void deserialize() throws IOException { 11 | byte[] bs = Helper.hexToBytes("00d2757ecf5ef40100000000000000127a0000000000c849169e39c9cfac6d4a82a3b21e0eb8c566f6d7fda2025e0f5b1dcea427947f4e8f4824fb73908d5041b2fd8b020962617463685f61646414288fb05ca7a85bf4172f288a9f5f35ee0ca5abbe6e10117ae814cd20be059d8ac4d14c8fa03483fbd7e5b98d84cd8b6fda5d19ba09c5d212845fe67d0a5242a017d33f25f6e68ed510580a5938c8322792acf1040c143acb0f659c97f2220b5155f330f2770d2fb3b34cffea292ef8f2e1d72296a8fb0f58f18d215b5cda953646f24e54d749389e41b3774a7f06848c22804ffea65863c77622e8f13c3dc0feef8736a191fd1170b218a973a3745bf7e8e1021980a1f8cd2b37969d645cc894924a5c93c86896f2a68aae36a80e133fd0edebcf455ac21465ecea12557845823fab61b6c047a9bb4fa881ef20c44992666b5eb9245296b05a18a0c8f2d4b7433e90c34c2435c631814bcacb3cafc6ba7ba94409a2f257becd3549aa1e2fe441b28efae906a665c13c0753d09a2adeebb28b943dd971bebbaa5c585d7b83c617e5f61e4d6a00f2dd97e7991916a869ed9d148369a99dd73d648ef6e7793ea39615247ae2cdf5721f902f70df6d96c345ae8dba7e6c52aa1a5d367fd6c57412eb065fb8126bfdc65519bf2a49c5bb685cd318bccb3f05ef58e2ac2bdb0dc58f8079cfd710d6bf3aed6b9f3594695609245556c468da6a9db033856096cfdcb561a16f9954b40372c257e736118b94fd482df0ab65cbcbf309cc8e168789eedfbfe8fd722097f944e9928ae217e5af9734c7f8457117c6b8ee300dd772533fa5f94511dc34914100f8fa953246915f3a98c69e911b522e9adf9ac26295209aeb0efa831acfc21885a5480a0b71542167038871bc1851e506fdd1aca85b3a5d717606d7b479a2781b137cf1cf8cff9d45365015111d562dbcc975ad3f1edf4dce0b80ff8787d2915db41de8e86442485d8c4279711e5e8837d4ff76cf76430d503600014140fdb08e2548222f01cff1226fbf562efa5e88af988877e6a26494e6ba6606637c73802af2eb4b5c1b76ddeec88961d5aa31693ffafb6a2300c9b770f762f17a11232102795dd24e035bd3072708920e833654ccd672d4f646358939778b279cf84e6abaac"); 12 | Transaction tx = Transaction.deserializeFrom(bs); 13 | System.out.println(tx.txType); 14 | System.out.println((InvokeWasmCode)tx); 15 | } 16 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/scripts/ScriptBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.scripts; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import java.math.BigInteger; 7 | 8 | import static org.junit.Assert.*; 9 | 10 | public class ScriptBuilderTest { 11 | 12 | ScriptBuilder scriptBuilder; 13 | 14 | @Before 15 | public void setUp(){ 16 | scriptBuilder = new ScriptBuilder(); 17 | } 18 | 19 | @Test 20 | public void add() { 21 | ScriptBuilder sb = scriptBuilder.add("test".getBytes()); 22 | assertNotNull(sb); 23 | 24 | } 25 | 26 | @Test 27 | public void push() { 28 | ScriptBuilder sb = scriptBuilder.emitPushBool(true); 29 | assertNotNull(sb); 30 | assertNotNull(scriptBuilder.emitPushByteArray("test".getBytes())); 31 | assertNotNull(scriptBuilder.emitPushInteger(new BigInteger("11"))); 32 | } 33 | 34 | 35 | @Test 36 | public void pushPack() { 37 | assertNotNull(scriptBuilder.pushPack()); 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/core/transaction/TransactionTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.core.transaction; 2 | 3 | import com.github.ontio.OntSdk; 4 | import com.github.ontio.account.Account; 5 | import com.github.ontio.common.Address; 6 | import com.github.ontio.common.Helper; 7 | import com.github.ontio.crypto.SignatureScheme; 8 | import com.github.ontio.sdk.exception.SDKException; 9 | import com.github.ontio.smartcontract.Vm; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | 13 | import java.io.IOException; 14 | 15 | import static org.junit.Assert.*; 16 | 17 | public class TransactionTest { 18 | 19 | OntSdk ontSdk; 20 | Vm vm; 21 | String ontContract = "ff00000000000000000000000000000000000001"; 22 | 23 | @Before 24 | public void setUp(){ 25 | ontSdk = OntSdk.getInstance(); 26 | vm = new Vm(ontSdk); 27 | } 28 | 29 | @Test 30 | public void serialize() throws Exception { 31 | Transaction tx = vm.buildNativeParams(Address.parse(ontContract),"init","1".getBytes(),null,0,0); 32 | Account account = new Account(Helper.hexToBytes("0bc8c1f75a028672cd42c221bf81709dfc7abbbaf0d87cb6fdeaf9a20492c194"),SignatureScheme.SHA256WITHECDSA); 33 | ontSdk.signTx(tx,new Account[][]{{account}}); 34 | 35 | String t = tx.toHexString(); 36 | System.out.println(t); 37 | 38 | Transaction tx2 = Transaction.deserializeFrom(Helper.hexToBytes(t)); 39 | System.out.println(tx2.json()); 40 | 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/crypto/MnemonicTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018-2019 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.crypto; 21 | 22 | import io.github.novacrypto.bip39.Words; 23 | import org.junit.Assert; 24 | import org.junit.Test; 25 | 26 | public class MnemonicTest { 27 | @Test 28 | public void TestMnemonic() { 29 | Assert.assertEquals(12, MnemonicCode.generateMnemonicCodesStr().split(" ").length); 30 | Assert.assertEquals(12, MnemonicCode.generateMnemonicCodesStr(Words.TWELVE).split(" ").length); 31 | Assert.assertEquals(15, MnemonicCode.generateMnemonicCodesStr(Words.FIFTEEN).split(" ").length); 32 | Assert.assertEquals(18, MnemonicCode.generateMnemonicCodesStr(Words.EIGHTEEN).split(" ").length); 33 | Assert.assertEquals(21, MnemonicCode.generateMnemonicCodesStr(Words.TWENTY_ONE).split(" ").length); 34 | Assert.assertEquals(24, MnemonicCode.generateMnemonicCodesStr(Words.TWENTY_FOUR).split(" ").length); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/io/BinaryWriterTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.io; 2 | 3 | import com.github.ontio.common.Address; 4 | import com.github.ontio.common.Helper; 5 | import com.github.ontio.sdk.exception.SDKException; 6 | import org.junit.Test; 7 | 8 | import java.io.ByteArrayOutputStream; 9 | import java.io.IOException; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | public class BinaryWriterTest { 14 | 15 | @Test 16 | public void writeVarInt() throws IOException { 17 | ByteArrayOutputStream ms = new ByteArrayOutputStream(); 18 | BinaryWriter binaryWriter = new BinaryWriter(ms); 19 | binaryWriter.writeVarInt((long) 2544); 20 | binaryWriter.flush(); 21 | assertNotNull(ms); 22 | } 23 | 24 | @Test 25 | public void write() throws IOException { 26 | ByteArrayOutputStream ms = new ByteArrayOutputStream(); 27 | BinaryWriter binaryWriter = new BinaryWriter(ms); 28 | binaryWriter.write("test".getBytes()); 29 | binaryWriter.flush(); 30 | assertNotNull(ms); 31 | } 32 | 33 | @Test 34 | public void writeInt() throws IOException { 35 | ByteArrayOutputStream ms = new ByteArrayOutputStream(); 36 | BinaryWriter binaryWriter = new BinaryWriter(ms); 37 | binaryWriter.writeInt(1); 38 | binaryWriter.flush(); 39 | assertNotNull(ms); 40 | } 41 | 42 | @Test 43 | public void writeSerializable() throws IOException, SDKException { 44 | // ByteArrayOutputStream ms = new ByteArrayOutputStream(); 45 | // BinaryWriter binaryWriter = new BinaryWriter(ms); 46 | // Address address = Address.decodeBase58("TA6nRD9DqGkE8xRJaB37bW2KQEz59ovKRH"); 47 | // binaryWriter.writeSerializable(address); 48 | // binaryWriter.flush(); 49 | // assertNotNull(ms); 50 | } 51 | 52 | @Test 53 | public void writeVarBytes() throws IOException { 54 | ByteArrayOutputStream ms = new ByteArrayOutputStream(); 55 | BinaryWriter binaryWriter = new BinaryWriter(ms); 56 | binaryWriter.writeVarBytes("test".getBytes()); 57 | binaryWriter.flush(); 58 | assertNotNull(ms); 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/sdk/wallet/WalletTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.sdk.wallet; 2 | 3 | import com.github.ontio.OntSdk; 4 | import org.junit.After; 5 | import org.junit.Assert; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import java.io.File; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | public class WalletTest { 14 | 15 | OntSdk ontSdk; 16 | Identity id1; 17 | Identity id2; 18 | Account acct1; 19 | Account acct2; 20 | 21 | String walletFile = "WalletTest.json"; 22 | 23 | @Before 24 | public void setUp() throws Exception { 25 | ontSdk = OntSdk.getInstance(); 26 | ontSdk.openWalletFile(walletFile); 27 | 28 | 29 | id1 = ontSdk.getWalletMgr().createIdentity("passwordtest"); 30 | id2 = ontSdk.getWalletMgr().createIdentity("passwordtest"); 31 | 32 | acct1 = ontSdk.getWalletMgr().createAccount("passwordtest"); 33 | acct2 = ontSdk.getWalletMgr().createAccount("passwordtest"); 34 | } 35 | 36 | @After 37 | public void removeWallet(){ 38 | File file = new File(walletFile); 39 | if(file.exists()){ 40 | if(file.delete()){ 41 | System.out.println("delete wallet file success"); 42 | } 43 | } 44 | } 45 | 46 | 47 | @Test 48 | public void getAccount() throws Exception { 49 | Account acct = ontSdk.getWalletMgr().getWallet().getAccount(acct1.address); 50 | Assert.assertNotNull(acct); 51 | 52 | ontSdk.getWalletMgr().getWallet().setDefaultIdentity(id1.ontid); 53 | ontSdk.getWalletMgr().getWallet().setDefaultIdentity(1); 54 | ontSdk.getWalletMgr().getWallet().setDefaultAccount(acct1.address); 55 | ontSdk.getWalletMgr().getWallet().setDefaultAccount(1); 56 | Identity did = ontSdk.getWalletMgr().getWallet().getIdentity(id1.ontid); 57 | Assert.assertNotNull(did); 58 | boolean b = ontSdk.getWalletMgr().getWallet().removeIdentity(id1.ontid); 59 | Assert.assertTrue(b); 60 | 61 | boolean b2 = ontSdk.getWalletMgr().getWallet().removeAccount(acct1.address); 62 | Assert.assertTrue(b2); 63 | 64 | 65 | } 66 | 67 | 68 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/smartcontract/VmTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ontio.smartcontract; 2 | 3 | import com.github.ontio.OntSdk; 4 | import com.github.ontio.common.Address; 5 | import com.github.ontio.sdk.exception.SDKException; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import static org.junit.Assert.*; 10 | 11 | public class VmTest { 12 | 13 | OntSdk ontSdk; 14 | Vm vm; 15 | @Before 16 | public void setUp(){ 17 | ontSdk = OntSdk.getInstance(); 18 | vm = new Vm(ontSdk); 19 | 20 | } 21 | 22 | @Test 23 | public void buildNativeParams() throws SDKException { 24 | // Address addr = Address.decodeBase58("TA9MXtwAcXkUMuujJh2iNRaWoXrvzfrmZb"); 25 | // vm.buildNativeParams(addr,"init","1".getBytes(),null,0,0); 26 | } 27 | } -------------------------------------------------------------------------------- /src/test/java/com/github/ontio/smartcontract/nativevm/GovernanceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018-2019 The ontology Authors 3 | * This file is part of The ontology library. 4 | * 5 | * The ontology is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The ontology is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with The ontology. If not, see . 17 | * 18 | */ 19 | 20 | package com.github.ontio.smartcontract.nativevm; 21 | 22 | import org.junit.Test; 23 | 24 | import static org.junit.Assert.assertEquals; 25 | import static org.junit.Assert.assertNull; 26 | 27 | public class GovernanceTest { 28 | @Test 29 | public void newPeerAttributes() { 30 | PeerAttributes peerAttributes1 = new PeerAttributes(); 31 | assertNull(peerAttributes1.peerPubkey); 32 | assertEquals(0, peerAttributes1.maxAuthorize); 33 | assertEquals(0, peerAttributes1.t1PeerCost); 34 | assertEquals(0, peerAttributes1.t2PeerCost); 35 | assertEquals(0, peerAttributes1.tPeerCost); 36 | String peerPubKey = "0379eff8cc07441daad01234291ba3f3da3e323119d97d6f1875da5f414be470b9"; 37 | PeerAttributes peerAttributes2 = new PeerAttributes(peerPubKey); 38 | assertEquals(peerPubKey, peerAttributes2.peerPubkey); 39 | assertEquals(0, peerAttributes2.maxAuthorize); 40 | assertEquals(100, peerAttributes2.t1PeerCost); 41 | assertEquals(100, peerAttributes2.t2PeerCost); 42 | assertEquals(100, peerAttributes2.tPeerCost); 43 | } 44 | } 45 | --------------------------------------------------------------------------------