An ultra-lightweight JavaScript library for Bitcoin Cash, Bitcoin, and Bitauth applications.
An ultra-lightweight JavaScript library for Bitcoin Cash, Bitcoin, and Bitauth applications.
Libauth has no dependencies and works in all JavaScript environments, including Node.js, Deno, and browsers.
Libauth is designed to be flexible, lightweight, and easily auditable. Rather than providing a single, overarching, object-oriented API, all functionality is composed from simple functions. This has several benefits:
- Flexibility – Even highly-complex functionality is built-up from simpler functions. These lower-level functions can be used to experiment, tweak, and remix your own higher-level methods without maintaining a fork of the library.
- Smaller application bundles – Applications can import only the methods they need, eliminating the unused code (via dead-code elimination).
- Better auditability – Beyond having no dependencies of its own, Libauth's functional programming approach makes auditing critical code easier: smaller bundles, smaller functions, and less churn between versions (fewer cascading changes to object-oriented interfaces).
- Fully-portable – No platform-specific APIs are ever used, so the same code paths are used across all JavaScript environments (reducing the auditable "surface area" and simplifying library development).
To get started, install @bitauth/libauth:
npm install @bitauth/libauth
# OR
yarn add @bitauth/libauthAnd import the functionality you need:
import { secp256k1 } from '@bitauth/libauth';
import { msgHash, pubkey, sig } from 'somewhere';
secp256k1.verifySignatureDERLowS(sig, pubkey, msgHash)
? console.log('🚀 Signature valid')
: console.log('❌ Signature invalid');See Installation for more guidance on getting set up.
These guides introduce some of the high-level concepts and functionality provided by Libauth.
- Installation
- Handling Errors
- Cryptography
- Keys
- Addresses
- Verifying Transactions
- Wallets & Transaction Creation
In addition to the usage examples in these guides, note that Libauth includes comprehensive tests that can help demonstrate usage of all functionality.
For example, utilities related to hexadecimal-encoded strings are defined in hex.ts; for thorough usage examples, see the co-located hex.spec.ts. You can also use GitHub search to see how a particular utility is used throughout the library, e.g. splitEvery.
Below is a partial selection of functionality provided by Libauth. If you're looking for something else, be sure to search the API Reference.
High-level utilities are composed from lower-level utilities which are also exported, so it's often possible to remix behavior in your own codebase with relatively little duplication or maintenance burden. See the Defined in ... link on each utility's API reference page to review and copy the implementation.
Table of Contents
encodeBase58Address/decodeBase58AddressencodeBase58AddressFormat/decodeBase58AddressFormatlockingBytecodeToBase58Address/base58AddressToLockingBytecode
encodeCashAddress/decodeCashAddresslockingBytecodeToCashAddress/cashAddressToLockingBytecodedecodeCashAddressFormatWithoutPrefixencodeCashAddressVersionByte/decodeCashAddressVersionByte
attemptCashAddressFormatErrorCorrectionencodeCashAddressFormat/decodeCashAddressFormatencodeCashAddressNonStandard/decodeCashAddressNonStandard
hash160(sha256->ripemd160)hash256(sha256->sha256)hmacSha256hmacSha512instantiateHmacFunctioninstantiatePbkdf2Functionpbkdf2HmacSha256pbkdf2HmacSha512ripemd160secp256k1sha1sha256sha512
bigIntToBinUint256BEClamped/binToBigIntUint256BEbigIntToBinUint64LE(bigIntToBinUint64LEClamped)/binToBigIntUint64LEbigIntToBinUintLE/binToBigIntUintLEbinToBigIntUintBE/bigIntToBinUintBEint32SignedToUnsigned/int32UnsignedToSignednumberToBinInt16LE/binToNumberInt16LEnumberToBinInt32LE/binToNumberInt32LEnumberToBinInt32TwosComplimentnumberToBinUintLE/binToNumberUintLEnumberToBinUint16BEnumberToBinUint16LE(numberToBinUint16LEClamped)/binToNumberUint16LEnumberToBinUint32BEnumberToBinUint32LE(numberToBinUint32LEClamped)/binToNumberUint32LE
compactUintToBigInt/bigIntToCompactUintcompactUintPrefixToSizereadCompactUintreadCompactUintMinimal
crackHdPrivateNodeFromHdPublicNodeAndChildPrivateNodedecodeHdKey(decodeHdKeyUnchecked)decodeHdPrivateKey/encodeHdPrivateKeydecodeHdPublicKey/encodeHdPrivateKeyderiveHdPathderiveHdPathRelativederiveHdPrivateNodeFromSeedderiveHdPrivateNodeIdentifier/deriveHdPublicNodeIdentifierderiveHdPrivateNodeChild/deriveHdPublicNodeChildderiveHdPublicKeyderiveHdPublicNodehdKeyVersionIsPrivateKey/hdKeyVersionIsPublicKeyhdPrivateKeyToIdentifier/hdPublicKeyToIdentifier
deriveHdPrivateNodeFromBip39MnemonicderiveSeedFromBip39MnemonicencodeBip39Mnemonic/decodeBip39MnemonicgenerateBip39Mnemonic
generateDeterministicEntropygenerateHdPrivateNodegeneratePrivateKeygenerateRandomBytesgenerateRandomSeedminimumEventsPerEntropyBitsshannonEntropyPerEventvalidateSecp256k1PrivateKey
readBytesreadCompactUintPrefixedBinreadRemainingBytesreadUint32LEreadUint64LEreadMultiplereadItemCount
encodeTransaction/decodeTransaction(decodeTransactionUnsafe)encodeTransactionOutputs/decodeTransactionOutputshashTransactionhashTransactionP2pOrderhashTransactionUiOrder
isArbitraryDataOutputisSimpleMultisigisStandardOutputBytecodeisStandardOutputBytecode2023isStandardMultisigisWitnessProgram
createInstructionSetBCHcreateInstructionSetBCH2022createInstructionSetBCH2023createInstructionSetBCHCHIPscreateInstructionSetXECcreateVirtualMachinecreateVirtualMachineBCHcreateVirtualMachineBCH2022createVirtualMachineBCH2023createVirtualMachineBCHCHIPscreateVirtualMachineXEC
assembleBytecodeassembleBytecodeBCHassembleBytecodeBTCdisassembleBytecodedisassembleBytecodeBCHdisassembleBytecodeBTCgenerateBytecodeMapsummarizeDebugTracestringifyDebugTraceSummarysummarizeStack
combineOperationsmapOverOperationspushToStackpushToStackCheckedpushToStackVmNumberpushToStackVmNumberCheckeduseOneStackItemuseTwoStackItemsuseThreeStackItemsuseFourStackItemsuseSixStackItemsuseOneVmNumberuseTwoVmNumbersuseThreeVmNumbers
allErrorsAreRecoverablecashAssemblyToBincompileScriptcontainsRangecreateCompilerextractBytecodeResolutionsextractEvaluationSamplesextractEvaluationSamplesRecursiveextractUnexecutedRangesgetResolutionErrorsmergeRangesparseScriptresolveVariableIdentifierstringifyErrorsverifyCashAssemblyEvaluationStatewalletTemplateToCompilerConfigurationwalletTemplateToCompilerBCH
hdPrivateKeyToP2pkhLockingBytecodehdPrivateKeyToP2pkhCashAddresshdPublicKeyToP2pkhLockingBytecodehdPublicKeyToP2pkhCashAddressprivateKeyToP2pkhLockingBytecodeprivateKeyToP2pkhCashAddresspublicKeyToP2pkhLockingBytecodepublicKeyToP2pkhCashAddress
Libauth's test suite includes a set of cross-implementation Virtual Machine Bytecode (VMB) test vectors for each supported VM. See Libauth VMB Tests for details.
CashAssembly is the assembly language used by Libauth's Wallet Templates. To learn more about CashAssembly, read the Bitauth IDE Guide.
Pull Requests welcome! Please see CONTRIBUTING.md for details.