commit 6b1ad46c9a23f149231814ade9df08de91758b42 Author: Rene Vergara Date: Thu Apr 13 18:35:15 2023 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c368d45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.stack-work/ +*~ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e88dc8c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,13 @@ +# Changelog for zcash-haskell + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Added + +- Function `f4Jumble` +- Function `isValidUnifiedAddress` diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4eb1836 --- /dev/null +++ b/LICENSE @@ -0,0 +1,178 @@ +Copyright (c) 2023 Vergara Technologies LLC + +======================================================= +Bootstrap Open Source Licence ("BOSL") v. 1.0 +======================================================= +This Bootstrap Open Source Licence (the "License") applies to any original work +of authorship (the "Original Work") whose owner (the "Licensor") has placed the +following licensing notice adjacent to the copyright notice for the Original +Work: + +*Licensed under the Bootstrap Open Source Licence version 1.0* + +1. **Grant of Copyright License.** Licensor grants You a worldwide, + royalty-free, non-exclusive, sublicensable license, for the duration of the + copyright in the Original Work, to do the following: + + a. to reproduce the Original Work in copies, either alone or as part of + a collective work; + + b. to translate, adapt, alter, transform, modify, or arrange the + Original Work, thereby creating derivative works ("Derivative Works") + based upon the Original Work; + + c. to distribute or communicate copies of the Original Work and + Derivative Works to the public, provided that prior to any such + distribution or communication You first place a machine-readable copy + of the Source Code of the Original Work and such Derivative Works that + You intend to distribute or communicate in an information repository + reasonably calculated to permit inexpensive and convenient access + thereto by the public (“Information Repository”) for as long as You + continue to distribute or communicate said copies, accompanied by an + irrevocable offer to license said copies to the public free of charge + under this License, said offer valid starting no later than 12 months + after You first distribute or communicate said copies; + + d. to perform the Original Work publicly; and + + e. to display the Original Work publicly. + +2. **Grant of Patent License.** Licensor grants You a worldwide, royalty-free, +non-exclusive, sublicensable license, under patent claims owned or controlled +by the Licensor that are embodied in the Original Work as furnished by the +Licensor, for the duration of the patents, to make, use, sell, offer for sale, +have made, and import the Original Work and Derivative Works. + +3. **Grant of Source Code License.** The "Source Code" for a work means the +preferred form of the work for making modifications to it and all available +documentation describing how to modify the work. Licensor agrees to provide a +machine-readable copy of the Source Code of the Original Work along with each +copy of the Original Work that Licensor distributes. Licensor reserves the +right to satisfy this obligation by placing a machine-readable copy of said +Source Code in an Information Repository for as long as Licensor continues to +distribute the Original Work. + +4. **Exclusions From License Grant.** Neither the names of Licensor, nor the +names of any contributors to the Original Work, nor any of their trademarks or +service marks, may be used to endorse or promote products derived from this +Original Work without express prior permission of the Licensor. Except as +expressly stated herein, nothing in this License grants any license to +Licensor's trademarks, copyrights, patents, trade secrets or any other +intellectual property. No patent license is granted to make, use, sell, offer +for sale, have made, or import embodiments of any patent claims other than the +licensed claims defined in Section 2. No license is granted to the trademarks +of Licensor even if such marks are included in the Original Work. Nothing in +this License shall be interpreted to prohibit Licensor from licensing under +terms different from this License any Original Work that Licensor otherwise +would have a right to license. + +5. **External Deployment.** The term "External Deployment" means the use, +distribution, or communication of the Original Work or Derivative Works in any +way such that the Original Work or Derivative Works may be used by anyone other +than You, whether those works are distributed or communicated to those persons +or made available as an application intended for use over a network. As an +express condition for the grants of license hereunder, You must treat any +External Deployment by You of the Original Work or a Derivative Work as a +distribution under section 1(c). + +6. **Attribution Rights.** You must retain, in the Source Code of any +Derivative Works that You create, all copyright, patent, or trademark notices +from the Source Code of the Original Work, as well as any notices of licensing +and any descriptive text identified therein as an "Attribution Notice." You +must cause the Source Code for any Derivative Works that You create to carry a +prominent Attribution Notice reasonably calculated to inform recipients that +You have modified the Original Work. + +7. **Warranty of Provenance and Disclaimer of Warranty.** Licensor warrants +that the copyright in and to the Original Work and the patent rights granted +herein by Licensor are owned by the Licensor or are sublicensed to You under +the terms of this License with the permission of the contributor(s) of those +copyrights and patent rights. Except as expressly stated in the immediately +preceding sentence, the Original Work is provided under this License on an "AS +IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without +limitation, the warranties of non-infringement, merchantability or fitness for +a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS +WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this +License. No license to the Original Work is granted by this License except +under this disclaimer. + +8. **Limitation of Liability.** Under no circumstances and under no legal +theory, whether in tort (including negligence), contract, or otherwise, shall +the Licensor be liable to anyone for any indirect, special, incidental, or +consequential damages of any character arising as a result of this License or +the use of the Original Work including, without limitation, damages for loss of +goodwill, work stoppage, computer failure or malfunction, or any and all other +commercial damages or losses. This limitation of liability shall not apply to +the extent applicable law prohibits such limitation. + +9. **Acceptance and Termination.** If, at any time, You expressly assented to +this License, that assent indicates your clear and irrevocable acceptance of +this License and all of its terms and conditions. If You distribute or +communicate copies of the Original Work or a Derivative Work, You must make a +reasonable effort under the circumstances to obtain the express assent of +recipients to the terms of this License. This License conditions your rights to +undertake the activities listed in Section 1, including your right to create +Derivative Works based upon the Original Work, and doing so without honoring +these terms and conditions is prohibited by copyright law and international +treaty. Nothing in this License is intended to affect copyright exceptions and +limitations (including 'fair use' or 'fair dealing'). This License shall +terminate immediately and You may no longer exercise any of the rights granted +to You by this License upon your failure to honor the conditions in Section +1(c). + +10. **Termination for Patent Action.** This License shall terminate +automatically and You may no longer exercise any of the rights granted to You +by this License as of the date You commence an action, including a cross-claim +or counterclaim, against Licensor or any licensee alleging that the Original +Work infringes a patent. This termination provision shall not apply for an +action alleging patent infringement by combinations of the Original Work with +other software or hardware. + +11. **Jurisdiction, Venue and Governing Law.** Any action or suit relating to +this License may be brought only in the courts of a jurisdiction wherein the +Licensor resides or in which Licensor conducts its primary business, and under +the laws of that jurisdiction excluding its conflict-of-law provisions. The +application of the United Nations Convention on Contracts for the International +Sale of Goods is expressly excluded. Any use of the Original Work outside the +scope of this License or after its termination shall be subject to the +requirements and penalties of copyright or patent law in the appropriate +jurisdiction. This section shall survive the termination of this License. + +12. **Attorneys' Fees.** In any action to enforce the terms of this License or +seeking damages relating thereto, the prevailing party shall be entitled to +recover its costs and expenses, including, without limitation, reasonable +attorneys' fees and costs incurred in connection with such action, including +any appeal of such action. This section shall survive the termination of this +License. + +13. **Miscellaneous.** If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent necessary to +make it enforceable. + +14. **Definition of "You" in This License.** "You" throughout this License, +whether in upper or lower case, means an individual or a legal entity +exercising rights under, and complying with all of the terms of, this License. +For legal entities, "You" includes any entity that controls, is controlled by, +or is under common control with you. For purposes of this definition, "control" +means (i) the power, direct or indirect, to cause the direction or management +of such entity, whether by contract or otherwise, or (ii) ownership of fifty +percent (50%) or more of the outstanding shares, or (iii) beneficial ownership +of such entity. + +15. **Right to Use.** You may use the Original Work in all ways not otherwise +restricted or conditioned by this License or by law, and Licensor promises not +to interfere with or be responsible for such uses by You. + +16. **Modification of This License.** This License is Copyright © 2007 Zooko +Wilcox-O'Hearn. Permission is granted to copy, distribute, or communicate this +License without modification. Nothing in this License permits You to modify +this License as applied to the Original Work or to Derivative Works. However, +You may modify the text of this License and copy, distribute or communicate +your modified version (the "Modified License") and apply it to other original +works of authorship subject to the following conditions: (i) You may not +indicate in any way that your Modified License is the "Bootstrap Open Source +Licence" or "BOSL" and you may not use those names in the name of your Modified +License; and (ii) You must replace the notice specified in the first paragraph +above with the notice "Licensed under " or with +a notice of your own that is not confusingly similar to the notice in this +License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ed72755 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# haskell-wrapper diff --git a/app/Main.hs b/app/Main.hs new file mode 100644 index 0000000..56a7df4 --- /dev/null +++ b/app/Main.hs @@ -0,0 +1,6 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Main where + +main :: IO () +main = putStrLn "Zcash Utilities for Haskell" diff --git a/package.yaml b/package.yaml new file mode 100644 index 0000000..3182baf --- /dev/null +++ b/package.yaml @@ -0,0 +1,46 @@ +name: zcash-haskell +version: 0.1.0 +git: "https://git.vergara.tech/Vergara_Tech/zcash-haskell" +license: BOSL +author: "Rene Vergara" +maintainer: "rene@vergara.network" +copyright: "(c)2023 Vergara Technologies LLC" + +extra-source-files: +- README.md +- CHANGELOG.md + +# Metadata used when publishing your package +synopsis: Utilities to interact with the Zcash blockchain +category: Blockchain + +# To avoid duplicated efforts in documentation and dealing with the +# complications of embedding Haddock markup inside cabal files, it is +# common to point users to the README.md file. +description: Please see the README on the repo at + +dependencies: +- base >= 4.7 && < 5 + +library: + source-dirs: src + dependencies: + - bytestring + - borsh + - text + - foreign-rust + pkg-config-dependencies: + - rust_wrapper-uninstalled + +tests: + zcash-haskell-test: + main: Spec.hs + source-dirs: test + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N + dependencies: + - zcash-haskell + - hspec + - bytestring diff --git a/src/C/GettingStarted.chs b/src/C/GettingStarted.chs new file mode 100644 index 0000000..36e4c4e --- /dev/null +++ b/src/C/GettingStarted.chs @@ -0,0 +1,12 @@ +module C.GettingStarted (rustWrapperAdd) where + +#include "rust_wrapper.h" + +import Data.Word + +{# fun pure unsafe rust_wrapper_add as rustWrapperAdd + { `Word64' + , `Word64' + } + -> `Word64' +#} diff --git a/src/C/Zcash.chs b/src/C/Zcash.chs new file mode 100644 index 0000000..05a5d2a --- /dev/null +++ b/src/C/Zcash.chs @@ -0,0 +1,39 @@ +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE DerivingVia #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE UndecidableInstances #-} + +module C.Zcash where + +#include "rust_wrapper.h" + +import qualified Data.ByteString as BS +import Codec.Borsh +import Data.Text (Text) +import Data.Word +import Data.Structured +import Foreign.C.Types +import Foreign.Rust.Marshall.External +import Foreign.Rust.Marshall.Fixed +import Foreign.Rust.Marshall.Variable +import Foreign.Rust.Serialisation.Raw +import Foreign.Rust.Serialisation.Raw.Base16 + +newtype CodedString = CodedString BS.ByteString + deriving (Eq) + deriving newtype (BorshSize, ToBorsh, FromBorsh) + deriving newtype (IsRaw) + deriving (Prelude.Show, Data.Structured.Show) via AsBase16 CodedString + +{# fun unsafe rust_wrapper_f4jumble as rustWrapperF4Jumble + { toBorshVar* `BS.ByteString'& + , getVarBuffer `Buffer (CodedString)'& + } + -> `()' +#} + +{# fun pure unsafe rust_wrapper_ua_decode as rustWrapperIsUA + { toBorshVar* `BS.ByteString'& + } + -> `Bool' +#} diff --git a/src/Zcash.hs b/src/Zcash.hs new file mode 100644 index 0000000..9b289f3 --- /dev/null +++ b/src/Zcash.hs @@ -0,0 +1,15 @@ +module Zcash + ( f4Jumble + , isValidUnifiedAddress + ) where + +import C.Zcash (CodedString, rustWrapperF4Jumble, rustWrapperIsUA) +import qualified Data.ByteString as BS +import Foreign.Rust.Marshall.Fixed +import Foreign.Rust.Marshall.Variable + +f4Jumble :: BS.ByteString -> CodedString +f4Jumble = withPureBorshVarBuffer . rustWrapperF4Jumble + +isValidUnifiedAddress :: BS.ByteString -> Bool +isValidUnifiedAddress = rustWrapperIsUA diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..ce32d8f --- /dev/null +++ b/stack.yaml @@ -0,0 +1,72 @@ +# This file was automatically generated by 'stack init' +# +# Some commonly used options have been documented as comments in this file. +# For advanced use and comprehensive documentation of the format, please see: +# https://docs.haskellstack.org/en/stable/yaml_configuration/ + +# Resolver to choose a 'specific' stackage snapshot or a compiler version. +# A snapshot resolver dictates the compiler version and the set of packages +# to be used for project dependencies. For example: +# +# resolver: lts-3.5 +# resolver: nightly-2015-09-21 +# resolver: ghc-7.10.2 +# +# The location of a snapshot can be provided as a file or url. Stack assumes +# a snapshot provided as a file might change, whereas a url resource does not. +# +# resolver: ./custom-snapshot.yaml +# resolver: https://example.com/snapshots/2018-01-01.yaml +resolver: + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/17.yaml + +# User packages to be built. +# Various formats can be used as shown in the example below. +# +# packages: +# - some-directory +# - https://example.com/foo/bar/baz-0.0.2.tar.gz +# subdirs: +# - auto-update +# - wai +packages: +- . +# Dependency packages to be pulled from upstream that are not in the resolver. +# These entries can reference officially published versions as well as +# forks / in-progress versions pinned to a git hash. For example: +# +extra-deps: +# - acme-missiles-0.3 + - git: https://git.vergara.tech/Vergara_Tech/haskell-foreign-rust.git + commit: 787c2e813eb3a5d16c375d4b37dfefbd2adcdf05 + - git: https://github.com/well-typed/borsh.git + commit: d2fcfa159e0a844b1ec5e8ed3e232d4b380fa831 + - vector-0.13.0.0@sha256:fa5cac81a17a5af388716792e8b99c24b3b66770086756d0d8b23f8272a0244c,9112 + - aeson-2.1.2.1@sha256:f10f3c661bd5cf57aee46b94420e47736240b8e209ac15f4bfc1a4e4d55831fa,6344 + - generically-0.1.1 +# +# extra-deps: [] + +# Override default flag values for local packages and extra-deps +# flags: {} + +# Extra package databases containing global packages +# extra-package-dbs: [] + +# Control whether we use the GHC we find on the path +# system-ghc: true +# +# Require a specific version of Stack, using version ranges +# require-stack-version: -any # Default +# require-stack-version: ">=2.9" +# +# Override the architecture used by Stack, especially useful on Windows +# arch: i386 +# arch: x86_64 +# +# Extra directories used by Stack for building +# extra-include-dirs: [/path/to/dir] +# extra-lib-dirs: [/path/to/dir] +# +# Allow a newer minor version of GHC than the snapshot specifies +# compiler-check: newer-minor diff --git a/test/Spec.hs b/test/Spec.hs new file mode 100644 index 0000000..dfbc469 --- /dev/null +++ b/test/Spec.hs @@ -0,0 +1,123 @@ +{-# LANGUAGE OverloadedStrings #-} + +import C.Zcash (CodedString(CodedString), rustWrapperIsUA) +import qualified Data.ByteString as BS +import Data.Word +import Test.Hspec +import Zcash + +main :: IO () +main = do + hspec $ do + describe "F4Jumble" $ do + it "jumble a string" $ do + let input = + [ 0x5d + , 0x7a + , 0x8f + , 0x73 + , 0x9a + , 0x2d + , 0x9e + , 0x94 + , 0x5b + , 0x0c + , 0xe1 + , 0x52 + , 0xa8 + , 0x04 + , 0x9e + , 0x29 + , 0x4c + , 0x4d + , 0x6e + , 0x66 + , 0xb1 + , 0x64 + , 0x93 + , 0x9d + , 0xaf + , 0xfa + , 0x2e + , 0xf6 + , 0xee + , 0x69 + , 0x21 + , 0x48 + , 0x1c + , 0xdd + , 0x86 + , 0xb3 + , 0xcc + , 0x43 + , 0x18 + , 0xd9 + , 0x61 + , 0x4f + , 0xc8 + , 0x20 + , 0x90 + , 0x5d + , 0x04 + , 0x2b + ] :: [Word8] + let out = + [ 0x03 + , 0x04 + , 0xd0 + , 0x29 + , 0x14 + , 0x1b + , 0x99 + , 0x5d + , 0xa5 + , 0x38 + , 0x7c + , 0x12 + , 0x59 + , 0x70 + , 0x67 + , 0x35 + , 0x04 + , 0xd6 + , 0xc7 + , 0x64 + , 0xd9 + , 0x1e + , 0xa6 + , 0xc0 + , 0x82 + , 0x12 + , 0x37 + , 0x70 + , 0xc7 + , 0x13 + , 0x9c + , 0xcd + , 0x88 + , 0xee + , 0x27 + , 0x36 + , 0x8c + , 0xd0 + , 0xc0 + , 0x92 + , 0x1a + , 0x04 + , 0x44 + , 0xc8 + , 0xe5 + , 0x85 + , 0x8d + , 0x22 + ] :: [Word8] + CodedString (BS.pack out) `shouldBe` f4Jumble (BS.pack input) + describe "Unified address" $ do + it "succeeds with correct UA" $ do + let ua = + "u1salpdyefywvsg2dlmxg9589yznh0h9v6qjr478k80amtkqkws5pr408lxt2953dpprvu06mahxt99cv65fgsm7sw8hlchplfg5pl89ur" + isValidUnifiedAddress ua `shouldBe` True + it "fails with incorrect UA" $ do + let ua = + "u1salpdyefbreakingtheaddressh0h9v6qjr478k80amtkqkws5pr408lxt2953dpprvu06mahxt99cv65fgsm7sw8hlchplfg5pl89ur" + isValidUnifiedAddress ua `shouldBe` False diff --git a/zcash-haskell.cabal b/zcash-haskell.cabal new file mode 100644 index 0000000..1f60903 --- /dev/null +++ b/zcash-haskell.cabal @@ -0,0 +1,58 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.35.1. +-- +-- see: https://github.com/sol/hpack + +name: zcash-haskell +version: 0.1.0 +synopsis: Utilities to interact with the Zcash blockchain +description: Please see the README on the repo at +category: Blockchain +author: Rene Vergara +maintainer: rene@vergara.network +copyright: (c)2023 Vergara Technologies LLC +license: BOSL +license-file: LICENSE +build-type: Simple +extra-source-files: + README.md + CHANGELOG.md + +source-repository head + type: git + location: https://git.vergara.tech/Vergara_Tech/zcash-haskell + +library + exposed-modules: + C.GettingStarted + C.Zcash + Zcash + other-modules: + Paths_zcash_haskell + hs-source-dirs: + src + pkgconfig-depends: + rust_wrapper-uninstalled + build-depends: + base >=4.7 && <5 + , borsh + , bytestring + , foreign-rust + , text + default-language: Haskell2010 + +test-suite zcash-haskell-test + type: exitcode-stdio-1.0 + main-is: Spec.hs + other-modules: + Paths_zcash_haskell + hs-source-dirs: + test + ghc-options: -threaded -rtsopts -with-rtsopts=-N + build-depends: + base >=4.7 && <5 + , bytestring + , hspec + , zcash-haskell + default-language: Haskell2010