phantasma-sdk-ts 0.1.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +13 -0
- package/.eslintrc.cjs +15 -0
- package/.github/workflows/npm-publish.yml +47 -0
- package/.prettierignore +13 -0
- package/.prettierrc +8 -0
- package/LICENSE +201 -0
- package/README.md +842 -0
- package/babel.config.js +6 -0
- package/core/index.d.ts +10 -0
- package/core/index.d.ts.map +1 -0
- package/core/index.js +25 -0
- package/core/interfaces/IContract.d.ts +27 -0
- package/core/interfaces/IContract.d.ts.map +1 -0
- package/core/interfaces/IContract.js +25 -0
- package/core/interfaces/IKeyPair.d.ts +7 -0
- package/core/interfaces/IKeyPair.d.ts.map +1 -0
- package/core/interfaces/IKeyPair.js +2 -0
- package/core/interfaces/ISerializable.d.ts +6 -0
- package/core/interfaces/ISerializable.d.ts.map +1 -0
- package/core/interfaces/ISerializable.js +9 -0
- package/core/interfaces/IStack.d.ts +7 -0
- package/core/interfaces/IStack.d.ts.map +1 -0
- package/core/interfaces/IStack.js +2 -0
- package/core/interfaces/IToken.d.ts +29 -0
- package/core/interfaces/IToken.d.ts.map +1 -0
- package/core/interfaces/IToken.js +22 -0
- package/core/interfaces/Signature.d.ts +21 -0
- package/core/interfaces/Signature.d.ts.map +1 -0
- package/core/interfaces/Signature.js +31 -0
- package/core/interfaces/index.d.ts +7 -0
- package/core/interfaces/index.d.ts.map +1 -0
- package/core/interfaces/index.js +22 -0
- package/core/ledger/Address-Transcode.d.ts +20 -0
- package/core/ledger/Address-Transcode.d.ts.map +1 -0
- package/core/ledger/Address-Transcode.js +70 -0
- package/core/ledger/Ledger-Commands.d.ts +76 -0
- package/core/ledger/Ledger-Commands.d.ts.map +1 -0
- package/core/ledger/Ledger-Commands.js +530 -0
- package/core/ledger/Ledger-Utils.d.ts +74 -0
- package/core/ledger/Ledger-Utils.d.ts.map +1 -0
- package/core/ledger/Ledger-Utils.js +609 -0
- package/core/ledger/LedgerConfig.d.ts +17 -0
- package/core/ledger/LedgerConfig.d.ts.map +1 -0
- package/core/ledger/LedgerConfig.js +2 -0
- package/core/ledger/Mnemonic.d.ts +35 -0
- package/core/ledger/Mnemonic.d.ts.map +1 -0
- package/core/ledger/Mnemonic.js +65 -0
- package/core/ledger/Transaction-Sign.d.ts +10 -0
- package/core/ledger/Transaction-Sign.d.ts.map +1 -0
- package/core/ledger/Transaction-Sign.js +149 -0
- package/core/ledger/Transaction-Transcode.d.ts +24 -0
- package/core/ledger/Transaction-Transcode.d.ts.map +1 -0
- package/core/ledger/Transaction-Transcode.js +47 -0
- package/core/ledger/index.d.ts +9 -0
- package/core/ledger/index.d.ts.map +1 -0
- package/core/ledger/index.js +24 -0
- package/core/ledger/interfaces/ApplicationNameResponse.d.ts +6 -0
- package/core/ledger/interfaces/ApplicationNameResponse.d.ts.map +1 -0
- package/core/ledger/interfaces/ApplicationNameResponse.js +2 -0
- package/core/ledger/interfaces/Device.d.ts +7 -0
- package/core/ledger/interfaces/Device.d.ts.map +1 -0
- package/core/ledger/interfaces/Device.js +2 -0
- package/core/ledger/interfaces/DeviceResponse.d.ts +7 -0
- package/core/ledger/interfaces/DeviceResponse.d.ts.map +1 -0
- package/core/ledger/interfaces/DeviceResponse.js +2 -0
- package/core/ledger/interfaces/Ledger.d.ts +2 -0
- package/core/ledger/interfaces/Ledger.d.ts.map +1 -0
- package/core/ledger/interfaces/Ledger.js +2 -0
- package/core/ledger/interfaces/LedgerBalanceFromLedgerResponse.d.ts +12 -0
- package/core/ledger/interfaces/LedgerBalanceFromLedgerResponse.d.ts.map +1 -0
- package/core/ledger/interfaces/LedgerBalanceFromLedgerResponse.js +2 -0
- package/core/ledger/interfaces/LedgerConfig.d.ts +17 -0
- package/core/ledger/interfaces/LedgerConfig.d.ts.map +1 -0
- package/core/ledger/interfaces/LedgerConfig.js +2 -0
- package/core/ledger/interfaces/LedgerDeviceInfoResponse.d.ts +7 -0
- package/core/ledger/interfaces/LedgerDeviceInfoResponse.d.ts.map +1 -0
- package/core/ledger/interfaces/LedgerDeviceInfoResponse.js +2 -0
- package/core/ledger/interfaces/LedgerSendTransactionResponse.d.ts +5 -0
- package/core/ledger/interfaces/LedgerSendTransactionResponse.d.ts.map +1 -0
- package/core/ledger/interfaces/LedgerSendTransactionResponse.js +2 -0
- package/core/ledger/interfaces/LedgerSigner.d.ts +6 -0
- package/core/ledger/interfaces/LedgerSigner.d.ts.map +1 -0
- package/core/ledger/interfaces/LedgerSigner.js +2 -0
- package/core/ledger/interfaces/LedgerSignerData.d.ts +8 -0
- package/core/ledger/interfaces/LedgerSignerData.d.ts.map +1 -0
- package/core/ledger/interfaces/LedgerSignerData.js +2 -0
- package/core/ledger/interfaces/PublicKeyResponse.d.ts +6 -0
- package/core/ledger/interfaces/PublicKeyResponse.d.ts.map +1 -0
- package/core/ledger/interfaces/PublicKeyResponse.js +2 -0
- package/core/ledger/interfaces/SignResponse.d.ts +6 -0
- package/core/ledger/interfaces/SignResponse.d.ts.map +1 -0
- package/core/ledger/interfaces/SignResponse.js +2 -0
- package/core/ledger/interfaces/VersionResponse.d.ts +6 -0
- package/core/ledger/interfaces/VersionResponse.d.ts.map +1 -0
- package/core/ledger/interfaces/VersionResponse.js +2 -0
- package/core/ledger/interfaces/index.d.ts +14 -0
- package/core/ledger/interfaces/index.d.ts.map +1 -0
- package/core/ledger/interfaces/index.js +29 -0
- package/core/ledger/ledger-comm.d.ts +85 -0
- package/core/ledger/ledger-comm.d.ts.map +1 -0
- package/core/ledger/ledger-comm.js +613 -0
- package/core/link/easyConnect.d.ts +23 -0
- package/core/link/easyConnect.d.ts.map +1 -0
- package/core/link/easyConnect.js +237 -0
- package/core/link/easyScript.d.ts +13 -0
- package/core/link/easyScript.d.ts.map +1 -0
- package/core/link/easyScript.js +52 -0
- package/core/link/index.d.ts +2 -0
- package/core/link/index.d.ts.map +1 -0
- package/core/link/index.js +17 -0
- package/core/link/interfaces/IAccount.d.ts +13 -0
- package/core/link/interfaces/IAccount.d.ts.map +1 -0
- package/core/link/interfaces/IAccount.js +2 -0
- package/core/link/interfaces/IFile.d.ts +7 -0
- package/core/link/interfaces/IFile.d.ts.map +1 -0
- package/core/link/interfaces/IFile.js +2 -0
- package/core/link/interfaces/ProofOfWork.d.ts +9 -0
- package/core/link/interfaces/ProofOfWork.d.ts.map +1 -0
- package/core/link/interfaces/ProofOfWork.js +12 -0
- package/core/link/interfaces/index.d.ts +4 -0
- package/core/link/interfaces/index.d.ts.map +1 -0
- package/core/link/interfaces/index.js +19 -0
- package/core/link/phantasmaLink.d.ts +41 -0
- package/core/link/phantasmaLink.d.ts.map +1 -0
- package/core/link/phantasmaLink.js +479 -0
- package/core/rpc/index.d.ts +3 -0
- package/core/rpc/index.d.ts.map +1 -0
- package/core/rpc/index.js +18 -0
- package/core/rpc/interfaces/ABIContract.d.ts +6 -0
- package/core/rpc/interfaces/ABIContract.d.ts.map +1 -0
- package/core/rpc/interfaces/ABIContract.js +2 -0
- package/core/rpc/interfaces/ABIEvent.d.ts +7 -0
- package/core/rpc/interfaces/ABIEvent.d.ts.map +1 -0
- package/core/rpc/interfaces/ABIEvent.js +2 -0
- package/core/rpc/interfaces/ABIMethod.d.ts +7 -0
- package/core/rpc/interfaces/ABIMethod.d.ts.map +1 -0
- package/core/rpc/interfaces/ABIMethod.js +2 -0
- package/core/rpc/interfaces/ABIParameter.d.ts +5 -0
- package/core/rpc/interfaces/ABIParameter.d.ts.map +1 -0
- package/core/rpc/interfaces/ABIParameter.js +2 -0
- package/core/rpc/interfaces/Account.d.ts +16 -0
- package/core/rpc/interfaces/Account.d.ts.map +1 -0
- package/core/rpc/interfaces/Account.js +2 -0
- package/core/rpc/interfaces/AccountTransactions.d.ts +6 -0
- package/core/rpc/interfaces/AccountTransactions.d.ts.map +1 -0
- package/core/rpc/interfaces/AccountTransactions.js +2 -0
- package/core/rpc/interfaces/Archive.d.ts +11 -0
- package/core/rpc/interfaces/Archive.d.ts.map +1 -0
- package/core/rpc/interfaces/Archive.js +2 -0
- package/core/rpc/interfaces/Auction.d.ts +13 -0
- package/core/rpc/interfaces/Auction.d.ts.map +1 -0
- package/core/rpc/interfaces/Auction.js +2 -0
- package/core/rpc/interfaces/Balance.d.ts +8 -0
- package/core/rpc/interfaces/Balance.d.ts.map +1 -0
- package/core/rpc/interfaces/Balance.js +2 -0
- package/core/rpc/interfaces/Block.d.ts +17 -0
- package/core/rpc/interfaces/Block.d.ts.map +1 -0
- package/core/rpc/interfaces/Block.js +2 -0
- package/core/rpc/interfaces/Chain.d.ts +10 -0
- package/core/rpc/interfaces/Chain.d.ts.map +1 -0
- package/core/rpc/interfaces/Chain.js +2 -0
- package/core/rpc/interfaces/Channel.d.ts +13 -0
- package/core/rpc/interfaces/Channel.d.ts.map +1 -0
- package/core/rpc/interfaces/Channel.js +2 -0
- package/core/rpc/interfaces/Contract.d.ts +11 -0
- package/core/rpc/interfaces/Contract.d.ts.map +1 -0
- package/core/rpc/interfaces/Contract.js +2 -0
- package/core/rpc/interfaces/Dapp.d.ts +6 -0
- package/core/rpc/interfaces/Dapp.d.ts.map +1 -0
- package/core/rpc/interfaces/Dapp.js +2 -0
- package/core/rpc/interfaces/Event.d.ts +7 -0
- package/core/rpc/interfaces/Event.d.ts.map +1 -0
- package/core/rpc/interfaces/Event.js +2 -0
- package/core/rpc/interfaces/Governance.d.ts +5 -0
- package/core/rpc/interfaces/Governance.d.ts.map +1 -0
- package/core/rpc/interfaces/Governance.js +2 -0
- package/core/rpc/interfaces/Interop.d.ts +5 -0
- package/core/rpc/interfaces/Interop.d.ts.map +1 -0
- package/core/rpc/interfaces/Interop.js +2 -0
- package/core/rpc/interfaces/KeyValue.d.ts +5 -0
- package/core/rpc/interfaces/KeyValue.d.ts.map +1 -0
- package/core/rpc/interfaces/KeyValue.js +2 -0
- package/core/rpc/interfaces/Leaderboard.d.ts +6 -0
- package/core/rpc/interfaces/Leaderboard.d.ts.map +1 -0
- package/core/rpc/interfaces/Leaderboard.js +2 -0
- package/core/rpc/interfaces/LeaderboardRow.d.ts +5 -0
- package/core/rpc/interfaces/LeaderboardRow.d.ts.map +1 -0
- package/core/rpc/interfaces/LeaderboardRow.js +2 -0
- package/core/rpc/interfaces/NFT.d.ts +14 -0
- package/core/rpc/interfaces/NFT.d.ts.map +1 -0
- package/core/rpc/interfaces/NFT.js +2 -0
- package/core/rpc/interfaces/Nexus.d.ts +14 -0
- package/core/rpc/interfaces/Nexus.d.ts.map +1 -0
- package/core/rpc/interfaces/Nexus.js +2 -0
- package/core/rpc/interfaces/Oracle.d.ts +5 -0
- package/core/rpc/interfaces/Oracle.d.ts.map +1 -0
- package/core/rpc/interfaces/Oracle.js +2 -0
- package/core/rpc/interfaces/Organization.d.ts +6 -0
- package/core/rpc/interfaces/Organization.d.ts.map +1 -0
- package/core/rpc/interfaces/Organization.js +2 -0
- package/core/rpc/interfaces/Paginated.d.ts +8 -0
- package/core/rpc/interfaces/Paginated.d.ts.map +1 -0
- package/core/rpc/interfaces/Paginated.js +2 -0
- package/core/rpc/interfaces/Peer.d.ts +8 -0
- package/core/rpc/interfaces/Peer.d.ts.map +1 -0
- package/core/rpc/interfaces/Peer.js +2 -0
- package/core/rpc/interfaces/Platform.d.ts +9 -0
- package/core/rpc/interfaces/Platform.d.ts.map +1 -0
- package/core/rpc/interfaces/Platform.js +2 -0
- package/core/rpc/interfaces/Receipt.d.ts +10 -0
- package/core/rpc/interfaces/Receipt.d.ts.map +1 -0
- package/core/rpc/interfaces/Receipt.js +2 -0
- package/core/rpc/interfaces/Script.d.ts +9 -0
- package/core/rpc/interfaces/Script.d.ts.map +1 -0
- package/core/rpc/interfaces/Script.js +2 -0
- package/core/rpc/interfaces/SendRawTx.d.ts +5 -0
- package/core/rpc/interfaces/SendRawTx.d.ts.map +1 -0
- package/core/rpc/interfaces/SendRawTx.js +2 -0
- package/core/rpc/interfaces/SignatureResult.d.ts +5 -0
- package/core/rpc/interfaces/SignatureResult.d.ts.map +1 -0
- package/core/rpc/interfaces/SignatureResult.js +2 -0
- package/core/rpc/interfaces/Stake.d.ts +6 -0
- package/core/rpc/interfaces/Stake.d.ts.map +1 -0
- package/core/rpc/interfaces/Stake.js +2 -0
- package/core/rpc/interfaces/Storage.d.ts +8 -0
- package/core/rpc/interfaces/Storage.d.ts.map +1 -0
- package/core/rpc/interfaces/Storage.js +2 -0
- package/core/rpc/interfaces/Swap.d.ts +13 -0
- package/core/rpc/interfaces/Swap.d.ts.map +1 -0
- package/core/rpc/interfaces/Swap.js +2 -0
- package/core/rpc/interfaces/Token.d.ts +19 -0
- package/core/rpc/interfaces/Token.d.ts.map +1 -0
- package/core/rpc/interfaces/Token.js +2 -0
- package/core/rpc/interfaces/TokenData.d.ts +13 -0
- package/core/rpc/interfaces/TokenData.d.ts.map +1 -0
- package/core/rpc/interfaces/TokenData.js +2 -0
- package/core/rpc/interfaces/TokenExternal.d.ts +5 -0
- package/core/rpc/interfaces/TokenExternal.d.ts.map +1 -0
- package/core/rpc/interfaces/TokenExternal.js +2 -0
- package/core/rpc/interfaces/TokenPrice.d.ts +8 -0
- package/core/rpc/interfaces/TokenPrice.d.ts.map +1 -0
- package/core/rpc/interfaces/TokenPrice.js +2 -0
- package/core/rpc/interfaces/TokenSeries.d.ts +12 -0
- package/core/rpc/interfaces/TokenSeries.d.ts.map +1 -0
- package/core/rpc/interfaces/TokenSeries.js +2 -0
- package/core/rpc/interfaces/TransactionData.d.ts +23 -0
- package/core/rpc/interfaces/TransactionData.d.ts.map +1 -0
- package/core/rpc/interfaces/TransactionData.js +2 -0
- package/core/rpc/interfaces/Validator.d.ts +5 -0
- package/core/rpc/interfaces/Validator.d.ts.map +1 -0
- package/core/rpc/interfaces/Validator.js +2 -0
- package/core/rpc/interfaces/index.d.ts +42 -0
- package/core/rpc/interfaces/index.d.ts.map +1 -0
- package/core/rpc/interfaces/index.js +57 -0
- package/core/rpc/phantasma.d.ts +89 -0
- package/core/rpc/phantasma.d.ts.map +1 -0
- package/core/rpc/phantasma.js +900 -0
- package/core/tx/SignatureKind.js +9 -0
- package/core/tx/Transaction.d.ts +28 -0
- package/core/tx/Transaction.d.ts.map +1 -0
- package/core/tx/Transaction.js +209 -0
- package/core/tx/index.d.ts +3 -0
- package/core/tx/index.d.ts.map +1 -0
- package/core/tx/index.js +18 -0
- package/core/tx/utils.d.ts +10 -0
- package/core/tx/utils.d.ts.map +1 -0
- package/core/tx/utils.js +147 -0
- package/core/types/Address.d.ts +41 -0
- package/core/types/Address.d.ts.map +1 -0
- package/core/types/Address.js +276 -0
- package/core/types/Consensus.d.ts +61 -0
- package/core/types/Consensus.d.ts.map +1 -0
- package/core/types/Consensus.js +151 -0
- package/core/types/Contract.d.ts +60 -0
- package/core/types/Contract.d.ts.map +1 -0
- package/core/types/Contract.js +342 -0
- package/core/types/DomainSettings.d.ts +87 -0
- package/core/types/DomainSettings.d.ts.map +1 -0
- package/core/types/DomainSettings.js +107 -0
- package/core/types/Ed25519Signature.d.ts +16 -0
- package/core/types/Ed25519Signature.d.ts.map +1 -0
- package/core/types/Ed25519Signature.js +78 -0
- package/core/types/Entropy.d.ts +5 -0
- package/core/types/Entropy.d.ts.map +1 -0
- package/core/types/Entropy.js +24 -0
- package/core/types/Extensions/Base16.d.ts +7 -0
- package/core/types/Extensions/Base16.d.ts.map +1 -0
- package/core/types/Extensions/Base16.js +36 -0
- package/core/types/Extensions/BinaryReader.d.ts +17 -0
- package/core/types/Extensions/BinaryReader.d.ts.map +1 -0
- package/core/types/Extensions/BinaryReader.js +192 -0
- package/core/types/Extensions/BinaryWriter.d.ts +25 -0
- package/core/types/Extensions/BinaryWriter.d.ts.map +1 -0
- package/core/types/Extensions/BinaryWriter.js +191 -0
- package/core/types/Extensions/Describer.d.ts +5 -0
- package/core/types/Extensions/Describer.d.ts.map +1 -0
- package/core/types/Extensions/Describer.js +25 -0
- package/core/types/Extensions/PBinaryReader.d.ts +42 -0
- package/core/types/Extensions/PBinaryReader.d.ts.map +1 -0
- package/core/types/Extensions/PBinaryReader.js +269 -0
- package/core/types/Extensions/PBinaryWriter.d.ts +49 -0
- package/core/types/Extensions/PBinaryWriter.d.ts.map +1 -0
- package/core/types/Extensions/PBinaryWriter.js +250 -0
- package/core/types/Extensions/TableColumns.d.ts +5 -0
- package/core/types/Extensions/TableColumns.d.ts.map +1 -0
- package/core/types/Extensions/TableColumns.js +22 -0
- package/core/types/Extensions/index.d.ts +5 -0
- package/core/types/Extensions/index.d.ts.map +1 -0
- package/core/types/Extensions/index.js +20 -0
- package/core/types/PhantasmaKeys.d.ts +19 -0
- package/core/types/PhantasmaKeys.d.ts.map +1 -0
- package/core/types/PhantasmaKeys.js +92 -0
- package/core/types/Serialization.d.ts +22 -0
- package/core/types/Serialization.d.ts.map +1 -0
- package/core/types/Serialization.js +195 -0
- package/core/types/Stack.d.ts +16 -0
- package/core/types/Stack.d.ts.map +1 -0
- package/core/types/Stack.js +42 -0
- package/core/types/Timestamp.d.ts +26 -0
- package/core/types/Timestamp.d.ts.map +1 -0
- package/core/types/Timestamp.js +74 -0
- package/core/types/index.d.ts +12 -0
- package/core/types/index.d.ts.map +1 -0
- package/core/types/index.js +27 -0
- package/core/utils/index.d.ts +25 -0
- package/core/utils/index.d.ts.map +1 -0
- package/core/utils/index.js +236 -0
- package/core/vm/Contracts.d.ts +17 -0
- package/core/vm/Contracts.d.ts.map +1 -0
- package/core/vm/Contracts.js +20 -0
- package/core/vm/Decoder.d.ts +19 -0
- package/core/vm/Decoder.d.ts.map +1 -0
- package/core/vm/Decoder.js +175 -0
- package/core/vm/EventData.d.ts +97 -0
- package/core/vm/EventData.d.ts.map +1 -0
- package/core/vm/EventData.js +134 -0
- package/core/vm/Opcode.d.ts +57 -0
- package/core/vm/Opcode.d.ts.map +1 -0
- package/core/vm/Opcode.js +69 -0
- package/core/vm/ScriptBuilder.d.ts +65 -0
- package/core/vm/ScriptBuilder.d.ts.map +1 -0
- package/core/vm/ScriptBuilder.js +592 -0
- package/core/vm/VMObject.d.ts +55 -0
- package/core/vm/VMObject.d.ts.map +1 -0
- package/core/vm/VMObject.js +1101 -0
- package/core/vm/VMType.d.ts +12 -0
- package/core/vm/VMType.d.ts.map +1 -0
- package/core/vm/VMType.js +15 -0
- package/core/vm/index.d.ts +8 -0
- package/core/vm/index.d.ts.map +1 -0
- package/core/vm/index.js +23 -0
- package/html/TestWebPage.html +187 -0
- package/html/phantasma.js +60504 -0
- package/index.d.ts +5 -0
- package/index.d.ts.map +1 -0
- package/index.js +36 -0
- package/jest.config.js +5 -0
- package/package.json +67 -0
- package/src/core/index.ts +9 -0
- package/src/core/interfaces/IContract.ts +28 -0
- package/src/core/interfaces/IKeyPair.ts +14 -0
- package/src/core/interfaces/ISerializable.ts +6 -0
- package/src/core/interfaces/IStack.ts +6 -0
- package/src/core/interfaces/IToken.ts +31 -0
- package/src/core/interfaces/Signature.ts +32 -0
- package/src/core/interfaces/index.ts +6 -0
- package/src/core/ledger/Address-Transcode.ts +44 -0
- package/src/core/ledger/Ledger-Commands.ts +463 -0
- package/src/core/ledger/Ledger-Utils.ts +549 -0
- package/src/core/ledger/Mnemonic.ts +79 -0
- package/src/core/ledger/Transaction-Sign.ts +126 -0
- package/src/core/ledger/Transaction-Transcode.ts +56 -0
- package/src/core/ledger/index.ts +8 -0
- package/src/core/ledger/interfaces/ApplicationNameResponse.ts +5 -0
- package/src/core/ledger/interfaces/Device.ts +6 -0
- package/src/core/ledger/interfaces/DeviceResponse.ts +6 -0
- package/src/core/ledger/interfaces/Ledger.ts +10 -0
- package/src/core/ledger/interfaces/LedgerBalanceFromLedgerResponse.ts +9 -0
- package/src/core/ledger/interfaces/LedgerConfig.ts +17 -0
- package/src/core/ledger/interfaces/LedgerDeviceInfoResponse.ts +7 -0
- package/src/core/ledger/interfaces/LedgerSendTransactionResponse.ts +4 -0
- package/src/core/ledger/interfaces/LedgerSigner.ts +6 -0
- package/src/core/ledger/interfaces/LedgerSignerData.ts +8 -0
- package/src/core/ledger/interfaces/PublicKeyResponse.ts +5 -0
- package/src/core/ledger/interfaces/SignResponse.ts +5 -0
- package/src/core/ledger/interfaces/VersionResponse.ts +5 -0
- package/src/core/ledger/interfaces/index.ts +13 -0
- package/src/core/link/easyConnect.ts +214 -0
- package/src/core/link/easyScript.ts +67 -0
- package/src/core/link/index.ts +1 -0
- package/src/core/link/interfaces/IAccount.ts +13 -0
- package/src/core/link/interfaces/IFile.ts +6 -0
- package/src/core/link/interfaces/ProofOfWork.ts +8 -0
- package/src/core/link/interfaces/index.ts +3 -0
- package/src/core/link/phantasmaLink.ts +561 -0
- package/src/core/rpc/index.ts +2 -0
- package/src/core/rpc/interfaces/ABIContract.ts +6 -0
- package/src/core/rpc/interfaces/ABIEvent.ts +6 -0
- package/src/core/rpc/interfaces/ABIMethod.ts +7 -0
- package/src/core/rpc/interfaces/ABIParameter.ts +4 -0
- package/src/core/rpc/interfaces/Account.ts +16 -0
- package/src/core/rpc/interfaces/AccountTransactions.ts +6 -0
- package/src/core/rpc/interfaces/Archive.ts +10 -0
- package/src/core/rpc/interfaces/Auction.ts +12 -0
- package/src/core/rpc/interfaces/Balance.ts +7 -0
- package/src/core/rpc/interfaces/Block.ts +17 -0
- package/src/core/rpc/interfaces/Chain.ts +9 -0
- package/src/core/rpc/interfaces/Channel.ts +12 -0
- package/src/core/rpc/interfaces/Contract.ts +11 -0
- package/src/core/rpc/interfaces/Dapp.ts +5 -0
- package/src/core/rpc/interfaces/Event.ts +6 -0
- package/src/core/rpc/interfaces/Governance.ts +4 -0
- package/src/core/rpc/interfaces/Interop.ts +4 -0
- package/src/core/rpc/interfaces/KeyValue.ts +4 -0
- package/src/core/rpc/interfaces/Leaderboard.ts +6 -0
- package/src/core/rpc/interfaces/LeaderboardRow.ts +4 -0
- package/src/core/rpc/interfaces/NFT.ts +14 -0
- package/src/core/rpc/interfaces/Nexus.ts +14 -0
- package/src/core/rpc/interfaces/Oracle.ts +4 -0
- package/src/core/rpc/interfaces/Organization.ts +5 -0
- package/src/core/rpc/interfaces/Paginated.ts +7 -0
- package/src/core/rpc/interfaces/Peer.ts +7 -0
- package/src/core/rpc/interfaces/Platform.ts +9 -0
- package/src/core/rpc/interfaces/Receipt.ts +9 -0
- package/src/core/rpc/interfaces/Script.ts +9 -0
- package/src/core/rpc/interfaces/SendRawTx.ts +4 -0
- package/src/core/rpc/interfaces/SignatureResult.ts +4 -0
- package/src/core/rpc/interfaces/Stake.ts +5 -0
- package/src/core/rpc/interfaces/Storage.ts +8 -0
- package/src/core/rpc/interfaces/Swap.ts +12 -0
- package/src/core/rpc/interfaces/Token.ts +19 -0
- package/src/core/rpc/interfaces/TokenData.ts +12 -0
- package/src/core/rpc/interfaces/TokenExternal.ts +4 -0
- package/src/core/rpc/interfaces/TokenPrice.ts +7 -0
- package/src/core/rpc/interfaces/TokenSeries.ts +12 -0
- package/src/core/rpc/interfaces/TransactionData.ts +23 -0
- package/src/core/rpc/interfaces/Validator.ts +4 -0
- package/src/core/rpc/interfaces/index.ts +41 -0
- package/src/core/rpc/phantasma.ts +466 -0
- package/src/core/tx/Transaction.ts +258 -0
- package/src/core/tx/index.ts +2 -0
- package/src/core/tx/utils.ts +106 -0
- package/src/core/types/Address.ts +290 -0
- package/src/core/types/Consensus.ts +181 -0
- package/src/core/types/Contract.ts +324 -0
- package/src/core/types/DomainSettings.ts +128 -0
- package/src/core/types/Ed25519Signature.ts +78 -0
- package/src/core/types/Entropy.ts +16 -0
- package/src/core/types/Extensions/Base16.ts +36 -0
- package/src/core/types/Extensions/Describer.ts +17 -0
- package/src/core/types/Extensions/PBinaryReader.ts +247 -0
- package/src/core/types/Extensions/PBinaryWriter.ts +278 -0
- package/src/core/types/Extensions/index.ts +4 -0
- package/src/core/types/PhantasmaKeys.ts +119 -0
- package/src/core/types/Serialization.ts +211 -0
- package/src/core/types/Stack.ts +46 -0
- package/src/core/types/Timestamp.ts +93 -0
- package/src/core/types/index.ts +11 -0
- package/src/core/utils/index.ts +206 -0
- package/src/core/vm/Contracts.ts +17 -0
- package/src/core/vm/Decoder.ts +162 -0
- package/src/core/vm/Disassembler.ts +1 -0
- package/src/core/vm/EventData.ts +135 -0
- package/src/core/vm/Opcode.ts +74 -0
- package/src/core/vm/ScriptBuilder.ts +588 -0
- package/src/core/vm/VMObject.ts +1162 -0
- package/src/core/vm/VMType.ts +11 -0
- package/src/core/vm/index.ts +7 -0
- package/src/core/vm/utils/DisasmMethodCall.ts +22 -0
- package/src/core/vm/utils/DisasmUtils.ts +89 -0
- package/src/core/vm/utils/index.ts +2 -0
- package/src/index.ts +7 -0
- package/tests/VMObject.test.ts +146 -0
- package/tests/ledger/Address-Transacode.test.ts +26 -0
- package/tests/tx/Transaction.test.ts +274 -0
- package/tests/types/Address.test.ts +26 -0
- package/tests/types/Consensus.test.ts +67 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import pkg from 'elliptic';
|
|
2
|
+
const { eddsa } = pkg;
|
|
3
|
+
import { Decoder, ScriptBuilder } from '../vm';
|
|
4
|
+
import {
|
|
5
|
+
hexStringToBytes,
|
|
6
|
+
byteArrayToHex,
|
|
7
|
+
getDifficulty,
|
|
8
|
+
stringToUint8Array,
|
|
9
|
+
uint8ArrayToHex,
|
|
10
|
+
uint8ArrayToString,
|
|
11
|
+
uint8ArrayToStringDefault,
|
|
12
|
+
hexStringToUint8Array,
|
|
13
|
+
hexToByteArray,
|
|
14
|
+
} from '../utils';
|
|
15
|
+
import hexEncoding from 'crypto-js/enc-hex';
|
|
16
|
+
import SHA256 from 'crypto-js/sha256';
|
|
17
|
+
import { ISerializable, ISignature, Signature } from '../interfaces';
|
|
18
|
+
import { Base16, PBinaryReader, PBinaryWriter, PhantasmaKeys } from '../types';
|
|
19
|
+
import { getWifFromPrivateKey } from './utils';
|
|
20
|
+
const curve = new eddsa('ed25519');
|
|
21
|
+
|
|
22
|
+
export class Transaction implements ISerializable {
|
|
23
|
+
script: string; // Should be HexString
|
|
24
|
+
nexusName: string;
|
|
25
|
+
chainName: string;
|
|
26
|
+
payload: string; // Should be HexString
|
|
27
|
+
expiration: Date;
|
|
28
|
+
signatures: Array<Signature>;
|
|
29
|
+
hash: string;
|
|
30
|
+
|
|
31
|
+
public static FromBytes(serializedData: string): Transaction {
|
|
32
|
+
let transaction = new Transaction('', '', '', new Date(), '');
|
|
33
|
+
return transaction.unserialize(serializedData);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
constructor(
|
|
37
|
+
nexusName: string,
|
|
38
|
+
chainName: string,
|
|
39
|
+
script: string, // Should be HexString
|
|
40
|
+
expiration: Date,
|
|
41
|
+
payload: string // Should be HexString
|
|
42
|
+
) {
|
|
43
|
+
this.nexusName = nexusName;
|
|
44
|
+
this.chainName = chainName;
|
|
45
|
+
this.script = script;
|
|
46
|
+
this.expiration = expiration;
|
|
47
|
+
this.payload = payload == null || payload == '' ? '7068616e7461736d612d7473' : payload;
|
|
48
|
+
this.signatures = [];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public sign(wif: string) {
|
|
52
|
+
let _keys = PhantasmaKeys.fromWIF(wif);
|
|
53
|
+
const msg = this.ToByteAray(false);
|
|
54
|
+
let sig: Signature = _keys.Sign(msg);
|
|
55
|
+
let sigs: Signature[] = [];
|
|
56
|
+
if (this.signatures != null && this.signatures.length > 0) {
|
|
57
|
+
sigs = this.signatures;
|
|
58
|
+
}
|
|
59
|
+
sigs.push(sig);
|
|
60
|
+
|
|
61
|
+
this.signatures = sigs;
|
|
62
|
+
//const signature = this.getSign(this.toString(false), privateKey);
|
|
63
|
+
//this.signatures.unshift({ signature, kind: 1 });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public signWithPrivateKey(privateKey: string) {
|
|
67
|
+
const msg = this.ToByteAray(false);
|
|
68
|
+
let sig: Signature = PhantasmaKeys.fromWIF(getWifFromPrivateKey(privateKey)).Sign(msg);
|
|
69
|
+
let sigs: Signature[] = [];
|
|
70
|
+
if (this.signatures != null && this.signatures.length > 0) {
|
|
71
|
+
sigs = this.signatures;
|
|
72
|
+
}
|
|
73
|
+
sigs.push(sig);
|
|
74
|
+
|
|
75
|
+
this.signatures = sigs;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public signWithKeys(keys: PhantasmaKeys) {
|
|
79
|
+
const msg = this.ToByteAray(false);
|
|
80
|
+
let sig: Signature = keys.Sign(msg);
|
|
81
|
+
let sigs: Signature[] = [];
|
|
82
|
+
if (this.signatures != null && this.signatures.length > 0) {
|
|
83
|
+
sigs = this.signatures;
|
|
84
|
+
}
|
|
85
|
+
sigs.push(sig);
|
|
86
|
+
|
|
87
|
+
this.signatures = sigs;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public ToByteAray(withSignature: boolean): Uint8Array {
|
|
91
|
+
let writer = new PBinaryWriter();
|
|
92
|
+
writer.writeString(this.nexusName);
|
|
93
|
+
writer.writeString(this.chainName);
|
|
94
|
+
writer.AppendHexEncoded(this.script);
|
|
95
|
+
writer.writeDateTime(this.expiration);
|
|
96
|
+
writer.AppendHexEncoded(this.payload);
|
|
97
|
+
if (withSignature) {
|
|
98
|
+
writer.writeVarInt(this.signatures.length);
|
|
99
|
+
this.signatures.forEach((sig) => {
|
|
100
|
+
writer.writeSignature(sig);
|
|
101
|
+
//writer.writeByte(sig.kind);
|
|
102
|
+
//writer.writeByteArray(stringToUint8Array(sig.signature));
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return writer.toUint8Array();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public UnserializeData(reader: PBinaryReader) {
|
|
110
|
+
this.nexusName = reader.readString();
|
|
111
|
+
this.chainName = reader.readString();
|
|
112
|
+
this.script = uint8ArrayToStringDefault(reader.readByteArray());
|
|
113
|
+
let time = reader.readTimestamp();
|
|
114
|
+
this.expiration = new Date(time.toString());
|
|
115
|
+
this.payload = uint8ArrayToStringDefault(reader.readByteArray());
|
|
116
|
+
let sigCount = reader.readVarInt();
|
|
117
|
+
for (let i = 0; i < sigCount; i++) {
|
|
118
|
+
let sig = reader.readSignatureV2();
|
|
119
|
+
this.signatures.push(sig);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public SerializeData(writer: PBinaryWriter) {
|
|
124
|
+
writer.writeString(this.nexusName);
|
|
125
|
+
writer.writeString(this.chainName);
|
|
126
|
+
writer.writeByteArray(Base16.decodeUint8Array(this.script));
|
|
127
|
+
writer.writeDateTime(this.expiration);
|
|
128
|
+
writer.writeByteArray(Base16.decodeUint8Array(this.payload));
|
|
129
|
+
writer.writeVarInt(this.signatures.length);
|
|
130
|
+
this.signatures.forEach((sig) => {
|
|
131
|
+
writer.writeSignature(sig);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public toString(withSignature: boolean): string {
|
|
136
|
+
/*const utc = Date.UTC(
|
|
137
|
+
this.expiration.getUTCFullYear(),
|
|
138
|
+
this.expiration.getUTCMonth(),
|
|
139
|
+
this.expiration.getUTCDate(),
|
|
140
|
+
this.expiration.getUTCHours(),
|
|
141
|
+
this.expiration.getUTCMinutes(),
|
|
142
|
+
this.expiration.getUTCSeconds()
|
|
143
|
+
);
|
|
144
|
+
let num = utc / 1000;
|
|
145
|
+
|
|
146
|
+
let a = (num & 0xff000000) >> 24;
|
|
147
|
+
let b = (num & 0x00ff0000) >> 16;
|
|
148
|
+
let c = (num & 0x0000ff00) >> 8;
|
|
149
|
+
let d = num & 0x000000ff;
|
|
150
|
+
|
|
151
|
+
let expirationBytes = [d, c, b, a];*/
|
|
152
|
+
|
|
153
|
+
let sb = new ScriptBuilder()
|
|
154
|
+
.EmitVarString(this.nexusName)
|
|
155
|
+
.EmitVarString(this.chainName)
|
|
156
|
+
.EmitVarInt(this.script.length / 2)
|
|
157
|
+
.AppendHexEncoded(this.script)
|
|
158
|
+
.EmitTimestamp(this.expiration)
|
|
159
|
+
.EmitVarInt(this.payload.length / 2)
|
|
160
|
+
.AppendHexEncoded(this.payload);
|
|
161
|
+
|
|
162
|
+
if (withSignature) {
|
|
163
|
+
sb.EmitVarInt(this.signatures.length);
|
|
164
|
+
this.signatures.forEach((sig) => {
|
|
165
|
+
console.log('adding signature ', sig);
|
|
166
|
+
if (sig.Kind == 1) {
|
|
167
|
+
sb.AppendByte(1); // Signature Type
|
|
168
|
+
sb.EmitVarInt(sig.Bytes.length / 2);
|
|
169
|
+
sb.AppendHexEncoded(uint8ArrayToHex(sig.Bytes));
|
|
170
|
+
} else if (sig.Kind == 2) {
|
|
171
|
+
sb.AppendByte(2); // ECDSA Signature
|
|
172
|
+
sb.AppendByte(1); // Curve type secp256k1
|
|
173
|
+
sb.EmitVarInt(sig.Bytes.length / 2);
|
|
174
|
+
sb.AppendHexEncoded(uint8ArrayToHex(sig.Bytes));
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return sb.str;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
public ToStringEncoded(withSignature: boolean): string {
|
|
182
|
+
return Base16.encodeUint8Array(this.ToByteAray(withSignature));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
public getHash() {
|
|
186
|
+
let generatedHash = SHA256(hexEncoding.parse(this.toString(false)));
|
|
187
|
+
this.hash = byteArrayToHex(hexStringToBytes(generatedHash.toString(hexEncoding)).reverse());
|
|
188
|
+
return this.hash;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public mineTransaction(difficulty: number) {
|
|
192
|
+
if (difficulty < 0 || difficulty > 256) {
|
|
193
|
+
console.log('Error adding difficulty');
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let nonce = 0;
|
|
198
|
+
let deepCopy = new Transaction(
|
|
199
|
+
JSON.parse(JSON.stringify(this.nexusName)),
|
|
200
|
+
JSON.parse(JSON.stringify(this.chainName)),
|
|
201
|
+
JSON.parse(JSON.stringify(this.script)),
|
|
202
|
+
this.expiration,
|
|
203
|
+
JSON.parse(JSON.stringify(this.payload))
|
|
204
|
+
);
|
|
205
|
+
let payload = Buffer.alloc(4);
|
|
206
|
+
|
|
207
|
+
while (true) {
|
|
208
|
+
if (getDifficulty(deepCopy.getHash()) >= difficulty) {
|
|
209
|
+
this.payload = deepCopy.payload;
|
|
210
|
+
console.log('It took ' + nonce + ' iterations to get a difficulty of >' + difficulty);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
nonce++;
|
|
215
|
+
|
|
216
|
+
payload[0] = (nonce >> 0) & 0xff;
|
|
217
|
+
payload[1] = (nonce >> 8) & 0xff;
|
|
218
|
+
payload[2] = (nonce >> 16) & 0xff;
|
|
219
|
+
payload[3] = (nonce >> 24) & 0xff;
|
|
220
|
+
|
|
221
|
+
deepCopy.payload = byteArrayToHex(payload);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private getSign(msgHex: string, privateKey: string): string {
|
|
226
|
+
const msgHashHex = Buffer.from(msgHex, 'hex');
|
|
227
|
+
const privateKeyBuffer = Buffer.from(privateKey, 'hex');
|
|
228
|
+
|
|
229
|
+
const sig = curve.sign(msgHashHex, privateKeyBuffer);
|
|
230
|
+
|
|
231
|
+
return sig.toHex();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
public unserialize(serializedData: string): Transaction {
|
|
235
|
+
let dec = new Decoder(serializedData);
|
|
236
|
+
let nexusName = dec.readString();
|
|
237
|
+
let chainName = dec.readString();
|
|
238
|
+
let scriptLength = dec.readVarInt();
|
|
239
|
+
let script = dec.read(scriptLength);
|
|
240
|
+
let date = new Date(dec.readTimestamp() * 1000);
|
|
241
|
+
let payloadLength = dec.readVarInt();
|
|
242
|
+
let payload = dec.read(payloadLength);
|
|
243
|
+
|
|
244
|
+
let nTransaction = new Transaction(nexusName, chainName, script, date, payload);
|
|
245
|
+
let signatureCount = dec.readVarInt();
|
|
246
|
+
/*for (let i = 0; i < signatureCount; i++) {
|
|
247
|
+
nTransaction.signatures.push(dec.readSignature());
|
|
248
|
+
}*/
|
|
249
|
+
return nTransaction;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
public static Unserialize(serialized: Uint8Array) {
|
|
253
|
+
let reader = new PBinaryReader(serialized);
|
|
254
|
+
let tx = new Transaction('', '', '', new Date(), '');
|
|
255
|
+
tx.UnserializeData(reader);
|
|
256
|
+
return tx;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import WIF from 'wif';
|
|
2
|
+
import pkg from 'elliptic';
|
|
3
|
+
import base58 from 'bs58';
|
|
4
|
+
import * as bip39 from 'bip39';
|
|
5
|
+
import { hdkey } from 'ethereumjs-wallet';
|
|
6
|
+
import crypto from 'crypto';
|
|
7
|
+
const { eddsa } = pkg;
|
|
8
|
+
const curve = new eddsa('ed25519');
|
|
9
|
+
|
|
10
|
+
function ab2hexstring(arr: ArrayBuffer | ArrayLike<number>): string {
|
|
11
|
+
if (typeof arr !== 'object') {
|
|
12
|
+
throw new Error(`ab2hexstring expects an array.Input was ${arr}`);
|
|
13
|
+
}
|
|
14
|
+
let result = '';
|
|
15
|
+
const intArray = new Uint8Array(arr);
|
|
16
|
+
for (const i of intArray) {
|
|
17
|
+
let str = i.toString(16);
|
|
18
|
+
str = str.length === 0 ? '00' : str.length === 1 ? '0' + str : str;
|
|
19
|
+
result += str;
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getPrivateKeyFromWif(wif: string): string {
|
|
25
|
+
return ab2hexstring(WIF.decode(wif, 128).privateKey);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getAddressFromWif(wif: string): string {
|
|
29
|
+
const curve = new eddsa('ed25519');
|
|
30
|
+
|
|
31
|
+
const privateKey = getPrivateKeyFromWif(wif);
|
|
32
|
+
const privateKeyBuffer = Buffer.from(privateKey, 'hex');
|
|
33
|
+
const publicKey = curve.keyFromSecret(privateKeyBuffer).getPublic('hex');
|
|
34
|
+
var addressHex = Buffer.from('0100' + publicKey, 'hex');
|
|
35
|
+
|
|
36
|
+
return 'P' + base58.encode(addressHex);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function getPublicKeyFromPrivateKey(privateKey: string): string {
|
|
40
|
+
const privateKeyBuffer = Buffer.from(privateKey, 'hex');
|
|
41
|
+
const publicKey = curve.keyFromSecret(privateKeyBuffer).getPublic('hex');
|
|
42
|
+
return publicKey;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function generateNewSeed(): string {
|
|
46
|
+
let buffer = new Uint8Array(32);
|
|
47
|
+
let privateKey = Buffer.alloc(32);
|
|
48
|
+
crypto.getRandomValues(buffer);
|
|
49
|
+
for (let i = 0; i < 32; ++i) {
|
|
50
|
+
privateKey.writeUInt8(buffer[i], i);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const wif = WIF.encode(128, privateKey, true);
|
|
54
|
+
const mnemonic = bip39.generateMnemonic();
|
|
55
|
+
return mnemonic;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function generateNewSeedWords(): string[] {
|
|
59
|
+
let buffer = new Uint8Array(32);
|
|
60
|
+
let privateKey = Buffer.alloc(32);
|
|
61
|
+
crypto.getRandomValues(buffer);
|
|
62
|
+
for (let i = 0; i < 32; ++i) {
|
|
63
|
+
privateKey.writeUInt8(buffer[i], i);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const wif = WIF.encode(128, privateKey, true);
|
|
67
|
+
const mnemonic = bip39.generateMnemonic();
|
|
68
|
+
const seedWords = mnemonic.split(' ');
|
|
69
|
+
return seedWords;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function generateNewWif(): string {
|
|
73
|
+
let buffer = new Uint8Array(32);
|
|
74
|
+
let privateKey = Buffer.alloc(32);
|
|
75
|
+
crypto.getRandomValues(buffer);
|
|
76
|
+
for (let i = 0; i < 32; ++i) {
|
|
77
|
+
privateKey.writeUInt8(buffer[i], i);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const wif = WIF.encode(128, privateKey, true);
|
|
81
|
+
return wif;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function getWifFromPrivateKey(privateKey: string): string {
|
|
85
|
+
const privateKeyBuffer = Buffer.from(privateKey, 'hex');
|
|
86
|
+
const wif = WIF.encode(128, privateKeyBuffer, true);
|
|
87
|
+
return wif;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function signData(msgHex: string, privateKey: string): string {
|
|
91
|
+
const msgHashHex = Buffer.from(msgHex, 'hex');
|
|
92
|
+
const privateKeyBuffer = Buffer.from(privateKey, 'hex');
|
|
93
|
+
|
|
94
|
+
const sig = curve.sign(msgHashHex, privateKeyBuffer);
|
|
95
|
+
const numBytes = sig.toBytes().length;
|
|
96
|
+
|
|
97
|
+
return '01' + (numBytes < 16 ? '0' : '') + numBytes.toString(16) + sig.toHex();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function verifyData(msgHex: string, phaSig: string, address: string): boolean {
|
|
101
|
+
const msgBytes = Buffer.from(msgHex, 'hex');
|
|
102
|
+
const realSig = phaSig.substring(4);
|
|
103
|
+
const pubKey = base58.decode(address.substring(1)).slice(2);
|
|
104
|
+
|
|
105
|
+
return curve.verify(msgBytes, realSig, ab2hexstring(pubKey));
|
|
106
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import base58 from 'bs58';
|
|
2
|
+
import { encodeBase16, stringToUint8Array, uint8ArrayToString } from '../utils';
|
|
3
|
+
import SHA256 from 'crypto-js/sha256';
|
|
4
|
+
import hexEncoding from 'crypto-js/enc-hex';
|
|
5
|
+
import { IKeyPair, ISerializable } from '../interfaces';
|
|
6
|
+
import { getPrivateKeyFromWif, getPublicKeyFromPrivateKey, getWifFromPrivateKey } from '../tx';
|
|
7
|
+
import pkg from 'elliptic';
|
|
8
|
+
import { PBinaryWriter, PBinaryReader } from './Extensions';
|
|
9
|
+
const { eddsa } = pkg;
|
|
10
|
+
const curve = new eddsa('ed25519');
|
|
11
|
+
|
|
12
|
+
export enum AddressKind {
|
|
13
|
+
Invalid = 0,
|
|
14
|
+
User = 1,
|
|
15
|
+
System = 2,
|
|
16
|
+
Interop = 3,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class Address implements ISerializable {
|
|
20
|
+
public static readonly NullText: string = 'NULL';
|
|
21
|
+
public static readonly LengthInBytes: number = 34;
|
|
22
|
+
public static readonly MaxPlatformNameLength: number = 10;
|
|
23
|
+
|
|
24
|
+
private static NullPublicKey = new Uint8Array(Address.LengthInBytes);
|
|
25
|
+
public static readonly Null: Address = new Address(Address.NullPublicKey);
|
|
26
|
+
|
|
27
|
+
private _bytes: Uint8Array;
|
|
28
|
+
|
|
29
|
+
/*public get Kind(): AddressKind {
|
|
30
|
+
return this.IsNull ? AddressKind.System : (this._bytes[0] >= 3) ? AddressKind.Interop
|
|
31
|
+
: (AddressKind)this._bytes[0];
|
|
32
|
+
}*/
|
|
33
|
+
|
|
34
|
+
public get Kind(): AddressKind {
|
|
35
|
+
if (this.IsNull) {
|
|
36
|
+
return AddressKind.System;
|
|
37
|
+
} else if (this._bytes[0] >= 3) {
|
|
38
|
+
return AddressKind.Interop;
|
|
39
|
+
} else {
|
|
40
|
+
return this._bytes[0] as AddressKind;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public get IsSystem(): boolean {
|
|
45
|
+
return this.Kind == AddressKind.System;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public get IsInterop(): boolean {
|
|
49
|
+
return this.Kind == AddressKind.Interop;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public get IsUser(): boolean {
|
|
53
|
+
return this.Kind == AddressKind.User;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*public get TendermintAddress(): string {
|
|
57
|
+
return encodeBase16(this._bytes.slice(2).SHA256().slice(0, 20));
|
|
58
|
+
}
|
|
59
|
+
public get TendermintAddress() {
|
|
60
|
+
return SHA256(this._bytes.slice(2)).slice(0, 20).toString('hex');
|
|
61
|
+
}
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
public get IsNull(): boolean {
|
|
65
|
+
if (this._bytes == null || this._bytes.length == 0) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (let i = 1; i < this._bytes.length; i++) {
|
|
70
|
+
if (this._bytes[i] != 0) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private _text: string;
|
|
79
|
+
|
|
80
|
+
private static _keyToTextCache = new Map<Uint8Array, string>();
|
|
81
|
+
|
|
82
|
+
public get Text(): string {
|
|
83
|
+
if (this.IsNull) {
|
|
84
|
+
return Address.NullText;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!this._text) {
|
|
88
|
+
if (Address._keyToTextCache.has(this._bytes)) {
|
|
89
|
+
this._text = Address._keyToTextCache.get(this._bytes);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (!this._text) {
|
|
93
|
+
let prefix: string;
|
|
94
|
+
|
|
95
|
+
switch (this.Kind) {
|
|
96
|
+
case AddressKind.User:
|
|
97
|
+
prefix = 'P';
|
|
98
|
+
break;
|
|
99
|
+
case AddressKind.Interop:
|
|
100
|
+
prefix = 'X';
|
|
101
|
+
break;
|
|
102
|
+
default:
|
|
103
|
+
prefix = 'S';
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
this._text = prefix + base58.encode(this._bytes);
|
|
107
|
+
Address._keyToTextCache.set(this._bytes, this._text);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return this._text;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private constructor(publicKey: Uint8Array) {
|
|
115
|
+
let pkFromArray = Array.from(publicKey);
|
|
116
|
+
if (publicKey.length != Address.LengthInBytes) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`publicKey length must be ${Address.LengthInBytes}, it was ${publicKey.length}}`
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
this._bytes = new Uint8Array(Address.LengthInBytes);
|
|
122
|
+
this._bytes.set(publicKey);
|
|
123
|
+
this._text = null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public static FromPublickKey(publicKey: Uint8Array): Address {
|
|
127
|
+
publicKey = publicKey.slice(0, 34);
|
|
128
|
+
return new Address(publicKey);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public static FromText(text: string): Address {
|
|
132
|
+
return Address.Parse(text);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public static Parse(text: string): Address {
|
|
136
|
+
if (text == null) {
|
|
137
|
+
return Address.Null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (text == Address.NullText) {
|
|
141
|
+
return Address.Null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let addr: Address;
|
|
145
|
+
|
|
146
|
+
let originalText = text;
|
|
147
|
+
let prefix = text[0];
|
|
148
|
+
|
|
149
|
+
text = text.slice(1);
|
|
150
|
+
var bytes = base58.decode(text);
|
|
151
|
+
|
|
152
|
+
addr = new Address(bytes);
|
|
153
|
+
|
|
154
|
+
switch (prefix) {
|
|
155
|
+
case 'P':
|
|
156
|
+
if (addr.Kind != AddressKind.User) {
|
|
157
|
+
throw new Error(`Invalid address prefix. Expected 'P', got '${prefix}'`);
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
case 'S':
|
|
161
|
+
if (addr.Kind != AddressKind.System) {
|
|
162
|
+
throw new Error(`Invalid address prefix. Expected 'S', got '${prefix}'`);
|
|
163
|
+
}
|
|
164
|
+
break;
|
|
165
|
+
case 'X':
|
|
166
|
+
if (addr.Kind < AddressKind.Interop) {
|
|
167
|
+
throw new Error(`Invalid address prefix. Expected 'X', got '${prefix}'`);
|
|
168
|
+
}
|
|
169
|
+
break;
|
|
170
|
+
default:
|
|
171
|
+
throw new Error(`Invalid address prefix. Expected 'P', 'S' or 'X', got '${prefix}'`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/*this._keyToTextCache.values().forEach((value) => {
|
|
175
|
+
if (value == text) {
|
|
176
|
+
return Address.FromHash(this._keyToTextCache(value));
|
|
177
|
+
}*/
|
|
178
|
+
|
|
179
|
+
return addr;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public static IsValidAddress(text: string): boolean {
|
|
183
|
+
try {
|
|
184
|
+
Address.FromText(text);
|
|
185
|
+
return true;
|
|
186
|
+
} catch (e) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public static FromBytes(bytes: Uint8Array): Address {
|
|
192
|
+
return new Address(bytes);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
public static FromKey(key: IKeyPair): Address {
|
|
196
|
+
const bytes = new Uint8Array(Address.LengthInBytes);
|
|
197
|
+
bytes[0] = AddressKind.User;
|
|
198
|
+
|
|
199
|
+
if (key.PublicKey.length == 32) {
|
|
200
|
+
bytes.set(key.PublicKey, 2);
|
|
201
|
+
} else if (key.PublicKey.length == 33) {
|
|
202
|
+
bytes.set(key.PublicKey, 1);
|
|
203
|
+
} else if (key.PublicKey.length == 64) {
|
|
204
|
+
bytes.set(key.PublicKey.slice(0, 32), 1);
|
|
205
|
+
} else {
|
|
206
|
+
throw new Error('Invalid public key length: ' + key.PublicKey.length);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return new Address(bytes);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
public static FromHash(str: string): Address;
|
|
213
|
+
public static FromHash(input: Uint8Array): Address;
|
|
214
|
+
public static FromHash(input: any): Address {
|
|
215
|
+
let bytes: Uint8Array;
|
|
216
|
+
if (typeof input === 'string') {
|
|
217
|
+
bytes = new TextEncoder().encode(input);
|
|
218
|
+
} else {
|
|
219
|
+
bytes = input;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const hash = SHA256(hexEncoding.parse(uint8ArrayToString(bytes)));
|
|
223
|
+
bytes = new Uint8Array(Address.LengthInBytes);
|
|
224
|
+
bytes[0] = AddressKind.User;
|
|
225
|
+
bytes.set(hash.words.slice(0, 32), 2);
|
|
226
|
+
return new Address(bytes);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
public static FromWif(wif: string): Address {
|
|
230
|
+
const privateKey = getPrivateKeyFromWif(wif);
|
|
231
|
+
const publicKey = getPublicKeyFromPrivateKey(privateKey);
|
|
232
|
+
var addressHex = Buffer.from('0100' + publicKey, 'hex');
|
|
233
|
+
return this.FromBytes(addressHex);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
public compareTo(other: Address): number {
|
|
237
|
+
for (let i = 0; i < Address.LengthInBytes; i++) {
|
|
238
|
+
if (this._bytes[i] < other._bytes[i]) {
|
|
239
|
+
return -1;
|
|
240
|
+
} else if (this._bytes[i] > other._bytes[i]) {
|
|
241
|
+
return 1;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return 0;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
public equals(other: any): boolean {
|
|
248
|
+
if (!(other instanceof Address)) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
const address = other as Address;
|
|
252
|
+
return this._bytes.toString() === address._bytes.toString();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
public toString(): string {
|
|
256
|
+
if (this.IsNull) {
|
|
257
|
+
return Address.NullText;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (!this._text) {
|
|
261
|
+
let prefix: string;
|
|
262
|
+
switch (this.Kind) {
|
|
263
|
+
case AddressKind.User:
|
|
264
|
+
prefix = 'P';
|
|
265
|
+
break;
|
|
266
|
+
case AddressKind.Interop:
|
|
267
|
+
prefix = 'X';
|
|
268
|
+
break;
|
|
269
|
+
default:
|
|
270
|
+
prefix = 'S';
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
this._text = prefix + base58.encode(this._bytes);
|
|
274
|
+
}
|
|
275
|
+
return this._text;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public ToByteArray(): Uint8Array {
|
|
279
|
+
return this._bytes;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
SerializeData(writer: PBinaryWriter) {
|
|
283
|
+
writer.writeByteArray(this._bytes);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
UnserializeData(reader: PBinaryReader) {
|
|
287
|
+
this._bytes = reader.readByteArray();
|
|
288
|
+
this._text = null;
|
|
289
|
+
}
|
|
290
|
+
}
|