├── .changeset ├── README.md └── config.json ├── .devcontainer └── devcontainer.json ├── .editorconfig ├── .eslintrc.base.yml ├── .eslintrc.ts.yml ├── .gitattributes ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug.yml │ ├── config.yml │ ├── epic-template.md │ ├── feature.yml │ └── issue-template.md ├── dependabot.yml ├── pull_request_template.md └── workflows │ ├── pull-request.yml │ ├── release.yml │ ├── transfer-to-project.yml │ └── typedoc-generator.yml ├── .gitignore ├── .gitpod.yml ├── .husky ├── commit-msg └── pre-push ├── .ignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── LICENSE-APACHE ├── MIGRATION.md ├── README.md ├── SECURITY.md ├── TOOLING.md ├── commitlint.config.js ├── docs ├── README_TYPEDOC.md └── package-architecture.png ├── package.json ├── packages ├── accounts │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── account.ts │ │ ├── account_creator.ts │ │ ├── connection.ts │ │ ├── constants.ts │ │ ├── contract.ts │ │ ├── errors.ts │ │ ├── index.ts │ │ ├── interface.ts │ │ ├── local-view-execution │ │ │ ├── index.ts │ │ │ ├── runtime.ts │ │ │ ├── storage.ts │ │ │ └── types.ts │ │ ├── types.ts │ │ └── utils.ts │ ├── test │ │ ├── account.access_key.test.ts │ │ ├── account.test.ts │ │ ├── config.js │ │ ├── contract.test.ts │ │ ├── contract_abi.test.ts │ │ ├── lve_runtime.test.ts │ │ ├── lve_storage.test.ts │ │ ├── promise.test.ts │ │ ├── providers.test.ts │ │ ├── test-utils.js │ │ └── wasm │ │ │ ├── guestbook.wasm │ │ │ └── multisig.wasm │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── biometric-ed25519 │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── fido2.ts │ │ ├── index.d.ts │ │ ├── index.ts │ │ └── utils.ts │ ├── test │ │ └── utils.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── build │ ├── cjsify.js │ └── package.json ├── client │ ├── .eslintrc.yml │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── constants.ts │ │ ├── crypto.ts │ │ ├── funded_account.ts │ │ ├── index.ts │ │ ├── interfaces │ │ │ ├── dependencies.ts │ │ │ ├── index.ts │ │ │ ├── transactions.ts │ │ │ └── view.ts │ │ ├── providers.ts │ │ ├── signing │ │ │ ├── index.ts │ │ │ └── signers.ts │ │ ├── transactions │ │ │ ├── actions.ts │ │ │ ├── composers │ │ │ │ ├── index.ts │ │ │ │ ├── signed_transaction_composer.ts │ │ │ │ └── transaction_composer.ts │ │ │ ├── create_account.ts │ │ │ ├── index.ts │ │ │ └── sign_and_send.ts │ │ └── view.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── cookbook │ ├── CHANGELOG.md │ ├── README.md │ ├── accounts │ │ ├── access-keys │ │ │ ├── README.md │ │ │ ├── create-full-access-key.ts │ │ │ ├── create-function-access-key.ts │ │ │ └── delete-access-key.ts │ │ ├── create-funded-testnet-account.ts │ │ ├── create-mainnet-account.ts │ │ ├── create-testnet-account.ts │ │ └── index.ts │ ├── api-keys │ │ ├── backup-provider.js │ │ ├── near-connection.js │ │ └── provider-example.js │ ├── index.ts │ ├── package.json │ ├── transactions │ │ ├── batch-transactions.ts │ │ ├── get-tx-status.ts │ │ ├── index.ts │ │ ├── meta-transaction-relayer.ts │ │ ├── meta-transaction.ts │ │ └── traverse-blocks.ts │ ├── tsconfig.json │ └── utils │ │ ├── calculate-gas.ts │ │ ├── check-account-existence.ts │ │ ├── deploy-contract.ts │ │ ├── get-state.ts │ │ ├── index.ts │ │ ├── unwrap-near.ts │ │ ├── verify-signature.ts │ │ ├── wasm-files │ │ ├── staking_pool_factory.wasm │ │ └── status_message.wasm │ │ └── wrap-near.ts ├── crypto │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── constants.ts │ │ ├── index.ts │ │ ├── key_pair.ts │ │ ├── key_pair_base.ts │ │ ├── key_pair_ed25519.ts │ │ ├── key_pair_secp256k1.ts │ │ └── public_key.ts │ ├── test │ │ └── key_pair.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── iframe-rpc │ ├── .eslintrc.yml │ ├── CHANGELOG.md │ ├── README.md │ ├── package.json │ ├── src │ │ ├── iframe-rpc-error.ts │ │ ├── iframe-rpc.ts │ │ ├── index.ts │ │ └── types.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── keystores-browser │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── browser_local_storage_key_store.ts │ │ ├── index.ts │ │ └── multi_contract_browser_local_storage_key_store.ts │ ├── test │ │ ├── browser_keystore.test.ts │ │ ├── keystore_common.ts │ │ └── multi_contract_browser_keystore_common.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── keystores-node │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── index.ts │ │ └── unencrypted_file_system_keystore.ts │ ├── test │ │ ├── keystore_common.ts │ │ └── unencrypted_file_system_keystore.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── keystores │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── in_memory_key_store.ts │ │ ├── index.ts │ │ ├── keystore.ts │ │ ├── merge_key_store.ts │ │ └── multi_contract_keystore.ts │ ├── test │ │ ├── in_memory_keystore.test.ts │ │ ├── keystore_common.ts │ │ └── merge_keystore.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── near-api-js │ ├── .eslintignore │ ├── .gitattributes │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.js │ ├── package.json │ ├── src │ │ ├── .eslintrc.yml │ │ ├── account.ts │ │ ├── account_creator.ts │ │ ├── browser-connect.ts │ │ ├── browser-index.ts │ │ ├── common-index.ts │ │ ├── connect.ts │ │ ├── connection.ts │ │ ├── constants.ts │ │ ├── contract.ts │ │ ├── index.ts │ │ ├── key_stores │ │ │ ├── browser-index.ts │ │ │ ├── browser_local_storage_key_store.ts │ │ │ ├── in_memory_key_store.ts │ │ │ ├── index.ts │ │ │ ├── keystore.ts │ │ │ ├── merge_key_store.ts │ │ │ └── unencrypted_file_system_keystore.ts │ │ ├── near.ts │ │ ├── providers │ │ │ ├── failover-rpc-provider.ts │ │ │ ├── index.ts │ │ │ ├── json-rpc-provider.ts │ │ │ └── provider.ts │ │ ├── signer.ts │ │ ├── transaction.ts │ │ ├── utils │ │ │ ├── enums.ts │ │ │ ├── errors.ts │ │ │ ├── exponential-backoff.ts │ │ │ ├── format.ts │ │ │ ├── index.ts │ │ │ ├── key_pair.ts │ │ │ ├── logger.ts │ │ │ ├── logging.ts │ │ │ ├── rpc_errors.ts │ │ │ └── serialize.ts │ │ └── validators.ts │ ├── test │ │ ├── .eslintrc.yml │ │ ├── config.js │ │ └── test-utils.js │ ├── tsconfig.json │ └── typedoc.json ├── providers │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── exponential-backoff.ts │ │ ├── failover-rpc-provider.ts │ │ ├── fetch_json.ts │ │ ├── index.ts │ │ ├── json-rpc-provider.ts │ │ └── provider.ts │ ├── test │ │ ├── fetch_json.test.ts │ │ ├── fetch_json_error.test.ts │ │ └── providers.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── signers │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── index.ts │ │ ├── key_pair_signer.ts │ │ └── signer.ts │ ├── test │ │ └── key_pair_signer.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── tokens │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── ft │ │ │ ├── format.ts │ │ │ └── index.ts │ │ ├── index.ts │ │ ├── mainnet │ │ │ └── index.ts │ │ ├── nft │ │ │ └── index.ts │ │ └── testnet │ │ │ └── index.ts │ ├── test │ │ ├── fungible_token.test.ts │ │ └── native_token.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── transactions │ ├── CHANGELOG.md │ ├── README.md │ ├── jest.config.ts │ ├── package.json │ ├── src │ │ ├── action_creators.ts │ │ ├── actions.ts │ │ ├── create_transaction.ts │ │ ├── delegate.ts │ │ ├── index.ts │ │ ├── prefix.ts │ │ ├── schema.ts │ │ └── signature.ts │ ├── test │ │ ├── data │ │ │ ├── signed_transaction1.json │ │ │ └── transaction1.json │ │ ├── serialize.test.ts │ │ └── transaction.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── tsconfig │ ├── base.json │ ├── browser.json │ ├── cjs.json │ ├── esm.json │ └── package.json ├── types │ ├── CHANGELOG.md │ ├── README.md │ ├── package.json │ ├── src │ │ ├── accounts.ts │ │ ├── assignable.ts │ │ ├── enum.ts │ │ ├── errors.ts │ │ ├── index.ts │ │ └── provider │ │ │ ├── index.ts │ │ │ ├── light_client.ts │ │ │ ├── protocol.ts │ │ │ ├── request.ts │ │ │ ├── response.ts │ │ │ └── validator.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json └── utils │ ├── CHANGELOG.md │ ├── README.md │ ├── fetch_error_schema.js │ ├── jest.config.ts │ ├── package.json │ ├── src │ ├── constants.ts │ ├── errors │ │ ├── error_messages.json │ │ ├── errors.ts │ │ ├── index.ts │ │ ├── rpc_error_schema.json │ │ └── rpc_errors.ts │ ├── format.ts │ ├── index.ts │ ├── logger │ │ ├── console.logger.ts │ │ ├── index.ts │ │ ├── interface.ts │ │ └── logger.ts │ ├── logging.ts │ ├── provider.ts │ ├── utils.ts │ └── validators.ts │ ├── test │ ├── format.test.ts │ ├── logger.test.ts │ ├── rpc-errors.test.ts │ └── validator.test.ts │ ├── tsconfig.cjs.json │ ├── tsconfig.json │ ├── tsup.config.ts │ └── typedoc.json ├── pnpm-lock.yaml ├── pnpm-workspace.yaml ├── turbo.json └── typedoc.json /.changeset/README.md: -------------------------------------------------------------------------------- 1 | # Changesets 2 | 3 | Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works 4 | with multi-package repos, or single-package repos to help you version and publish your code. You can 5 | find the full documentation for it [in our repository](https://github.com/changesets/changesets) 6 | 7 | We have a quick list of common questions to get you started engaging with this project in 8 | [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) 9 | -------------------------------------------------------------------------------- /.changeset/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/@changesets/config@2.1.1/schema.json", 3 | "changelog": ["@changesets/changelog-github", { "repo": "near/near-api-js" }], 4 | "commit": false, 5 | "fixed": [["@near-js/*"]], 6 | "linked": [], 7 | "access": "public", 8 | "baseBranch": "master", 9 | "updateInternalDependencies": "patch", 10 | "bumpVersionsWithWorkspaceProtocolOnly": true, 11 | "ignore": [] 12 | } 13 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "image": "mcr.microsoft.com/devcontainers/base:ubuntu", 3 | "features": { 4 | "ghcr.io/devcontainers/features/node:1": {} 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # https://EditorConfig.org 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | -------------------------------------------------------------------------------- /.eslintrc.base.yml: -------------------------------------------------------------------------------- 1 | env: 2 | es6: true 3 | node: true 4 | extends: 5 | - 'eslint:recommended' 6 | parserOptions: 7 | ecmaVersion: 2020 8 | sourceType: module 9 | rules: 10 | indent: 11 | - error 12 | - 4 13 | - SwitchCase: 1 14 | linebreak-style: 15 | - error 16 | - unix 17 | quotes: 18 | - error 19 | - single 20 | semi: 21 | - error 22 | - always 23 | no-console: off 24 | object-curly-spacing: 25 | - error 26 | - always 27 | globals: 28 | window: true 29 | fetch: true 30 | Headers: true 31 | document: true 32 | -------------------------------------------------------------------------------- /.eslintrc.ts.yml: -------------------------------------------------------------------------------- 1 | extends: 2 | - './.eslintrc.base.yml' 3 | - 'plugin:@typescript-eslint/eslint-recommended' 4 | - 'plugin:@typescript-eslint/recommended' 5 | parser: '@typescript-eslint/parser' 6 | overrides: 7 | - rules: 8 | "@typescript-eslint/no-explicit-any": "warn" 9 | "@typescript-eslint/no-empty-function": "warn" 10 | files: 11 | - "*.ts" 12 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | package-lock.json linguist-generated=true -diff 2 | yarn.lock linguist-generated=true -diff 3 | docs/**/*.md linguist-generated=true -diff 4 | lib/**/*.js linguist-generated=true -diff 5 | lib/**/*.d.ts linguist-generated=true -diff 6 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | .github/workflows/ @morgsmccauley @andy-haynes @r-near @frol 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: 🚀 Feature request 4 | url: https://github.com/near/near-api-js/discussions 5 | about: Use GitHub discussions for feature requests. 6 | - name: ❓ Simple question - Discord chat 7 | url: https://near.chat 8 | about: This issue tracker is not for technical support. Please use our Discord chat, and ask the community for help. 9 | - name: ❓ Simple question - Stack Overflow 10 | url: https://stackoverflow.com/questions/tagged/nearprotocol 11 | about: This issue tracker is not for technical support. Please use Stack Overflow, and ask the community for help. 12 | - name: ❓ Advanced question - GitHub Discussions 13 | url: https://github.com/near/near-api-js/discussions 14 | about: Use GitHub Discussions for advanced and unanswered questions only, requiring a maintainer's answer. Make sure the question wasn't already asked. 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/epic-template.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Epic Template 3 | about: Epics are milestones or groups of alike issues 4 | title: '' 5 | labels: Epic 6 | assignees: '' 7 | 8 | --- 9 | 10 | ### Description 11 | 12 | (Overview of milestone or function governed by this epic) 13 | 14 | ### Resources 15 | 16 | (Relevant documentation, Figma links, and other reference material) 17 | 18 | Item 1 19 | 20 | Item 2 21 | 22 | Item 3 23 | 24 | ```[tasklist] 25 | ### Related Issues 26 | ``` 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/issue-template.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Issue Template 3 | about: Regular issues like defects or tasks. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | ### Description 11 | 12 | (Summary of task, purpose, impact) 13 | 14 | ### Optional: User Story 15 | 16 | (As a [user], I need [function, outcome, enhancement] that [provides value].) 17 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | ignore: 9 | - dependency-name: typescript 10 | versions: 11 | - 4.0.5 12 | - 4.0.6 13 | - 4.0.7 14 | - 4.2.3 15 | - 4.2.4 16 | - dependency-name: browserify 17 | versions: 18 | - 17.0.0 19 | - dependency-name: semver 20 | versions: 21 | - 7.3.5 22 | - dependency-name: mustache 23 | versions: 24 | - 4.2.0 25 | - dependency-name: typedoc 26 | versions: 27 | - 0.20.19 28 | - 0.20.20 29 | - 0.20.23 30 | - 0.20.24 31 | - 0.20.25 32 | - 0.20.27 33 | - 0.20.28 34 | - 0.20.29 35 | - 0.20.30 36 | - 0.20.32 37 | - 0.20.33 38 | - 0.20.34 39 | - dependency-name: highlight.js 40 | versions: 41 | - 10.4.1 42 | - 10.6.0 43 | - dependency-name: node-notifier 44 | versions: 45 | - 8.0.1 46 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | ## Pre-flight checklist 8 | 9 | - [ ] I have read the [Contributing Guidelines on pull requests](https://github.com/near/near-api-js/blob/master/CONTRIBUTING.md). 10 | - [ ] Commit messages follow the [conventional commits](https://www.conventionalcommits.org/) spec 11 | - [ ] **If this is a code change**: I have written unit tests. 12 | - [ ] **If this changes code in a published package**: I have run `pnpm changeset` to create a `changeset` JSON document appropriate for this change. 13 | - [ ] **If this is a new API or substantial change**: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan. 14 | 15 | ## Motivation 16 | 17 | 18 | 19 | ## Test Plan 20 | 21 | 22 | 23 | ## Related issues/PRs 24 | 25 | 26 | -------------------------------------------------------------------------------- /.github/workflows/pull-request.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - master 8 | pull_request: 9 | 10 | workflow_dispatch: 11 | 12 | concurrency: 13 | group: ${{ github.workflow }}-${{ github.ref }} 14 | cancel-in-progress: true 15 | 16 | jobs: 17 | checks: 18 | runs-on: ubuntu-latest 19 | 20 | steps: 21 | - name: Checkout repository 22 | uses: actions/checkout@v4 23 | 24 | 25 | - name: Cache turbo build setup 26 | uses: actions/cache@v4 27 | with: 28 | path: .turbo 29 | key: ${{ runner.os }}-turbo-${{ github.sha }} 30 | restore-keys: | 31 | ${{ runner.os }}-turbo- 32 | 33 | - name: Setup pnpm 34 | uses: pnpm/action-setup@v4 35 | with: 36 | version: 10.4.1 37 | 38 | - name: Setup Node 39 | uses: actions/setup-node@v4 40 | with: 41 | node-version: 20.18.3 42 | cache: pnpm 43 | 44 | - name: Install dependencies 45 | run: pnpm install 46 | 47 | - name: Build 48 | run: pnpm build 49 | 50 | - name: Lint 51 | run: pnpm lint 52 | 53 | - name: Test 54 | run: pnpm test 55 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | concurrency: ${{ github.workflow }}-${{ github.ref }} 9 | 10 | jobs: 11 | release: 12 | name: Release 13 | runs-on: ubuntu-latest 14 | permissions: 15 | contents: write 16 | issues: write 17 | pull-requests: write 18 | environment: NPM 19 | steps: 20 | - name: Checkout Repo 21 | uses: actions/checkout@v4 22 | 23 | - name: Setup pnpm 24 | uses: pnpm/action-setup@v4 25 | with: 26 | version: 10.4.1 27 | 28 | - name: Setup Node.js 29 | uses: actions/setup-node@v4 30 | with: 31 | node-version: 20.18.3 32 | cache: pnpm 33 | 34 | - name: Install Dependencies 35 | run: pnpm install 36 | 37 | - name: Build Packages 38 | run: pnpm build 39 | 40 | - name: Create Release Pull Request or Publish to NPM 41 | uses: changesets/action@v1 42 | with: 43 | publish: pnpm release 44 | commit: "chore(release): publish packages" 45 | title: "Publish packages" 46 | createGithubReleases: true 47 | env: 48 | GITHUB_TOKEN: ${{ github.token }} 49 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 50 | # setup-node by default reads from this env var 51 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 52 | -------------------------------------------------------------------------------- /.github/workflows/transfer-to-project.yml: -------------------------------------------------------------------------------- 1 | name: 'Add to DevTools Project' 2 | 3 | on: 4 | issues: 5 | types: 6 | - opened 7 | - reopened 8 | pull_request_target: 9 | types: 10 | - opened 11 | - reopened 12 | 13 | jobs: 14 | add-to-project: 15 | name: Add issue/PR to project 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/add-to-project@v1.0.0 19 | with: 20 | # add to DevTools Project #156 21 | project-url: https://github.com/orgs/near/projects/156 22 | github-token: ${{ secrets.PROJECT_GH_TOKEN }} 23 | -------------------------------------------------------------------------------- /.github/workflows/typedoc-generator.yml: -------------------------------------------------------------------------------- 1 | name: Deploy TypeDoc on GitHub pages 2 | 3 | on: 4 | push: 5 | branches: 6 | master 7 | 8 | env: 9 | NODE_VERSION: 20.18.3 10 | ENTRY_FILE: 'packages' 11 | CONFIG_PATH: 'tsconfig.base.json' 12 | USES_PNPM: 'true' 13 | DESTINATION_FOLDER: "docs" 14 | 15 | jobs: 16 | deploy: 17 | concurrency: ci-${{ github.ref }} 18 | runs-on: ubuntu-latest 19 | permissions: 20 | contents: write 21 | steps: 22 | - uses: actions/checkout@v4 23 | 24 | - name: Setup pnpm 25 | uses: pnpm/action-setup@v4 26 | with: 27 | version: 10.4.1 28 | 29 | - uses: actions/setup-node@v4 30 | with: 31 | node-version: ${{ env.NODE_VERSION }} 32 | cache: 'pnpm' 33 | 34 | - name: Install dependencies 35 | run: pnpm install 36 | 37 | - name: Build project 38 | run: pnpm build 39 | 40 | - name: Build documentation 41 | run: pnpm docs:generate 42 | 43 | - name: Deploy to GitHub pages 44 | uses: JamesIves/github-pages-deploy-action@v4 45 | with: 46 | token: ${{ secrets.GITHUB_TOKEN }} 47 | branch: gh-pages 48 | folder: ${{ env.DESTINATION_FOLDER }} 49 | clean: true 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #IDE 2 | .idea 3 | .vscode 4 | 5 | storage 6 | node_modules 7 | *.map 8 | 9 | dist/ 10 | 11 | # near-api-js files & coverage 12 | neardev/ 13 | coverage/ 14 | .nyc_output 15 | src\.ts/protos\.d\.ts 16 | package\-lock\.json 17 | lib/ 18 | 19 | # OS X 20 | .DS_Store 21 | 22 | # Generated test files 23 | test-keys/ 24 | 25 | .turbo 26 | 27 | typedoc-docs/ -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | tasks: 2 | - init: yarn install && yarn run build 3 | command: yarn run dev 4 | github: 5 | prebuilds: 6 | addCheck: true 7 | addComment: true 8 | -------------------------------------------------------------------------------- /.husky/commit-msg: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | pnpm commitlint --edit $1 4 | -------------------------------------------------------------------------------- /.husky/pre-push: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | pnpm lint 4 | -------------------------------------------------------------------------------- /.ignore: -------------------------------------------------------------------------------- 1 | packages/near-api-js/lib/ 2 | packages/near-api-js/dist/ 3 | packages/near-api-js/docs/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any 2 | person obtaining a copy of this software and associated 3 | documentation files (the "Software"), to deal in the 4 | Software without restriction, including without 5 | limitation the rights to use, copy, modify, merge, 6 | publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software 8 | is furnished to do so, subject to the following 9 | conditions: 10 | 11 | The above copyright notice and this permission notice 12 | shall be included in all copies or substantial portions 13 | of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 16 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 17 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 22 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Reporting a Vulnerability 4 | 5 | To report security issues in this project please send an email to [security@near.org](mailto:security@near.org) 6 | -------------------------------------------------------------------------------- /TOOLING.md: -------------------------------------------------------------------------------- 1 | # Tooling 2 | 3 | This document describes at a high level some the key tools used across `near-js`. 4 | 5 | ## Package Manager - [PNPM](https://pnpm.io/) 6 | PNPM is a package manager focused on fast installations while still being disk efficient. It manages dependency installations across the monorepo as well as linking interdependent packages. Unlike other package managers, PNPM creates a non-flat `node_modules` structure which ensures packages only have access to dependencies defined in their `package.json`. 7 | 8 | ## Build System - [Turborepo](https://turborepo.org/) 9 | Turborepo provides tooling for orchestrating commands across the monorepo. Similar to Yarn Workspaces, Turborepo can run tasks within each project via a single command, but rather than executing serially, Turborepo has various performance enhancing features such as task parallelization and local/remote caching. 10 | 11 | ## Package Publishing - [Changesets](https://github.com/changesets/changesets) 12 | Changesets manages package versioning, changelog generation, and publishing to the package registry. Changesets provides a CLI to simplify bumping packages and adding changelog entries, it then automates versioning and publishing via GitHub Actions. 13 | 14 | ## Automation - [GitHub Actions](https://github.com/features/actions) 15 | Github Actions is used to automate various tasks including PR checks (linting, type checks, and tests), docs generation, and also releasing packages to NPM. 16 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | '@commitlint/config-conventional' 4 | ], 5 | }; 6 | -------------------------------------------------------------------------------- /docs/package-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/near/near-api-js/96b63f4d48815400e4422182bf8c98501995e8a6/docs/package-architecture.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/monorepo", 3 | "private": true, 4 | "engines": { 5 | "node": ">=20.18.3", 6 | "pnpm": ">=10.4.1" 7 | }, 8 | "packageManager": "pnpm@10.4.1", 9 | "scripts": { 10 | "preinstall": "npx only-allow pnpm", 11 | "build": "turbo run build", 12 | "clean": "turbo run clean", 13 | "lint": "turbo run lint", 14 | "lint:fix": "turbo run lint:fix", 15 | "autoclave": "rimraf packages/**/dist && rimraf packages/**/lib && rimraf packages/**/node_modules && rimraf packages/**/coverage && rimraf packages/**/.turbo && rm -rf node_modules", 16 | "test": "turbo run test", 17 | "release": "changeset publish", 18 | "prepare": "husky install", 19 | "docs:generate": "typedoc" 20 | }, 21 | "devDependencies": { 22 | "@arethetypeswrong/cli": "^0.18.1", 23 | "@changesets/changelog-github": "0.5.1", 24 | "@changesets/cli": "2.28.1", 25 | "@commitlint/cli": "19.3.0", 26 | "@commitlint/config-conventional": "19.7.1", 27 | "@typescript-eslint/eslint-plugin": "6.21.0", 28 | "@typescript-eslint/parser": "6.21.0", 29 | "commitlint": "19.7.1", 30 | "esbuild-fix-imports-plugin": "^1.0.21", 31 | "eslint": "8.20.0", 32 | "husky": "9.1.7", 33 | "rimraf": "6.0.1", 34 | "tsconfig": "workspace:*", 35 | "tsup": "^8.5.0", 36 | "turbo": "2.4.4", 37 | "typedoc": "0.27.9", 38 | "typescript": "5.4.5" 39 | }, 40 | "resolutions": { 41 | "near-sandbox": "0.0.18", 42 | "near-api-js": "workspace:*" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /packages/accounts/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/accounts 2 | 3 | A collection of classes, functions, and types for interacting with accounts and contracts. 4 | 5 | ## Modules 6 | 7 | - [Account](https://github.com/near/near-api-js/blob/master/packages/accounts/src/account.ts) a class with methods to transfer NEAR, manage account keys, sign transactions, etc. 8 | - [AccountMultisig](https://github.com/near/near-api-js/blob/master/packages/accounts/src/account_multisig.ts) a [multisig](https://github.com/near/core-contracts/tree/master/multisig) deployed `Account` requiring multiple keys to sign transactions 9 | - [Account2FA](https://github.com/near/near-api-js/blob/master/packages/accounts/src/account_2fa.ts) extension of `AccountMultisig` used in conjunction with 2FA provided by [near-contract-helper](https://github.com/near/near-contract-helper) 10 | - [AccountCreator](https://github.com/near/near-api-js/blob/master/packages/accounts/src/account_creator.ts) classes for creating NEAR accounts 11 | - [Contract](https://github.com/near/near-api-js/blob/master/packages/accounts/src/contract.ts) represents a deployed smart contract with view and/or change methods 12 | - [Connection](https://github.com/near/near-api-js/blob/master/packages/accounts/src/connection.ts) a record containing the information required to connect to NEAR RPC 13 | - [Constants](https://github.com/near/near-api-js/blob/master/packages/accounts/src/constants.ts) account-specific constants 14 | - [Types](https://github.com/near/near-api-js/blob/master/packages/accounts/src/types.ts) account-specific types 15 | 16 | # License 17 | 18 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 19 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 20 | -------------------------------------------------------------------------------- /packages/accounts/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/accounts/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/accounts", 3 | "version": "2.0.2", 4 | "description": "Classes encapsulating account-specific functionality", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "type": "module", 8 | "scripts": { 9 | "build": "tsup", 10 | "clean": "rimraf lib/", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 13 | "test": "DEFAULT_FINALITY=optimistic jest", 14 | "check-exports": "attw --pack ." 15 | }, 16 | "keywords": [], 17 | "author": "", 18 | "license": "ISC", 19 | "dependencies": { 20 | "@near-js/crypto": "workspace:*", 21 | "@near-js/providers": "workspace:*", 22 | "@near-js/signers": "workspace:*", 23 | "@near-js/tokens": "workspace:*", 24 | "@near-js/transactions": "workspace:*", 25 | "@near-js/types": "workspace:*", 26 | "@near-js/utils": "workspace:*", 27 | "@noble/hashes": "1.7.1", 28 | "borsh": "1.0.0", 29 | "depd": "2.0.0", 30 | "is-my-json-valid": "^2.20.6", 31 | "lru_map": "0.4.1", 32 | "near-abi": "0.2.0" 33 | }, 34 | "devDependencies": { 35 | "@jest/globals": "^29.7.0", 36 | "@near-js/keystores": "workspace:*", 37 | "@scure/base": "^1.2.4", 38 | "@types/depd": "^1.1.37", 39 | "@types/json-schema": "^7.0.15", 40 | "@types/node": "20.0.0", 41 | "build": "workspace:*", 42 | "jest": "29.7.0", 43 | "near-hello": "0.5.1", 44 | "near-workspaces": "5.0.0", 45 | "semver": "7.7.1", 46 | "ts-jest": "29.2.6", 47 | "tsconfig": "workspace:*", 48 | "typescript": "5.4.5" 49 | }, 50 | "peerDependencies": { 51 | "@near-js/crypto": "^2.0.1", 52 | "@near-js/providers": "^2.0.1", 53 | "@near-js/signers": "^2.0.1", 54 | "@near-js/transactions": "^2.0.1", 55 | "@near-js/types": "^2.0.1", 56 | "@near-js/utils": "^2.0.1" 57 | }, 58 | "files": [ 59 | "lib" 60 | ], 61 | "exports": { 62 | ".": { 63 | "import": "./lib/esm/index.js", 64 | "require": "./lib/commonjs/index.cjs" 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /packages/accounts/src/constants.ts: -------------------------------------------------------------------------------- 1 | import { parseNearAmount } from '@near-js/utils'; 2 | 3 | export const MULTISIG_STORAGE_KEY = '__multisigRequest'; 4 | export const MULTISIG_ALLOWANCE = BigInt(parseNearAmount('1')); 5 | // TODO: Different gas value for different requests (can reduce gas usage dramatically) 6 | export const MULTISIG_GAS = 100000000000000n; 7 | export const MULTISIG_DEPOSIT = 0n; 8 | export const MULTISIG_CHANGE_METHODS = ['add_request', 'add_request_and_confirm', 'delete_request', 'confirm']; 9 | export const MULTISIG_CONFIRM_METHODS = ['confirm']; 10 | -------------------------------------------------------------------------------- /packages/accounts/src/errors.ts: -------------------------------------------------------------------------------- 1 | import { ValidationError } from 'is-my-json-valid'; 2 | 3 | export class UnsupportedSerializationError extends Error { 4 | constructor(methodName: string, serializationType: string) { 5 | super(`Contract method '${methodName}' is using an unsupported serialization type ${serializationType}`); 6 | } 7 | } 8 | 9 | export class UnknownArgumentError extends Error { 10 | constructor(actualArgName: string, expectedArgNames: string[]) { 11 | super(`Unrecognized argument '${actualArgName}', expected '${JSON.stringify(expectedArgNames)}'`); 12 | } 13 | } 14 | 15 | export class ArgumentSchemaError extends Error { 16 | constructor(argName: string, errors: ValidationError[]) { 17 | super(`Argument '${argName}' does not conform to the specified ABI schema: '${JSON.stringify(errors)}'`); 18 | } 19 | } 20 | 21 | export class ConflictingOptions extends Error { 22 | constructor() { 23 | super('Conflicting contract method options have been passed. You can either specify ABI or a list of view/call methods.'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /packages/accounts/src/index.ts: -------------------------------------------------------------------------------- 1 | export { 2 | Account, 3 | AccountBalance, 4 | AccountAuthorizedApp, 5 | SignAndSendTransactionOptions 6 | } from './account'; 7 | export { 8 | AccountCreator, 9 | LocalAccountCreator, 10 | UrlAccountCreator, 11 | } from './account_creator'; 12 | export { Connection } from './connection'; 13 | export { 14 | MULTISIG_STORAGE_KEY, 15 | MULTISIG_ALLOWANCE, 16 | MULTISIG_GAS, 17 | MULTISIG_DEPOSIT, 18 | MULTISIG_CHANGE_METHODS, 19 | MULTISIG_CONFIRM_METHODS, 20 | } from './constants'; 21 | export { 22 | Contract, 23 | ContractMethods, 24 | } from './contract'; 25 | export { 26 | ArgumentSchemaError, 27 | ConflictingOptions, 28 | UnknownArgumentError, 29 | UnsupportedSerializationError, 30 | } from './errors'; 31 | export { 32 | MultisigDeleteRequestRejectionError, 33 | MultisigStateStatus, 34 | } from './types'; 35 | export { 36 | FunctionCallOptions, 37 | ChangeFunctionCallOptions, 38 | ViewFunctionCallOptions, 39 | } from './interface'; -------------------------------------------------------------------------------- /packages/accounts/src/interface.ts: -------------------------------------------------------------------------------- 1 | import { BlockReference } from "@near-js/types"; 2 | import type { Connection } from "./connection"; 3 | 4 | export interface IntoConnection { 5 | getConnection(): Connection; 6 | } 7 | 8 | /** 9 | * Options used to initiate a function call (especially a change function call) 10 | * @see {@link Account#viewFunction | viewFunction} to initiate a view function call 11 | */ 12 | export interface FunctionCallOptions { 13 | /** The NEAR account id where the contract is deployed */ 14 | contractId: string; 15 | /** The name of the method to invoke */ 16 | methodName: string; 17 | /** 18 | * named arguments to pass the method `{ messageText: 'my message' }` 19 | */ 20 | args?: object; 21 | /** max amount of gas that method call can use */ 22 | gas?: bigint; 23 | /** amount of NEAR (in yoctoNEAR) to send together with the call */ 24 | attachedDeposit?: bigint; 25 | /** 26 | * Convert input arguments into bytes array. 27 | */ 28 | stringify?: (input: any) => Buffer; 29 | } 30 | 31 | export interface ChangeFunctionCallOptions extends FunctionCallOptions { 32 | /** 33 | * Metadata to send the NEAR Wallet if using it to sign transactions. 34 | * @see RequestSignTransactionsOptions 35 | */ 36 | walletMeta?: string; 37 | /** 38 | * Callback url to send the NEAR Wallet if using it to sign transactions. 39 | * @see RequestSignTransactionsOptions 40 | */ 41 | walletCallbackUrl?: string; 42 | } 43 | export interface ViewFunctionCallOptions extends FunctionCallOptions { 44 | parse?: (response: Uint8Array) => any; 45 | blockQuery?: BlockReference; 46 | } -------------------------------------------------------------------------------- /packages/accounts/src/local-view-execution/storage.ts: -------------------------------------------------------------------------------- 1 | import { LRUMap } from 'lru_map'; 2 | import { BlockHash, BlockReference } from '@near-js/types'; 3 | import { ContractState } from './types'; 4 | 5 | export interface StorageData { 6 | blockHeight: number, 7 | blockTimestamp: number, 8 | contractCode: string, 9 | contractState: ContractState 10 | } 11 | 12 | export interface StorageOptions { 13 | max: number 14 | } 15 | 16 | export class Storage { 17 | private readonly cache: LRUMap; 18 | 19 | private static MAX_ELEMENTS = 100; 20 | 21 | // map block hash to block height 22 | private blockHeights: Map; 23 | 24 | constructor(options: StorageOptions = { max: Storage.MAX_ELEMENTS }) { 25 | this.cache = new LRUMap(options.max); 26 | this.blockHeights = new Map(); 27 | } 28 | 29 | public load(blockRef: BlockReference): StorageData | undefined { 30 | const noBlockId = !('blockId' in blockRef); 31 | 32 | if (noBlockId) return undefined; 33 | 34 | let blockId = blockRef.blockId; 35 | 36 | // block hash is passed, so get its corresponding block height 37 | if (blockId.toString().length == 44) { 38 | blockId = this.blockHeights.get(blockId.toString()); 39 | } 40 | // get cached values for the given block height 41 | return this.cache.get(blockId); 42 | } 43 | 44 | public save(blockHash: BlockHash, { blockHeight, blockTimestamp, contractCode, contractState }: StorageData) { 45 | this.blockHeights.set(blockHash, blockHeight); 46 | this.cache.set(blockHeight, { blockHeight, blockTimestamp, contractCode, contractState }); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /packages/accounts/src/local-view-execution/types.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/no-empty-interface */ 2 | export interface ContractState extends Array<{ 3 | key: Buffer; 4 | value: Buffer; 5 | }> { } -------------------------------------------------------------------------------- /packages/accounts/src/types.ts: -------------------------------------------------------------------------------- 1 | export enum MultisigDeleteRequestRejectionError { 2 | CANNOT_DESERIALIZE_STATE = 'Cannot deserialize the contract state', 3 | MULTISIG_NOT_INITIALIZED = 'Smart contract panicked: Multisig contract should be initialized before usage', 4 | NO_SUCH_REQUEST = 'Smart contract panicked: panicked at \'No such request: either wrong number or already confirmed\'', 5 | REQUEST_COOLDOWN_ERROR = 'Request cannot be deleted immediately after creation.', 6 | METHOD_NOT_FOUND = 'Contract method is not found' 7 | } 8 | 9 | export enum MultisigStateStatus { 10 | INVALID_STATE, 11 | STATE_NOT_INITIALIZED, 12 | VALID_STATE, 13 | UNKNOWN_STATE 14 | } 15 | -------------------------------------------------------------------------------- /packages/accounts/test/config.js: -------------------------------------------------------------------------------- 1 | import { Worker } from 'near-workspaces'; 2 | import fs from 'fs'; 3 | 4 | module.exports = async function getConfig(env) { 5 | switch (env) { 6 | case 'production': 7 | case 'mainnet': 8 | return { 9 | networkId: 'mainnet', 10 | nodeUrl: 'https://rpc.mainnet.near.org', 11 | walletUrl: 'https://wallet.near.org', 12 | helperUrl: 'https://helper.mainnet.near.org', 13 | }; 14 | case 'development': 15 | case 'testnet': 16 | return { 17 | networkId: 'default', 18 | nodeUrl: 'https://rpc.testnet.near.org', 19 | walletUrl: 'https://wallet.testnet.near.org', 20 | helperUrl: 'https://helper.testnet.near.org', 21 | masterAccount: 'test.near', 22 | }; 23 | case 'betanet': 24 | return { 25 | networkId: 'betanet', 26 | nodeUrl: 'https://rpc.betanet.near.org', 27 | walletUrl: 'https://wallet.betanet.near.org', 28 | helperUrl: 'https://helper.betanet.near.org', 29 | }; 30 | case 'local': 31 | return { 32 | networkId: 'local', 33 | nodeUrl: 'http://localhost:3030', 34 | keyPath: `${process.env.HOME}/.near/validator_key.json`, 35 | walletUrl: 'http://localhost:4000/wallet', 36 | }; 37 | case 'test': 38 | case 'ci': { 39 | const worker = await Worker.init(); 40 | const keyFile = fs.readFileSync(`${worker.rootAccount.manager.config.homeDir}/validator_key.json`); 41 | const keyPair = JSON.parse(keyFile.toString()); 42 | return { 43 | networkId: worker.config.network, 44 | nodeUrl: worker.manager.config.rpcAddr, 45 | masterAccount: worker.rootAccount._accountId, 46 | secretKey: keyPair.secret_key || keyPair.private_key, 47 | worker: worker 48 | }; 49 | } 50 | default: 51 | throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`); 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /packages/accounts/test/wasm/guestbook.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/near/near-api-js/96b63f4d48815400e4422182bf8c98501995e8a6/packages/accounts/test/wasm/guestbook.wasm -------------------------------------------------------------------------------- /packages/accounts/test/wasm/multisig.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/near/near-api-js/96b63f4d48815400e4422182bf8c98501995e8a6/packages/accounts/test/wasm/multisig.wasm -------------------------------------------------------------------------------- /packages/accounts/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/accounts/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ], 10 | } 11 | -------------------------------------------------------------------------------- /packages/accounts/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/accounts/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/biometric-ed25519/README.md: -------------------------------------------------------------------------------- 1 | 2 | # NEAR Web 3 Authentication 3 | 4 | 5 | 6 | @near-js/biometric-ed25519 package contains interfaces to assist the implementation of biometric driven web3 authentication. 7 | 8 | 9 | ## Installation and Usage 10 | 11 | 12 | 13 | The easiest way to use NEAR Web 3 Authentication is to install the [`biometric-ed25519`](https://www.npmjs.com/package/@near-js/biometric-ed25519) package from the NPM registry. 14 | 15 | 16 | ```bash 17 | 18 | # Using Yarn 19 | 20 | yarn add @near-js/biometric-ed25519 21 | 22 | 23 | 24 | # Using NPM. 25 | 26 | npm install @near-js/biometric-ed25519 27 | 28 | ``` 29 | 30 | Then in your dApp: 31 | 32 | 33 | 34 | ```ts 35 | import { createKey, getKeys } from "@near-js/biometric-ed25519"; 36 | 37 | // To register with userName 38 | const key = await createKey(userName); 39 | 40 | 41 | // To retrieve keys with userName 42 | const keys = await getKeys(userName); 43 | 44 | ``` 45 | 46 | As the nature of Elliptic Curve crypto, `getKeys` return with two possible public key pairs. In order to select the correct public key pair out of two pairs, it is essential to implement a logic to preserve the public key pair created from `createKey` and retrieve them after calling `getKeys` and find the right one among the two. 47 | 48 | 49 | ## Use Case 50 | 51 | 1. Check if given user name exist against RPC endpoint. 52 | 2. User creates an authentication to browser using biometric finger print with desire user name. (by calling `createKey`) 53 | 3. Use public key from step 2 to create a near account with given user name. 54 | 4. When user come back to authenticate, use `getKeys` to retrieve two possible public key pairs. 55 | 5. Create near connection with user name 56 | 6. Retrieve list of access keys and check if one of public key pairs exist in the list. 57 | 7. If exist one is found, it should be used to make the authentication. 58 | 59 | 60 | ## License 61 | 62 | 63 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). See [LICENSE-MIT](LICENSE-MIT) and [LICENSE-APACHE](LICENSE-APACHE) for details. 64 | -------------------------------------------------------------------------------- /packages/biometric-ed25519/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/biometric-ed25519/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/biometric-ed25519", 3 | "description": "JavaScript library to handle webauthn and biometric keys", 4 | "version": "2.0.2", 5 | "main": "lib/esm/index.js", 6 | "types": "lib/esm/index.d.ts", 7 | "type": "module", 8 | "scripts": { 9 | "build": "tsup", 10 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 11 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 12 | "test": "jest", 13 | "check-exports": "attw --pack ." 14 | }, 15 | "keywords": [], 16 | "author": "Pagoda", 17 | "license": "ISC", 18 | "dependencies": { 19 | "@hexagon/base64": "2.0.4", 20 | "@near-js/crypto": "workspace:*", 21 | "@near-js/utils": "workspace:*", 22 | "@noble/curves": "1.8.1", 23 | "@noble/hashes": "1.7.1", 24 | "asn1-parser": "1.1.8", 25 | "borsh": "1.0.0", 26 | "buffer": "6.0.3", 27 | "build": "workspace:*", 28 | "cbor-js": "^0.1.0", 29 | "fido2-lib": "3.4.1" 30 | }, 31 | "devDependencies": { 32 | "@jest/globals": "^29.7.0", 33 | "@types/node": "20.0.0", 34 | "jest": "29.7.0", 35 | "ts-jest": "29.2.6" 36 | }, 37 | "exports": { 38 | ".": { 39 | "import": "./lib/esm/index.js", 40 | "require": "./lib/commonjs/index.cjs" 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /packages/biometric-ed25519/src/index.d.ts: -------------------------------------------------------------------------------- 1 | export type AssertionResponse = { 2 | authenticatorAttachment: 'platform' | 'cross-platform'; 3 | getClientExtensionResults: () => any; 4 | id: string; 5 | rawId: string; 6 | response: { 7 | authenticatorData: string; 8 | clientDataJSON: string; 9 | signature: string; 10 | userHandle: string; 11 | }; 12 | type: 'public-key'; 13 | }; 14 | -------------------------------------------------------------------------------- /packages/biometric-ed25519/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/biometric-ed25519/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/browser.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts", 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/biometric-ed25519/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/biometric-ed25519/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/build/cjsify.js: -------------------------------------------------------------------------------- 1 | import { lstat, opendir, readFile, rename, writeFile } from 'node:fs/promises'; 2 | import path from 'node:path'; 3 | 4 | async function enumerateContents(contentPath) { 5 | const dir = await opendir(contentPath); 6 | let files = []; 7 | for await (let entry of dir) { 8 | if (entry.name === 'node_modules') { 9 | continue; 10 | } 11 | 12 | const entryPath = path.join(contentPath, entry.name); 13 | if (entry.isDirectory()) { 14 | files = [...files, ...(await enumerateContents(entryPath))]; 15 | } else if (entry.name.endsWith('.js')) { 16 | files.push(entryPath); 17 | } 18 | } 19 | 20 | return files; 21 | } 22 | 23 | async function cjsIfy() { 24 | const [,, inputPath] = process.argv; 25 | const basePath = path.resolve(process.cwd(), inputPath); 26 | 27 | for (let projectFilePath of await enumerateContents(basePath)) { 28 | let contents = (await readFile(projectFilePath)).toString(); 29 | const relativeImports = [...contents.matchAll(/require\("(\.\.?\/+[^"]+)"\)/ig)]; 30 | for (let localImport of relativeImports) { 31 | const [matchedText, relativePath] = [...localImport]; 32 | if (relativePath.endsWith('.json')) { 33 | continue; 34 | } 35 | 36 | const absolutePath = path.resolve(projectFilePath.split('/').slice(0, -1).join('/'), relativePath); 37 | let isDirectory = false; 38 | try { 39 | isDirectory = (await lstat(absolutePath)).isDirectory(); 40 | } catch { /* lstat has failed because `absolutePath` points to a JS file but is missing the .js extension */ } 41 | 42 | const replacementPath = isDirectory 43 | ? `${relativePath}/index.cjs` 44 | : `${relativePath}.cjs`; 45 | contents = contents.replaceAll(matchedText, `require("${replacementPath}")`); 46 | } 47 | 48 | if (relativeImports.length) { 49 | await writeFile(projectFilePath, contents); 50 | } 51 | 52 | await rename(projectFilePath, [...projectFilePath.split('.').slice(0, -1), 'cjs'].join('.')); 53 | } 54 | } 55 | 56 | (async function() { 57 | await cjsIfy(); 58 | }()); 59 | -------------------------------------------------------------------------------- /packages/build/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "build", 3 | "version": "0.0.0", 4 | "type": "module", 5 | "private": true, 6 | "bin": { 7 | "cjsify": "./cjsify.js" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /packages/client/.eslintrc.yml: -------------------------------------------------------------------------------- 1 | env: 2 | es6: true 3 | node: true 4 | extends: 5 | - 'eslint:recommended' 6 | - 'plugin:@typescript-eslint/eslint-recommended' 7 | - 'plugin:@typescript-eslint/recommended' 8 | parser: '@typescript-eslint/parser' 9 | rules: 10 | no-inner-declarations: off 11 | indent: 12 | - error 13 | - 2 14 | - SwitchCase: 1 15 | '@typescript-eslint/no-explicit-any': off 16 | 17 | parserOptions: 18 | ecmaVersion: 2018 19 | sourceType: module 20 | -------------------------------------------------------------------------------- /packages/client/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/client", 3 | "version": "2.0.2", 4 | "description": "", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "lint": "eslint -c .eslintrc.yml src/**/*.ts --no-eslintrc --no-error-on-unmatched-pattern", 12 | "lint:fix": "eslint -c .eslintrc.yml src/**/*.ts --no-eslintrc --no-error-on-unmatched-pattern --fix", 13 | "check-exports": "attw --pack ." 14 | }, 15 | "dependencies": { 16 | "@near-js/crypto": "workspace:*", 17 | "@near-js/keystores": "workspace:*", 18 | "@near-js/providers": "workspace:*", 19 | "@near-js/signers": "workspace:*", 20 | "@near-js/transactions": "workspace:*", 21 | "@near-js/types": "workspace:*", 22 | "@near-js/utils": "workspace:*", 23 | "@near-js/accounts": "workspace:*", 24 | "@noble/hashes": "1.7.1" 25 | }, 26 | "keywords": [], 27 | "author": "", 28 | "license": "ISC", 29 | "devDependencies": { 30 | "@types/node": "20.0.0", 31 | "build": "workspace:*", 32 | "tsconfig": "workspace:*", 33 | "typescript": "5.4.5" 34 | }, 35 | "peerDependencies": { 36 | "@near-js/crypto": "^2.0.1", 37 | "@near-js/keystores": "^2.0.1", 38 | "@near-js/providers": "^2.0.1", 39 | "@near-js/signers": "^2.0.1", 40 | "@near-js/transactions": "^2.0.1", 41 | "@near-js/types": "^2.0.1", 42 | "@near-js/utils": "^2.0.1", 43 | "@near-js/accounts": "^2.0.1" 44 | }, 45 | "files": [ 46 | "lib" 47 | ], 48 | "exports": { 49 | ".": { 50 | "import": "./lib/esm/index.js", 51 | "require": "./lib/commonjs/index.cjs" 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /packages/client/src/constants.ts: -------------------------------------------------------------------------------- 1 | export const DEFAULT_META_TRANSACTION_BLOCK_HEIGHT_TTL = 100n; 2 | export const MAX_GAS = 300000000000000n; 3 | 4 | export const PAGODA_RPC_ENDPOINTS_MAINNET = [ 5 | 'https://rpc.near.org', 6 | 'https://rpc.mainnet.pagoda.co', 7 | ]; 8 | 9 | export const PAGODA_RPC_ARCHIVAL_ENDPOINTS_MAINNET = [ 10 | 'https://archival-rpc.near.org', 11 | ]; 12 | 13 | export const PAGODA_RPC_ENDPOINTS_TESTNET = [ 14 | 'https://rpc.testnet.near.org', 15 | 'https://rpc.testnet.pagoda.co', 16 | ]; 17 | 18 | export const PAGODA_RPC_ARCHIVAL_ENDPOINTS_TESTNET = [ 19 | 'https://archival-rpc.testnet.near.org', 20 | ]; 21 | 22 | export const KITWALLET_FUNDED_TESTNET_ACCOUNT_ENDPOINT = 'https://testnet-api.kitwallet.app/account'; 23 | -------------------------------------------------------------------------------- /packages/client/src/crypto.ts: -------------------------------------------------------------------------------- 1 | import { CurveType, KeyPair, KeyPairString } from '@near-js/crypto'; 2 | 3 | /** 4 | * Generate a random key pair for the specified elliptic curve 5 | * @param curve elliptic curve (e.g. `ed25519`) 6 | */ 7 | export function generateRandomKeyPair(curve: CurveType) { 8 | return KeyPair.fromRandom(curve); 9 | } 10 | 11 | /** 12 | * Parse a signing key pair from a private key string 13 | * @param privateKey private key string 14 | */ 15 | export function parseKeyPair(privateKey: KeyPairString) { 16 | return KeyPair.fromString(privateKey); 17 | } 18 | -------------------------------------------------------------------------------- /packages/client/src/funded_account.ts: -------------------------------------------------------------------------------- 1 | import type { FinalExecutionOutcome } from '@near-js/types'; 2 | 3 | import { KITWALLET_FUNDED_TESTNET_ACCOUNT_ENDPOINT } from './constants'; 4 | import { NewAccountParams } from './interfaces'; 5 | 6 | interface CreateFundedTestnetAccountParams extends NewAccountParams { 7 | endpointUrl?: string; 8 | } 9 | 10 | /** 11 | * Create a new funded testnet account via faucet REST endpoint 12 | * (e.g. create `new.testnet` with a preset amount of Near) 13 | * @param endpointUrl REST endpoint for the funded testnet account creation (defaults to the current Near Contract Helper endpoint) 14 | * @param newAccount name of the created account 15 | * @param newPublicKey public key for the created account's initial full access key 16 | */ 17 | export async function createFundedTestnetAccount({ 18 | newAccount, 19 | newPublicKey, 20 | endpointUrl = KITWALLET_FUNDED_TESTNET_ACCOUNT_ENDPOINT, 21 | }: CreateFundedTestnetAccountParams) { 22 | const res = await fetch(endpointUrl, { 23 | method: 'POST', 24 | body: JSON.stringify({ 25 | newAccountId: newAccount, 26 | newAccountPublicKey: newPublicKey, 27 | }), 28 | headers: { 'Content-Type': 'application/json' }, 29 | }); 30 | 31 | const { ok, status } = res; 32 | if (!ok) { 33 | throw new Error(`Failed to create account on ${endpointUrl}: ${status}`); 34 | } 35 | 36 | return await res.json() as FinalExecutionOutcome; 37 | } 38 | -------------------------------------------------------------------------------- /packages/client/src/index.ts: -------------------------------------------------------------------------------- 1 | export { formatNearAmount } from '@near-js/utils'; 2 | 3 | export * from './constants'; 4 | export * from './crypto'; 5 | export * from './funded_account'; 6 | export * from './interfaces'; 7 | export * from './providers'; 8 | export * from './signing'; 9 | export * from './transactions'; 10 | export * from './view'; 11 | -------------------------------------------------------------------------------- /packages/client/src/interfaces/dependencies.ts: -------------------------------------------------------------------------------- 1 | import { Provider } from '@near-js/providers'; 2 | import { Signer } from '@near-js/signers'; 3 | 4 | interface Dependent { 5 | deps: T; 6 | } 7 | 8 | interface RpcProviderDependent { 9 | rpcProvider: Provider; 10 | } 11 | 12 | interface SignerDependent { 13 | signer: Signer; 14 | } 15 | 16 | export interface RpcProviderDependency extends Dependent {} 17 | 18 | export interface SignerDependency extends Dependent {} 19 | 20 | export interface SignAndSendTransactionDependency extends Dependent {} 21 | -------------------------------------------------------------------------------- /packages/client/src/interfaces/index.ts: -------------------------------------------------------------------------------- 1 | export * from './dependencies'; 2 | export * from './transactions'; 3 | export * from './view'; 4 | -------------------------------------------------------------------------------- /packages/client/src/interfaces/view.ts: -------------------------------------------------------------------------------- 1 | import type { BlockReference } from '@near-js/types'; 2 | 3 | import { RpcProviderDependency } from './dependencies'; 4 | 5 | export interface RpcProviderQueryParams { 6 | blockReference?: BlockReference; 7 | } 8 | 9 | export interface ViewBaseParams extends RpcProviderDependency, RpcProviderQueryParams { 10 | } 11 | 12 | export interface ViewAccountParams extends ViewBaseParams { 13 | account: string; 14 | } 15 | 16 | export interface ViewValidatorStakeParams extends ViewAccountParams { 17 | validator: string; 18 | } 19 | 20 | export interface ViewParams extends ViewAccountParams { 21 | method: string; 22 | args?: T; 23 | } 24 | 25 | export interface ViewContractStateParams extends ViewAccountParams { 26 | prefix: string | Uint8Array; 27 | } 28 | 29 | export interface ViewAccessKeyParams extends ViewAccountParams { 30 | publicKey: string; 31 | } 32 | 33 | interface AccessKey { 34 | nonce: bigint; 35 | publicKey: string; 36 | } 37 | 38 | export interface FullAccessKey extends AccessKey {} 39 | export interface FunctionCallAccessKey extends AccessKey { 40 | contract: string; 41 | methods: string[]; 42 | allowance: bigint; 43 | } 44 | 45 | export interface AccessKeys { 46 | fullAccessKeys: FullAccessKey[]; 47 | functionCallAccessKeys: FunctionCallAccessKey[]; 48 | } 49 | 50 | export interface AccountState { 51 | availableBalance: bigint; 52 | codeHash: string; 53 | locked: bigint; 54 | storageUsed: bigint; 55 | } 56 | -------------------------------------------------------------------------------- /packages/client/src/providers.ts: -------------------------------------------------------------------------------- 1 | import { FailoverRpcProvider, JsonRpcProvider, Provider } from '@near-js/providers'; 2 | 3 | import { 4 | PAGODA_RPC_ARCHIVAL_ENDPOINTS_TESTNET, 5 | PAGODA_RPC_ENDPOINTS_MAINNET, 6 | PAGODA_RPC_ENDPOINTS_TESTNET, 7 | } from './constants'; 8 | 9 | /** 10 | * Get the set of public endpoints for the provided network 11 | * @param network target blockchain network (e.g. `mainnet`) 12 | */ 13 | export function getEndpointsByNetwork(network: string) { 14 | switch (network) { 15 | case 'testnet': 16 | return PAGODA_RPC_ENDPOINTS_TESTNET; 17 | case 'mainnet': 18 | return PAGODA_RPC_ENDPOINTS_MAINNET; 19 | default: 20 | return null; 21 | } 22 | } 23 | 24 | /** 25 | * Initialize a failover RPC provider capable of retrying requests against a set of endpoints 26 | * @param urls RPC endpoint URLs 27 | */ 28 | export function createRpcClientWrapper(urls: string[]): Provider { 29 | if (!urls) { 30 | throw new Error('at least one RPC endpoint URL required'); 31 | } 32 | 33 | return new FailoverRpcProvider(urls.map((url) => new JsonRpcProvider({ url }))); 34 | } 35 | 36 | /** 37 | * Initialize a failover RPC provider for the given network 38 | * @param network target blockchain network (e.g. `mainnet`) 39 | */ 40 | export function getProviderByNetwork(network: string) { 41 | return createRpcClientWrapper(getEndpointsByNetwork(network)); 42 | } 43 | 44 | /** 45 | * Initialize a failover RPC provider for a set of RPC endpoint URLs 46 | * @param urls RPC endpoint URLs 47 | */ 48 | export function getProviderByEndpoints(...urls: string[]) { 49 | return createRpcClientWrapper(urls); 50 | } 51 | 52 | /** 53 | * Initialize a testnet RPC provider 54 | */ 55 | export function getTestnetRpcProvider() { 56 | return getProviderByNetwork('testnet'); 57 | } 58 | 59 | /** 60 | * Initialize a testnet archival RPC provider 61 | */ 62 | export function getTestnetRpcArchivalProvider() { 63 | return createRpcClientWrapper(PAGODA_RPC_ARCHIVAL_ENDPOINTS_TESTNET); 64 | } 65 | 66 | /** 67 | * Initialize a mainnet RPC provider 68 | */ 69 | export function getMainnetRpcProvider() { 70 | return getProviderByNetwork('mainnet'); 71 | } 72 | -------------------------------------------------------------------------------- /packages/client/src/signing/index.ts: -------------------------------------------------------------------------------- 1 | export * from './signers'; -------------------------------------------------------------------------------- /packages/client/src/signing/signers.ts: -------------------------------------------------------------------------------- 1 | import { KeyPair, type KeyPairString } from "@near-js/crypto"; 2 | import { KeyStore } from '@near-js/keystores'; 3 | import { KeyPairSigner, Signer } from "@near-js/signers"; 4 | 5 | /** 6 | * Initialize a message signer from a KeyPair 7 | * @param keyPair used to sign transactions 8 | */ 9 | export function getSignerFromKeyPair(keyPair: KeyPair): Signer { 10 | return new KeyPairSigner(keyPair); 11 | } 12 | 13 | /** 14 | * Initialize a message singer from a private key string 15 | * @param privateKey string representation of the private key used to sign transactions 16 | */ 17 | export function getSignerFromPrivateKey( 18 | privateKey: KeyPairString 19 | ): Signer { 20 | return KeyPairSigner.fromSecretKey(privateKey); 21 | } 22 | 23 | /** 24 | * Initialize a message signer from a keystore instance 25 | * @param account used to sign transactions 26 | * @param network to sign transactions on 27 | * @param keyStore used to store the signing key 28 | */ 29 | export async function getSignerFromKeystore(account: string, network: string, keyStore: KeyStore): Promise { 30 | const keyPair = await keyStore.getKey(network, account); 31 | return new KeyPairSigner(keyPair); 32 | } 33 | -------------------------------------------------------------------------------- /packages/client/src/transactions/composers/index.ts: -------------------------------------------------------------------------------- 1 | export * from './signed_transaction_composer'; 2 | export * from './transaction_composer'; -------------------------------------------------------------------------------- /packages/client/src/transactions/index.ts: -------------------------------------------------------------------------------- 1 | export * from './actions'; 2 | export * from './composers'; 3 | export * from './create_account'; 4 | export * from './sign_and_send'; 5 | -------------------------------------------------------------------------------- /packages/client/src/transactions/sign_and_send.ts: -------------------------------------------------------------------------------- 1 | import { getTransactionLastResult } from '@near-js/utils'; 2 | 3 | import type { SignTransactionParams, SignAndSendTransactionParams } from '../interfaces'; 4 | import { getNonce } from '../view'; 5 | import { BlockReference } from '@near-js/types'; 6 | import { SerializedReturnValue } from '@near-js/types/lib/esm/provider/response'; 7 | 8 | const DEFAULT_FINALITY: BlockReference = { finality: 'final' }; 9 | 10 | /** 11 | * Sign a transaction, returning the signed transaction and encoded hash 12 | * @param transaction Transaction instance 13 | * @param signer MessageSigner 14 | */ 15 | export async function signTransaction({ transaction, deps: { signer } }: SignTransactionParams) { 16 | const [txHash, signedTransaction] = await signer.signTransaction(transaction); 17 | 18 | return { 19 | encodedTransactionHash: txHash, 20 | signedTransaction, 21 | }; 22 | } 23 | 24 | /** 25 | * Sign a transaction and publish to RPC 26 | * @param transaction Transaction instance to sign and publish 27 | * @param deps sign-and-send dependencies 28 | */ 29 | export async function signAndSendTransaction({ transaction, deps: { rpcProvider, signer } }: SignAndSendTransactionParams) { 30 | const { signedTransaction } = await signTransaction({ transaction, deps: { signer } }); 31 | const outcome = await rpcProvider.sendTransaction(signedTransaction); 32 | return { 33 | outcome, 34 | result: getTransactionLastResult(outcome) as T, 35 | }; 36 | } 37 | 38 | /** 39 | * Get the current nonce for an access key given an account and MessageSigner instance 40 | * @param account owner of the access key 41 | * @param blockReference block ID/finality 42 | * @param rpcProvider RPC provider instance 43 | * @param deps sign-and-send dependencies 44 | */ 45 | export async function getSignerNonce({ account, blockReference = DEFAULT_FINALITY, deps: { rpcProvider, signer } }) { 46 | return getNonce({ 47 | account, 48 | publicKey: (await signer.getPublicKey()).toString(), 49 | blockReference, 50 | deps: { rpcProvider }, 51 | }); 52 | } 53 | -------------------------------------------------------------------------------- /packages/client/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/client/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } -------------------------------------------------------------------------------- /packages/client/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/client/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/cookbook/accounts/access-keys/README.md: -------------------------------------------------------------------------------- 1 | # Access Key Rotation 2 | 3 | When rotating access keys be sure to follow these three steps in order. 4 | 5 | 1) Create new [full access key](./create-full-access-key.js) or [function access key](./create-function-access-key.js). 6 | 2) Confirm key creation 7 | 3) [Delete old access key](./delete-access-key.js) -------------------------------------------------------------------------------- /packages/cookbook/accounts/access-keys/create-full-access-key.ts: -------------------------------------------------------------------------------- 1 | import { 2 | addFullAccessKey, 3 | generateRandomKeyPair, 4 | getSignerFromKeystore, 5 | getTestnetRpcProvider, 6 | } from '@near-js/client'; 7 | import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 8 | import chalk from 'chalk'; 9 | import { join } from 'node:path'; 10 | import { homedir } from 'node:os'; 11 | 12 | 13 | export default async function createFullAccessKey(accountId: string) { 14 | if (!accountId) { 15 | console.log(chalk`{red pnpm createFullAccessKey -- ACCOUNT_ID}`); 16 | return; 17 | } 18 | 19 | // initialize testnet RPC provider 20 | const rpcProvider = getTestnetRpcProvider(); 21 | // initialize the transaction signer using a pre-existing key for `accountId` 22 | const keystore = new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials')); 23 | const signer = await getSignerFromKeystore(accountId, 'testnet', keystore); 24 | const previousKey = await signer.getPublicKey(); 25 | 26 | // create a new key from random data 27 | const keyPair = generateRandomKeyPair('ed25519'); 28 | 29 | // add the generated key to the account as a Full Access Key (FAK) 30 | await addFullAccessKey({ 31 | account: accountId, 32 | publicKey: keyPair.getPublicKey().toString(), 33 | deps: { 34 | rpcProvider, 35 | signer, 36 | }, 37 | }); 38 | 39 | // overwrite the key used to sign the transaction 40 | // above with the new FAK in the key store 41 | await keystore.setKey('testnet', accountId, keyPair); 42 | 43 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 44 | console.log(chalk`{bold.green RESULTS} {white Added new full access key and set in keystore}`); 45 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 46 | console.log(chalk`{bold.white Previous Key} {white |} {bold.yellow ${previousKey.toString()}}`); 47 | console.log(chalk`{bold.white New Key} {white |} {bold.yellow ${keyPair.getPublicKey().toString()}}`); 48 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 49 | } 50 | -------------------------------------------------------------------------------- /packages/cookbook/accounts/access-keys/create-function-access-key.ts: -------------------------------------------------------------------------------- 1 | import { 2 | addFunctionCallAccessKey, 3 | generateRandomKeyPair, 4 | getSignerFromKeystore, 5 | getTestnetRpcProvider, 6 | } from '@near-js/client'; 7 | import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 8 | import chalk from 'chalk'; 9 | import { join } from 'node:path'; 10 | import { homedir } from 'node:os'; 11 | 12 | const CONTRACT_NAME = 'example-account.testnet'; 13 | const METHOD_NAMES = ['example_method']; 14 | 15 | export default async function createFunctionCallAccessKey(accountId: string, contract = CONTRACT_NAME, methods = METHOD_NAMES) { 16 | if (!accountId) { 17 | console.log(chalk`{red pnpm createFunctionCallAccessKey -- ACCOUNT_ID}`); 18 | return; 19 | } 20 | 21 | // initialize testnet RPC provider 22 | const rpcProvider = getTestnetRpcProvider(); 23 | // initialize the transaction signer using a pre-existing key for ACCOUNT_ID 24 | const signer = await getSignerFromKeystore(accountId, 'testnet', new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials'))); 25 | 26 | // create a new key from random data 27 | const keyPair = generateRandomKeyPair('ed25519'); 28 | 29 | // add the generated key to the account as a Function Call Access Key 30 | await addFunctionCallAccessKey({ 31 | account: accountId, 32 | publicKey: keyPair.getPublicKey().toString(), 33 | contract, 34 | methodNames: methods, 35 | allowance: 2500000000000n, 36 | deps: { 37 | rpcProvider, 38 | signer, 39 | }, 40 | }); 41 | 42 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 43 | console.log(chalk`{bold.green RESULTS} {white Added new function call access key}` ); 44 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 45 | console.log(chalk`{bold.white New Key} {white |} {bold.yellow ${keyPair.getPublicKey().toString()}} {white |} {bold.yellow ${contract}} {white |} {bold.yellow ${methods}}`); 46 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 47 | } 48 | -------------------------------------------------------------------------------- /packages/cookbook/accounts/access-keys/delete-access-key.ts: -------------------------------------------------------------------------------- 1 | import { 2 | deleteAccessKey, 3 | getSignerFromKeystore, 4 | getTestnetRpcProvider, 5 | parseKeyPair, 6 | } from '@near-js/client'; 7 | import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 8 | import { type KeyPairString } from '@near-js/crypto'; 9 | import chalk from 'chalk'; 10 | import { join } from 'node:path'; 11 | import { homedir } from 'node:os'; 12 | 13 | export default async function deleteAccessKeyCookbook(accountId: string, publicKey: string) { 14 | if (!accountId) { 15 | console.log(chalk`{red pnpm deleteAccessKey -- ACCOUNT_ID [PUBLIC_KEY]}`); 16 | return; 17 | } 18 | 19 | // initialize testnet RPC provider 20 | const rpcProvider = getTestnetRpcProvider(); 21 | // initialize the transaction signer using a pre-existing key for ACCOUNT_ID 22 | const signer = await getSignerFromKeystore(accountId, 'testnet', new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials'))); 23 | 24 | // parse the target key from its string representation 25 | const keyPair = parseKeyPair(publicKey as KeyPairString); 26 | 27 | // add the generated key to the account as a Function Call Access Key 28 | await deleteAccessKey({ 29 | account: accountId, 30 | publicKey: keyPair.getPublicKey().toString(), 31 | deps: { 32 | rpcProvider, 33 | signer, 34 | }, 35 | }); 36 | 37 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 38 | console.log(chalk`{bold.green RESULTS} {white Deleted access key}` ); 39 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 40 | console.log(chalk`{bold.white Deleted Key} {white |} {bold.yellow ${keyPair.getPublicKey().toString()}}`); 41 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 42 | } 43 | -------------------------------------------------------------------------------- /packages/cookbook/accounts/create-funded-testnet-account.ts: -------------------------------------------------------------------------------- 1 | import { 2 | createFundedTestnetAccount, 3 | generateRandomKeyPair, 4 | } from '@near-js/client'; 5 | import chalk from 'chalk'; 6 | import { join } from 'node:path'; 7 | import { homedir } from 'node:os'; 8 | import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 9 | 10 | export default async function createFundedTestnetAccountCookbook(accountId: string) { 11 | if (!accountId) { 12 | console.log(chalk`{red pnpm createFundedTestnetAccount -- ACCOUNT_ID}`); 13 | return; 14 | } 15 | 16 | // initialize the transaction signer using a pre-existing key for ACCOUNT_ID 17 | const keystore = new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials')); 18 | 19 | // create new keypair and persist it to filesystem keystore 20 | const keyPair = generateRandomKeyPair('ed25519'); 21 | await keystore.setKey('testnet', accountId, keyPair); 22 | 23 | // call funded testnet creation endpoint 24 | await createFundedTestnetAccount({ 25 | newAccount: accountId, 26 | newPublicKey: keyPair.getPublicKey().toString(), 27 | }); 28 | 29 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 30 | console.log(chalk`{bold.green RESULTS} {white Created funded testnet account}` ); 31 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 32 | console.log(chalk`{bold.white New Account} {white |} {bold.yellow ${accountId}} {white |} {bold.yellow ${keyPair.getPublicKey().toString()}}`); 33 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 34 | } 35 | -------------------------------------------------------------------------------- /packages/cookbook/accounts/create-mainnet-account.ts: -------------------------------------------------------------------------------- 1 | import { 2 | createTopLevelAccount, 3 | generateRandomKeyPair, 4 | getMainnetRpcProvider, 5 | getSignerFromKeystore, 6 | } from '@near-js/client'; 7 | import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 8 | import chalk from 'chalk'; 9 | import { join } from 'node:path'; 10 | import { homedir } from 'node:os'; 11 | 12 | export default async function createMainnetAccountCookbook(accountId: string, newAccountId?: string) { 13 | if (!accountId) { 14 | console.log(chalk`{red pnpm createMainnetAccount -- CREATOR_ACCOUNT_ID [NEW_ACCOUNT_ID]}`); 15 | return; 16 | } 17 | 18 | // initialize testnet RPC provider 19 | const rpcProvider = getMainnetRpcProvider(); 20 | // initialize the transaction signer using a pre-existing key for `accountId` 21 | const keystore = new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials')); 22 | const signer = await getSignerFromKeystore(accountId, 'mainnet', keystore); 23 | 24 | // create a new key from random data 25 | const keyPair = generateRandomKeyPair('ed25519'); 26 | 27 | const newAccount = newAccountId || `${new Date().valueOf()}-${Math.ceil(Math.random() * 10e12)}.near`; 28 | 29 | await createTopLevelAccount({ 30 | account: accountId, 31 | contract: 'mainnet', 32 | initialBalance: 100n, 33 | newAccount, 34 | newPublicKey: keyPair.getPublicKey().toString(), 35 | deps: { 36 | rpcProvider, 37 | signer, 38 | }, 39 | }); 40 | 41 | // save new account in plaintext filesystem keystore 42 | await keystore.setKey('mainnet', accountId, keyPair); 43 | 44 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 45 | console.log(chalk`{bold.green RESULTS} {white Created mainnet account}`); 46 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 47 | console.log(chalk`{bold.white New Account} {white |} {bold.yellow ${accountId}} {white |} {bold.yellow ${keyPair.getPublicKey().toString()}}`); 48 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 49 | } 50 | -------------------------------------------------------------------------------- /packages/cookbook/accounts/create-testnet-account.ts: -------------------------------------------------------------------------------- 1 | import { 2 | createTopLevelAccount, 3 | generateRandomKeyPair, 4 | getSignerFromKeystore, 5 | getTestnetRpcProvider, 6 | } from '@near-js/client'; 7 | import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 8 | import chalk from 'chalk'; 9 | import { join } from 'node:path'; 10 | import { homedir } from 'node:os'; 11 | 12 | export default async function createTestnetAccountCookbook(accountId: string, newAccountId?: string) { 13 | if (!accountId) { 14 | console.log(chalk`{red pnpm createTestnetAccount -- CREATOR_ACCOUNT_ID [NEW_ACCOUNT_ID]}`); 15 | return; 16 | } 17 | 18 | // initialize testnet RPC provider 19 | const rpcProvider = getTestnetRpcProvider(); 20 | // initialize the transaction signer using a pre-existing key for `accountId` 21 | const keystore = new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials')); 22 | const signer = await getSignerFromKeystore(accountId, 'testnet', keystore); 23 | 24 | // create a new key from random data 25 | const keyPair = generateRandomKeyPair('ed25519'); 26 | 27 | const newAccount = newAccountId || `${new Date().valueOf()}-${Math.ceil(Math.random() * 10e12)}.testnet`; 28 | 29 | await createTopLevelAccount({ 30 | account: accountId, 31 | contract: 'testnet', 32 | initialBalance: 100n, 33 | newAccount, 34 | newPublicKey: keyPair.getPublicKey().toString(), 35 | deps: { 36 | rpcProvider, 37 | signer, 38 | }, 39 | }); 40 | 41 | // save new account in plaintext filesystem keystore 42 | await keystore.setKey('testnet', accountId, keyPair); 43 | 44 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 45 | console.log(chalk`{bold.green RESULTS} {white Created testnet account}`); 46 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 47 | console.log(chalk`{bold.white New Account} {white |} {bold.yellow ${accountId}} {white |} {bold.yellow ${keyPair.getPublicKey().toString()}}`); 48 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 49 | } 50 | -------------------------------------------------------------------------------- /packages/cookbook/accounts/index.ts: -------------------------------------------------------------------------------- 1 | export * from './access-keys/create-full-access-key'; 2 | export * from './access-keys/create-function-access-key'; 3 | export * from './access-keys/delete-access-key'; 4 | export * from './create-funded-testnet-account'; 5 | export * from './create-mainnet-account'; 6 | export * from './create-testnet-account'; -------------------------------------------------------------------------------- /packages/cookbook/api-keys/backup-provider.js: -------------------------------------------------------------------------------- 1 | // demonstrates how to use multiple providers with different API-KEYs 2 | const { providers } = require('near-api-js'); 3 | 4 | const RPC_API_ENDPOINT_1 = ''; 5 | const API_KEY_1 = ''; 6 | 7 | const RPC_API_ENDPOINT_2 = ''; 8 | const API_KEY_2 = ''; 9 | 10 | const jsonProviders = [ 11 | new providers.JsonRpcProvider({ 12 | url: RPC_API_ENDPOINT_1, 13 | headers: { 'x-api-key': API_KEY_1 }, 14 | }), 15 | new providers.JsonRpcProvider({ 16 | url: RPC_API_ENDPOINT_2, 17 | headers: { 'x-api-key': API_KEY_2 }, 18 | }), 19 | ]; 20 | const provider = new providers.FailoverRpcProvider(jsonProviders); 21 | 22 | getNetworkStatus(); 23 | 24 | async function getNetworkStatus() { 25 | const result = await provider.status(); 26 | console.log(result); 27 | } 28 | -------------------------------------------------------------------------------- /packages/cookbook/api-keys/near-connection.js: -------------------------------------------------------------------------------- 1 | // demonstrates how to use API-KEY with 'connect' function. 2 | const { connect, keyStores } = require("near-api-js"); 3 | const path = require("path"); 4 | 5 | const homedir = require("os").homedir(); 6 | const CREDENTIALS_DIR = ".near-credentials"; 7 | const credentialsPath = path.join(homedir, CREDENTIALS_DIR); 8 | const keyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath); 9 | 10 | const RPC_API_ENDPOINT = ''; 11 | const API_KEY = ''; 12 | 13 | const ACCOUNT_ID = ''; 14 | 15 | const config = { 16 | networkId: 'testnet', 17 | keyStore, 18 | nodeUrl: RPC_API_ENDPOINT, 19 | headers: { 'x-api-key': API_KEY }, 20 | }; 21 | 22 | async function getState(accountId) { 23 | const near = await connect(config); 24 | const account = await near.account(accountId); 25 | const state = await account.state(); 26 | console.log(state); 27 | } 28 | 29 | getState(ACCOUNT_ID); -------------------------------------------------------------------------------- /packages/cookbook/api-keys/provider-example.js: -------------------------------------------------------------------------------- 1 | // demonstrates how to use API-KEY with provider 2 | const { providers } = require("near-api-js"); 3 | 4 | const RPC_API_ENDPOINT = ''; 5 | const API_KEY = ''; 6 | 7 | const provider = new providers.JsonRpcProvider({ 8 | url: RPC_API_ENDPOINT, 9 | headers: { 'x-api-key': API_KEY }, 10 | }); 11 | 12 | getNetworkStatus(); 13 | 14 | async function getNetworkStatus() { 15 | const result = await provider.status(); 16 | console.log(result); 17 | } -------------------------------------------------------------------------------- /packages/cookbook/index.ts: -------------------------------------------------------------------------------- 1 | export * from './accounts'; 2 | export * from './transactions'; 3 | export * from './utils'; 4 | -------------------------------------------------------------------------------- /packages/cookbook/transactions/get-tx-status.ts: -------------------------------------------------------------------------------- 1 | import { 2 | getTestnetRpcArchivalProvider, 3 | } from '@near-js/client'; 4 | 5 | const TX_HASH = '9av2U6cova7LZPA9NPij6CTUrpBbgPG6LKVkyhcCqtk3'; 6 | // account ID associated with the transaction 7 | const ACCOUNT_ID = 'sender.testnet'; 8 | 9 | export default async function getTransactionStatus(accountId: string = ACCOUNT_ID, transactionHash: string = TX_HASH) { 10 | // initialize testnet RPC provider 11 | const rpcProvider = getTestnetRpcArchivalProvider(); 12 | 13 | const result = await rpcProvider.viewTransactionStatusWithReceipts(transactionHash, accountId, 'FINAL'); 14 | 15 | console.log(JSON.stringify(result, null, 2)); 16 | } 17 | -------------------------------------------------------------------------------- /packages/cookbook/transactions/index.ts: -------------------------------------------------------------------------------- 1 | export * from './batch-transactions'; 2 | export * from './get-tx-status'; 3 | export * from './meta-transaction'; 4 | export * from './meta-transaction-relayer'; 5 | export * from './traverse-blocks'; 6 | -------------------------------------------------------------------------------- /packages/cookbook/transactions/meta-transaction-relayer.ts: -------------------------------------------------------------------------------- 1 | import { 2 | getSignerFromKeystore, 3 | getTestnetRpcProvider, 4 | SignedTransactionComposer, 5 | } from '@near-js/client'; 6 | import { encodeSignedDelegate } from '@near-js/transactions'; 7 | import chalk from 'chalk'; 8 | import { join } from 'node:path'; 9 | import { homedir } from 'node:os'; 10 | import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 11 | 12 | /** 13 | * Submit a transaction to a relayer 14 | * @param signerAccountId account requesting the transaction's execution 15 | * @param receiverAccountId recipient of the transaction 16 | * @param relayerUrl URL processing relayer requests 17 | */ 18 | export default async function sendMetaTransactionViaRelayer(signerAccountId: string, receiverAccountId: string, relayerUrl: string) { 19 | if (!signerAccountId || !receiverAccountId) { 20 | console.log(chalk`{red pnpm metaTransaction -- SENDER_ACCOUNT_ID RECEIVER_ACCOUNT_ID RELAYER_URL}`); 21 | return; 22 | } 23 | 24 | // initialize testnet RPC provider 25 | const rpcProvider = getTestnetRpcProvider(); 26 | // initialize the transaction signer using a pre-existing key for `accountId` 27 | const signer = await getSignerFromKeystore(signerAccountId, 'testnet', new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials'))); 28 | 29 | const signedDelegate = await SignedTransactionComposer.init({ 30 | sender: signerAccountId, 31 | receiver: receiverAccountId, 32 | deps: { 33 | rpcProvider, 34 | signer, 35 | }, 36 | }) 37 | .transfer(100n) 38 | .toSignedDelegateAction({ blockHeightTtl: 60n }); 39 | 40 | // @ts-ignore global 41 | const res = await fetch(relayerUrl, { 42 | method: 'POST', 43 | body: JSON.stringify(Array.from(encodeSignedDelegate(signedDelegate))), 44 | }); 45 | console.log(await res.json()); 46 | } 47 | -------------------------------------------------------------------------------- /packages/cookbook/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "noEmit": true, 5 | "moduleResolution": "node" 6 | }, 7 | "files": [ 8 | "index.ts" 9 | ] 10 | } -------------------------------------------------------------------------------- /packages/cookbook/utils/check-account-existence.ts: -------------------------------------------------------------------------------- 1 | import { 2 | getAccountState, 3 | getTestnetRpcProvider, 4 | } from '@near-js/client'; 5 | 6 | export default async function accountExists() { 7 | const isRegisteredAccount = async (account: string) => { 8 | try { 9 | await getAccountState({ 10 | account, 11 | deps: { 12 | rpcProvider: getTestnetRpcProvider(), 13 | }, 14 | }); 15 | } catch (e) { 16 | if (e.type === 'AccountDoesNotExist') { 17 | return false; 18 | } 19 | } 20 | 21 | return true; 22 | }; 23 | 24 | for (const account of ['does-not-exist.mike.testnet', 'mike.testnet']) { 25 | const succeeded = await isRegisteredAccount(account); 26 | console.log(succeeded ? `The account ${account} exists.` : `There is no account ${account}.`); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /packages/cookbook/utils/deploy-contract.ts: -------------------------------------------------------------------------------- 1 | import { 2 | deployContract, 3 | getSignerFromKeystore, 4 | getTestnetRpcProvider, 5 | } from '@near-js/client'; 6 | import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 7 | import chalk from 'chalk'; 8 | import { join } from 'node:path'; 9 | import { homedir } from 'node:os'; 10 | import * as fs from 'node:fs/promises'; 11 | 12 | const WASM_PATH = join(__dirname, '/wasm-files/status_message.wasm'); 13 | 14 | export default async function deployContractCookbook(accountId: string, wasmPath: string = WASM_PATH) { 15 | if (!accountId) { 16 | console.log(chalk`{red pnpm deployContract -- ACCOUNT_ID [WASM_PATH]}`); 17 | return; 18 | } 19 | 20 | // initialize testnet RPC provider 21 | const rpcProvider = getTestnetRpcProvider(); 22 | // initialize the transaction signer using a pre-existing key for `accountId` 23 | const signer = await getSignerFromKeystore(accountId, 'testnet', new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials'))); 24 | 25 | await deployContract({ 26 | account: accountId, 27 | code: await fs.readFile(wasmPath), 28 | deps: { 29 | rpcProvider, 30 | signer, 31 | }, 32 | }); 33 | 34 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 35 | console.log(chalk`{bold.green RESULTS} {white Deployed contract}` ); 36 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 37 | console.log(chalk`{bold.white Contract Deployed} {white |} {bold.yellow WASM at ${wasmPath} deployed to ${accountId}}`); 38 | console.log(chalk`{white ------------------------------------------------------------------------ }`); 39 | } 40 | -------------------------------------------------------------------------------- /packages/cookbook/utils/get-state.ts: -------------------------------------------------------------------------------- 1 | import { getTestnetRpcProvider, view } from '@near-js/client'; 2 | 3 | export default async function getState() { 4 | // initialize testnet RPC provider 5 | const rpcProvider = getTestnetRpcProvider(); 6 | 7 | // call view method without parameters 8 | const result = await view({ 9 | account: 'guest-book.testnet', 10 | method: 'getMessages', 11 | deps: { rpcProvider }, 12 | }); 13 | 14 | console.log(result); 15 | } 16 | -------------------------------------------------------------------------------- /packages/cookbook/utils/index.ts: -------------------------------------------------------------------------------- 1 | export * from './calculate-gas'; 2 | export * from './check-account-existence'; 3 | export * from './deploy-contract'; 4 | export * from './get-state'; 5 | export * from './unwrap-near'; 6 | export * from './verify-signature'; 7 | export * from './wrap-near'; 8 | -------------------------------------------------------------------------------- /packages/cookbook/utils/verify-signature.ts: -------------------------------------------------------------------------------- 1 | import { getTestnetRpcProvider } from '@near-js/client'; 2 | import { KeyType, PublicKey } from '@near-js/crypto'; 3 | import { baseDecode } from '@near-js/utils'; 4 | 5 | const ACCOUNT_ID = 'gornt.testnet'; 6 | const TX_HASH = '4tMHzHU5p9dXc4WqopReNZ2TMJxZyu913zK4Fn9nMRoB'; 7 | 8 | export default async function verifySignature(accountId: string = ACCOUNT_ID, transactionHash: string = TX_HASH) { 9 | // initialize testnet RPC provider 10 | const rpcProvider = getTestnetRpcProvider(); 11 | 12 | const { transaction: { public_key, signature } } = await rpcProvider.viewTransactionStatus(transactionHash, accountId, 'FINAL'); 13 | 14 | const hashBytes = baseDecode(transactionHash); 15 | const publicKeyBytes = baseDecode(public_key.slice('ed25519:'.length)); 16 | const signatureBytes = baseDecode(signature.slice('ed25519:'.length)); 17 | const publicKey = new PublicKey({ keyType: KeyType.ED25519, data: publicKeyBytes }); 18 | 19 | const isVerified = publicKey.verify(hashBytes, signatureBytes); 20 | 21 | console.log(isVerified); 22 | } 23 | -------------------------------------------------------------------------------- /packages/cookbook/utils/wasm-files/staking_pool_factory.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/near/near-api-js/96b63f4d48815400e4422182bf8c98501995e8a6/packages/cookbook/utils/wasm-files/staking_pool_factory.wasm -------------------------------------------------------------------------------- /packages/cookbook/utils/wasm-files/status_message.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/near/near-api-js/96b63f4d48815400e4422182bf8c98501995e8a6/packages/cookbook/utils/wasm-files/status_message.wasm -------------------------------------------------------------------------------- /packages/crypto/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/crypto 2 | 3 | A collection of classes and types for working with cryptographic key pairs. 4 | 5 | ## Modules 6 | 7 | - [PublicKey](https://github.com/near/near-api-js/blob/master/packages/crypto/src/public_key.ts) representation of a public key capable of verifying signatures 8 | - [KeyPairBase](https://github.com/near/near-api-js/blob/master/packages/crypto/src/key_pair_base.ts) abstract class representing a key pair 9 | - [KeyPair](https://github.com/near/near-api-js/blob/master/packages/crypto/src/key_pair.ts) abstract extension of `KeyPairBase` with static methods for parsing and generating key pairs 10 | - [KeyPairEd25519](https://github.com/near/near-api-js/blob/master/packages/crypto/src/key_pair_ed25519.ts) implementation of `KeyPairBase` using [Ed25519](https://en.wikipedia.org/wiki/EdDSA#Ed25519) 11 | - [Constants](https://github.com/near/near-api-js/blob/master/packages/crypto/src/constants.ts) keypair-specific constants 12 | 13 | # License 14 | 15 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 16 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 17 | -------------------------------------------------------------------------------- /packages/crypto/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/crypto/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/crypto", 3 | "version": "2.0.2", 4 | "description": "Abstractions around NEAR-compatible elliptical curves and cryptographic keys", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 13 | "test": "jest", 14 | "check-exports": "attw --pack ." 15 | }, 16 | "keywords": [], 17 | "author": "", 18 | "license": "ISC", 19 | "dependencies": { 20 | "@near-js/types": "workspace:*", 21 | "@near-js/utils": "workspace:*", 22 | "@noble/curves": "1.8.1", 23 | "borsh": "1.0.0", 24 | "randombytes": "2.1.0", 25 | "secp256k1": "5.0.1" 26 | }, 27 | "devDependencies": { 28 | "@jest/globals": "^29.7.0", 29 | "@noble/hashes": "^1.7.1", 30 | "@types/node": "20.0.0", 31 | "build": "workspace:*", 32 | "jest": "29.7.0", 33 | "ts-jest": "29.2.6", 34 | "tsconfig": "workspace:*", 35 | "typescript": "5.4.5" 36 | }, 37 | "peerDependencies": { 38 | "@near-js/types": "^2.0.1", 39 | "@near-js/utils": "^2.0.1" 40 | }, 41 | "files": [ 42 | "lib" 43 | ], 44 | "exports": { 45 | ".": { 46 | "import": "./lib/esm/index.js", 47 | "require": "./lib/commonjs/index.cjs" 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /packages/crypto/src/constants.ts: -------------------------------------------------------------------------------- 1 | /** All supported key types */ 2 | export enum KeyType { 3 | ED25519 = 0, 4 | SECP256K1 = 1, 5 | } 6 | 7 | export const KeySize = { 8 | SECRET_KEY: 32, 9 | ED25519_PUBLIC_KEY: 32, 10 | SECP256k1_PUBLIC_KEY: 64, 11 | }; 12 | 13 | export type CurveType = 14 | 'ed25519' | 'ED25519' 15 | | 'secp256k1' | 'SECP256K1'; 16 | 17 | export type KeyPairString = `ed25519:${string}` | `secp256k1:${string}`; 18 | -------------------------------------------------------------------------------- /packages/crypto/src/index.ts: -------------------------------------------------------------------------------- 1 | export { CurveType, KeyPairString, KeyType } from './constants'; 2 | export { KeyPair } from './key_pair'; 3 | export { Signature } from './key_pair_base'; 4 | export { KeyPairEd25519 } from './key_pair_ed25519'; 5 | export { KeyPairSecp256k1 } from './key_pair_secp256k1'; 6 | export { PublicKey } from './public_key'; 7 | -------------------------------------------------------------------------------- /packages/crypto/src/key_pair.ts: -------------------------------------------------------------------------------- 1 | import { CurveType, KeyPairString } from './constants'; 2 | import { KeyPairBase } from './key_pair_base'; 3 | import { KeyPairEd25519 } from './key_pair_ed25519'; 4 | import { KeyPairSecp256k1 } from './key_pair_secp256k1'; 5 | 6 | export abstract class KeyPair extends KeyPairBase { 7 | /** 8 | * @param curve Name of elliptical curve, case-insensitive 9 | * @returns Random KeyPair based on the curve 10 | */ 11 | static fromRandom(curve: CurveType): KeyPair { 12 | switch (curve.toUpperCase()) { 13 | case 'ED25519': return KeyPairEd25519.fromRandom(); 14 | case 'SECP256K1': return KeyPairSecp256k1.fromRandom(); 15 | default: throw new Error(`Unknown curve ${curve}`); 16 | } 17 | } 18 | 19 | /** 20 | * Creates a key pair from an encoded key string. 21 | * @param encodedKey The encoded key string. 22 | * @returns {KeyPair} The key pair created from the encoded key string. 23 | */ 24 | static fromString(encodedKey: KeyPairString): KeyPair { 25 | const parts = encodedKey.split(':'); 26 | if (parts.length === 2) { 27 | switch (parts[0].toUpperCase()) { 28 | case 'ED25519': return new KeyPairEd25519(parts[1]); 29 | case 'SECP256K1': return new KeyPairSecp256k1(parts[1]); 30 | default: throw new Error(`Unknown curve: ${parts[0]}`); 31 | } 32 | } else { 33 | throw new Error('Invalid encoded key format, must be :'); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /packages/crypto/src/key_pair_base.ts: -------------------------------------------------------------------------------- 1 | import type { KeyPairString } from './constants'; 2 | import { PublicKey } from './public_key'; 3 | 4 | export interface Signature { 5 | signature: Uint8Array; 6 | publicKey: PublicKey; 7 | } 8 | 9 | export abstract class KeyPairBase { 10 | abstract sign(message: Uint8Array): Signature; 11 | abstract verify(message: Uint8Array, signature: Uint8Array): boolean; 12 | abstract toString(): KeyPairString; 13 | abstract getPublicKey(): PublicKey; 14 | } 15 | -------------------------------------------------------------------------------- /packages/crypto/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/crypto/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } -------------------------------------------------------------------------------- /packages/crypto/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: true, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: true, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/crypto/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/iframe-rpc/.eslintrc.yml: -------------------------------------------------------------------------------- 1 | env: 2 | es6: true 3 | node: true 4 | extends: 5 | - 'eslint:recommended' 6 | - 'plugin:@typescript-eslint/eslint-recommended' 7 | - 'plugin:@typescript-eslint/recommended' 8 | parser: '@typescript-eslint/parser' 9 | rules: 10 | no-inner-declarations: off 11 | indent: 12 | - error 13 | - 4 14 | - SwitchCase: 1 15 | '@typescript-eslint/no-explicit-any': off 16 | 17 | parserOptions: 18 | ecmaVersion: 2018 19 | sourceType: module 20 | -------------------------------------------------------------------------------- /packages/iframe-rpc/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @near-js/iframe-rpc 2 | 3 | ## 2.0.2 4 | 5 | ### Patch Changes 6 | 7 | - [#1559](https://github.com/near/near-api-js/pull/1559) [`59d3dc9`](https://github.com/near/near-api-js/commit/59d3dc9580be05662cb9a587e82359faccd69d1b) Thanks [@r-near](https://github.com/r-near)! - fix: ESM Module Resolution 8 | 9 | ## 2.0.1 10 | 11 | ### Patch Changes 12 | 13 | - [#1554](https://github.com/near/near-api-js/pull/1554) [`13f93eb`](https://github.com/near/near-api-js/commit/13f93ebdac497bb473364da66a493344d955b27f) Thanks [@denbite](https://github.com/denbite)! - Redeploy recent release as patch 14 | 15 | ## 2.0.0 16 | 17 | ## 0.1.0 18 | 19 | ### Minor Changes 20 | 21 | - [#1353](https://github.com/near/near-api-js/pull/1353) [`73690557`](https://github.com/near/near-api-js/commit/73690557c8e2a74386fca62f4ae123abe0651403) Thanks [@andy-haynes](https://github.com/andy-haynes)! - Update to Node.js 20 LTS & pnpm 9.4, modularize packages, simplify dependencies, and update tests 22 | 23 | **Breaking Changes** 24 | 25 | - `near-api-js@5.0.0` 26 | 27 | - The following functions are no longer exported: 28 | - `logWarning` 29 | - `fetchJson` 30 | - the unnamed wrapped `fetch` function exported from `setup-node-fetch.ts` 31 | - The browser bundle is no longer being built in version 5; for browser support please use modules 32 | 33 | - `@near-js/providers@1.0.0` 34 | 35 | - The following functions are no longer exported: 36 | - `fetchJson` 37 | 38 | - `@near-js/utils@1.0.0` 39 | - The following functions are no longer exported: 40 | - `logWarning` 41 | 42 | ## 0.0.2 43 | 44 | ### Patch Changes 45 | 46 | - [#1121](https://github.com/near/near-api-js/pull/1121) [`6a4cd9cc`](https://github.com/near/near-api-js/commit/6a4cd9cca0eb64e66b77e70af74b253da4c08535) Thanks [@MaximusHaximus](https://github.com/MaximusHaximus)! - Initial implementation - RPC via Iframes 47 | -------------------------------------------------------------------------------- /packages/iframe-rpc/README.md: -------------------------------------------------------------------------------- 1 | 2 | # IFrame RPC 3 | 4 | The `@near-js/iframe-rpc` package facilitates async RPC calls between cross-domain frames using the `postMessage` API. 5 | 6 | 7 | ## Installation and Usage 8 | 9 | Install [`@near-js/iframe-rpc`](https://www.npmjs.com/package/@near-js/iframe-rpc) package from the NPM registry. 10 | 11 | ```bash 12 | # Using Yarn 13 | yarn add @near-js/iframe-rpc 14 | 15 | # Using NPM. 16 | npm install @near-js/iframe-rpc 17 | ``` 18 | 19 | In your app: 20 | ```ts 21 | import { IFrameRPC } from "@near-js/iframe-rpc"; 22 | const rpc = new IFrameRPC({...}) 23 | await rpc.isReady 24 | // Call away! 25 | ``` 26 | 27 | ## License 28 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). See [LICENSE-MIT](LICENSE-MIT) and [LICENSE-APACHE](LICENSE-APACHE) for details. 29 | -------------------------------------------------------------------------------- /packages/iframe-rpc/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/iframe-rpc", 3 | "version": "2.0.2", 4 | "description": "IFrame RPC client/server implementation", 5 | "main": "lib/esm/index.js", 6 | "types": "lib/esm/index.d.ts", 7 | "type": "module", 8 | "scripts": { 9 | "build": "tsup", 10 | "check-exports": "attw --pack ." 11 | }, 12 | "keywords": [], 13 | "author": "Pagoda", 14 | "license": "ISC", 15 | "dependencies": { 16 | "events": "3.3.0" 17 | }, 18 | "devDependencies": { 19 | "@types/node": "18.11.18", 20 | "build": "workspace:*" 21 | }, 22 | "exports": { 23 | ".": { 24 | "import": "./lib/esm/index.js", 25 | "require": "./lib/commonjs/index.cjs" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /packages/iframe-rpc/src/iframe-rpc-error.ts: -------------------------------------------------------------------------------- 1 | export class IFrameRPCError extends Error { 2 | constructor( 3 | override readonly message: string, 4 | public readonly code: number 5 | ) { 6 | super(`Error #${code}: ${message}`); 7 | } 8 | 9 | public toResponseError() { 10 | return { 11 | code: this.code, 12 | message: this.message, 13 | }; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /packages/iframe-rpc/src/index.ts: -------------------------------------------------------------------------------- 1 | // Portions derived from microsoft/mixer JS 2 | /*MIT License 3 | 4 | Copyright (c) Microsoft Corporation 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE.*/ 23 | 24 | export * from './iframe-rpc-error'; 25 | export * from './iframe-rpc'; 26 | export * from './types'; 27 | -------------------------------------------------------------------------------- /packages/iframe-rpc/src/types.ts: -------------------------------------------------------------------------------- 1 | export type RPCMessage = IRPCMethod | IRPCResponse; 2 | 3 | export interface IRPCMethod { 4 | type: 'method'; 5 | requesterId: string; 6 | id: number; 7 | method: string; 8 | params: T; 9 | } 10 | 11 | export interface IRPCResponse { 12 | type: 'response'; 13 | requesterId: string; 14 | id: number; 15 | result: T; 16 | error?: { 17 | code: number; 18 | message: string; 19 | }; 20 | } 21 | 22 | export function isRPCMessage(data: any): data is RPCMessage { 23 | return (data.type === 'method' || data.type === 'response') 24 | && typeof data.id === 'number' 25 | && typeof data.requesterId === 'string'; 26 | } 27 | 28 | export interface IMessageEvent { 29 | data: any; 30 | origin: string; 31 | } 32 | 33 | export interface IMessagePoster { 34 | postMessage(data: any, targetOrigin: string): void; 35 | } 36 | 37 | export interface IMessageReceiver { 38 | readMessages(callback: (ev: IMessageEvent) => void): () => void; 39 | } 40 | 41 | export const windowReceiver: IMessageReceiver = { 42 | readMessages(callback) { 43 | window.addEventListener('message', callback); 44 | 45 | // Unsubscribe handler for consumers to call to stop listening 46 | return () => window.removeEventListener('message', callback); 47 | }, 48 | }; 49 | -------------------------------------------------------------------------------- /packages/iframe-rpc/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/iframe-rpc/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/browser.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | "types": ["node"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/iframe-rpc/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/iframe-rpc/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/keystores-browser/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/keystores-browser 2 | 3 | A collection of classes for managing keys in a web browser execution context. 4 | 5 | ## Modules 6 | 7 | - [BrowserLocalStorageKeyStore](https://github.com/near/near-api-js/blob/master/packages/keystores-browser/src/browser_local_storage_key_store.ts) implementation of [KeyStore](https://github.com/near/near-api-js/blob/master/packages/keystores/src/keystore.ts) storing unencrypted keys in browser LocalStorage 8 | 9 | # License 10 | 11 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 12 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 13 | -------------------------------------------------------------------------------- /packages/keystores-browser/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true 10 | } 11 | }] 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/keystores-browser/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/keystores-browser", 3 | "version": "2.0.2", 4 | "description": "KeyStore implementation for working with keys in browser LocalStorage", 5 | "main": "lib/esm/index.js", 6 | "types": "./lib/esm/index.d.ts", 7 | "type": "module", 8 | "scripts": { 9 | "build": "tsup", 10 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 11 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 12 | "test": "jest", 13 | "check-exports": "attw --pack ." 14 | }, 15 | "keywords": [], 16 | "author": "", 17 | "license": "ISC", 18 | "dependencies": { 19 | "@near-js/crypto": "workspace:*", 20 | "@near-js/keystores": "workspace:*" 21 | }, 22 | "devDependencies": { 23 | "@jest/globals": "^29.7.0", 24 | "build": "workspace:*", 25 | "jest": "29.7.0", 26 | "localstorage-memory": "1.0.3", 27 | "ts-jest": "29.2.6", 28 | "tsconfig": "workspace:*", 29 | "typescript": "5.4.5" 30 | }, 31 | "files": [ 32 | "lib" 33 | ], 34 | "exports": { 35 | ".": { 36 | "import": "./lib/esm/index.js", 37 | "require": "./lib/commonjs/index.cjs" 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /packages/keystores-browser/src/index.ts: -------------------------------------------------------------------------------- 1 | export { BrowserLocalStorageKeyStore } from './browser_local_storage_key_store'; 2 | export { MultiContractBrowserLocalStorageKeyStore } from './multi_contract_browser_local_storage_key_store'; 3 | -------------------------------------------------------------------------------- /packages/keystores-browser/test/browser_keystore.test.ts: -------------------------------------------------------------------------------- 1 | import { beforeAll, describe } from '@jest/globals'; 2 | import LocalStorageMemory from 'localstorage-memory'; 3 | 4 | import { BrowserLocalStorageKeyStore, MultiContractBrowserLocalStorageKeyStore } from '../src'; 5 | import { shouldStoreAndRetrieveKeys } from './keystore_common'; 6 | 7 | describe('Browser keystore', () => { 8 | const ctx: any = {}; 9 | 10 | beforeAll(async () => { 11 | ctx.keyStore = new BrowserLocalStorageKeyStore(LocalStorageMemory); 12 | }); 13 | 14 | shouldStoreAndRetrieveKeys(ctx); 15 | }); 16 | 17 | describe('Browser multi keystore', () => { 18 | const ctx: any = {}; 19 | 20 | beforeAll(async () => { 21 | ctx.keyStore = new MultiContractBrowserLocalStorageKeyStore(LocalStorageMemory); 22 | }); 23 | 24 | shouldStoreAndRetrieveKeys(ctx); 25 | }); 26 | -------------------------------------------------------------------------------- /packages/keystores-browser/test/multi_contract_browser_keystore_common.test.ts: -------------------------------------------------------------------------------- 1 | import { beforeEach, expect, test } from '@jest/globals'; 2 | import { KeyPairEd25519 } from '@near-js/crypto'; 3 | import LocalStorageMemory from 'localstorage-memory'; 4 | 5 | import { MultiContractBrowserLocalStorageKeyStore } from '../src'; 6 | 7 | const NETWORK_ID = 'networkid'; 8 | const ACCOUNT_ID = 'accountid'; 9 | const CONTRACT_ID = 'contractid'; 10 | const KEYPAIR = new KeyPairEd25519('2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw'); 11 | 12 | const ctx = { 13 | keyStore: new MultiContractBrowserLocalStorageKeyStore(LocalStorageMemory) 14 | }; 15 | 16 | beforeEach(async () => { 17 | await ctx.keyStore.clear(); 18 | await ctx.keyStore.setKey(NETWORK_ID, ACCOUNT_ID, KEYPAIR, CONTRACT_ID); 19 | }); 20 | 21 | test('Get not-existing account', async () => { 22 | expect(await ctx.keyStore.getKey('somenetwork', 'someaccount', 'somecontract')).toBeNull(); 23 | }); 24 | 25 | test('Get account id from a network with single key', async () => { 26 | const key = await ctx.keyStore.getKey(NETWORK_ID, ACCOUNT_ID, CONTRACT_ID); 27 | expect(key).toEqual(KEYPAIR); 28 | }); 29 | 30 | test('Get contracts', async () => { 31 | const contracts = await ctx.keyStore.getContracts(NETWORK_ID, ACCOUNT_ID); 32 | expect(contracts).toEqual([CONTRACT_ID]); 33 | }); 34 | 35 | test('Add two contracts to account and retrieve them', async () => { 36 | const networkId = 'network'; 37 | const accountId = 'account'; 38 | const contract1 = 'contract1'; 39 | const contract2 = 'contract2'; 40 | const key1Expected = KeyPairEd25519.fromRandom(); 41 | const key2Expected = KeyPairEd25519.fromRandom(); 42 | await ctx.keyStore.setKey(networkId, accountId, key1Expected, contract1); 43 | await ctx.keyStore.setKey(networkId, accountId, key2Expected, contract2); 44 | const key1 = await ctx.keyStore.getKey(networkId, accountId, contract1); 45 | const key2 = await ctx.keyStore.getKey(networkId, accountId, contract2); 46 | expect(key1).toEqual(key1Expected); 47 | expect(key2).toEqual(key2Expected); 48 | const contractIds = await ctx.keyStore.getContracts(networkId, accountId); 49 | expect(contractIds).toEqual([contract1, contract2]); 50 | }); 51 | -------------------------------------------------------------------------------- /packages/keystores-browser/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/keystores-browser/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/browser.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/keystores-browser/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/keystores-browser/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/keystores-node/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/keystores-node 2 | 3 | A collection of classes and functions for managing keys in NodeJS execution context. 4 | 5 | ## Modules 6 | 7 | - [UnencryptedFileSystemKeyStore](https://github.com/near/near-api-js/blob/master/packages/keystores-node/src/unencrypted_file_system_keystore.ts) implementation of [KeyStore](https://github.com/near/near-api-js/blob/master/packages/keystores/src/keystore.ts) storing unencrypted keys on the local filesystem 8 | 9 | # License 10 | 11 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 12 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 13 | -------------------------------------------------------------------------------- /packages/keystores-node/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/keystores-node/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/keystores-node", 3 | "version": "2.0.2", 4 | "description": "KeyStore implementation for working with keys in the local filesystem", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 13 | "test": "jest", 14 | "check-exports": "attw --pack ." 15 | }, 16 | "keywords": [], 17 | "author": "", 18 | "license": "ISC", 19 | "dependencies": { 20 | "@near-js/crypto": "workspace:*", 21 | "@near-js/keystores": "workspace:*" 22 | }, 23 | "devDependencies": { 24 | "@jest/globals": "^29.7.0", 25 | "@types/node": "20.0.0", 26 | "build": "workspace:*", 27 | "jest": "29.7.0", 28 | "rimraf": "^6.0.1", 29 | "ts-jest": "29.2.6", 30 | "tsconfig": "workspace:*", 31 | "typescript": "5.4.5" 32 | }, 33 | "peerDependencies": { 34 | "@near-js/crypto": "^2.0.1", 35 | "@near-js/keystores": "^2.0.1" 36 | }, 37 | "files": [ 38 | "lib" 39 | ], 40 | "exports": { 41 | ".": { 42 | "import": "./lib/esm/index.js", 43 | "require": "./lib/commonjs/index.cjs" 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /packages/keystores-node/src/index.ts: -------------------------------------------------------------------------------- 1 | export { readKeyFile, UnencryptedFileSystemKeyStore } from './unencrypted_file_system_keystore'; 2 | -------------------------------------------------------------------------------- /packages/keystores-node/test/unencrypted_file_system_keystore.test.ts: -------------------------------------------------------------------------------- 1 | import { beforeAll, describe, expect, it } from '@jest/globals'; 2 | import { KeyPairEd25519 } from '@near-js/crypto'; 3 | import * as fs from 'fs'; 4 | import * as path from 'path'; 5 | import { rimraf } from 'rimraf'; 6 | 7 | import { UnencryptedFileSystemKeyStore } from '../src'; 8 | import { shouldStoreAndRetriveKeys } from './keystore_common'; 9 | 10 | const KEYSTORE_PATH = '../../test-keys'; 11 | 12 | describe('Unencrypted file system keystore', () => { 13 | const ctx: { keyStore?: any } = {}; 14 | 15 | beforeAll(() => { 16 | rimraf.sync(KEYSTORE_PATH); 17 | try { 18 | fs.mkdirSync(KEYSTORE_PATH, { recursive: true }); 19 | } catch (err) { 20 | if (err.code !== 'EEXIST') throw err; 21 | } 22 | ctx.keyStore = new UnencryptedFileSystemKeyStore(KEYSTORE_PATH); 23 | }); 24 | 25 | shouldStoreAndRetriveKeys(ctx); 26 | 27 | it('test path resolve', async() => { 28 | expect(ctx.keyStore.keyDir).toEqual(path.join(process.cwd(), KEYSTORE_PATH)); 29 | }); 30 | 31 | it('test public key exists', async () => { 32 | const key1 = KeyPairEd25519.fromRandom(); 33 | await ctx.keyStore.setKey('network', 'account', key1); 34 | const keyFilePath = ctx.keyStore.getKeyFilePath('network', 'account'); 35 | const content = fs.readFileSync(keyFilePath).toString(); 36 | const accountInfo = JSON.parse(content.toString()); 37 | expect(accountInfo.public_key).toEqual(key1.getPublicKey().toString()); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /packages/keystores-node/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/keystores-node/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/keystores-node/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/keystores-node/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/keystores/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/keystores 2 | 3 | A collection of classes for managing NEAR-compatible cryptographic keys. 4 | 5 | ## Modules 6 | 7 | - [KeyStore](https://github.com/near/near-api-js/blob/master/packages/keystores/src/keystore.ts) abstract class for managing account keys 8 | - [InMemoryKeyStore](https://github.com/near/near-api-js/blob/master/packages/keystores/src/in_memory_key_store.ts) implementation of `KeyStore` using an in-memory data structure local to the instance 9 | - [MergeKeyStore](https://github.com/near/near-api-js/blob/master/packages/keystores/src/merge_key_store.ts) implementation of `KeyStore` aggregating multiple `KeyStore` implementations 10 | 11 | # License 12 | 13 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 14 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 15 | -------------------------------------------------------------------------------- /packages/keystores/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/keystores/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/keystores", 3 | "version": "2.0.2", 4 | "description": "Key storage and management implementations", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "clean": "rimraf lib/", 12 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 13 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 14 | "test": "jest", 15 | "check-exports": "attw --pack ." 16 | }, 17 | "keywords": [], 18 | "author": "", 19 | "license": "ISC", 20 | "dependencies": { 21 | "@near-js/crypto": "workspace:*", 22 | "@near-js/types": "workspace:*" 23 | }, 24 | "devDependencies": { 25 | "@jest/globals": "^29.7.0", 26 | "@types/node": "20.0.0", 27 | "build": "workspace:*", 28 | "jest": "29.7.0", 29 | "ts-jest": "29.2.6", 30 | "tsconfig": "workspace:*", 31 | "typescript": "5.4.5" 32 | }, 33 | "peerDependencies": { 34 | "@near-js/crypto": "^2.0.1", 35 | "@near-js/types": "^2.0.1" 36 | }, 37 | "files": [ 38 | "lib" 39 | ], 40 | "exports": { 41 | ".": { 42 | "import": "./lib/esm/index.js", 43 | "require": "./lib/commonjs/index.cjs" 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /packages/keystores/src/index.ts: -------------------------------------------------------------------------------- 1 | export { InMemoryKeyStore } from './in_memory_key_store'; 2 | export { KeyStore } from './keystore'; 3 | export { MergeKeyStore } from './merge_key_store'; 4 | export { MultiContractKeyStore } from './multi_contract_keystore'; 5 | -------------------------------------------------------------------------------- /packages/keystores/src/keystore.ts: -------------------------------------------------------------------------------- 1 | import { KeyPair } from '@near-js/crypto'; 2 | 3 | /** 4 | * KeyStores are used by {@link "@near-js/signers".Signer} to sign transactions. 5 | * 6 | */ 7 | export abstract class KeyStore { 8 | abstract setKey(networkId: string, accountId: string, keyPair: KeyPair): Promise; 9 | abstract getKey(networkId: string, accountId: string): Promise; 10 | abstract removeKey(networkId: string, accountId: string): Promise; 11 | abstract clear(): Promise; 12 | abstract getNetworks(): Promise; 13 | abstract getAccounts(networkId: string): Promise; 14 | } 15 | -------------------------------------------------------------------------------- /packages/keystores/src/multi_contract_keystore.ts: -------------------------------------------------------------------------------- 1 | import { KeyPair } from '@near-js/crypto'; 2 | 3 | /** 4 | * KeyStores are passed to {@link near!Near} via {@link near!NearConfig} 5 | * and are used by the {@link signer!InMemorySigner} to sign transactions. 6 | * 7 | * @see {@link connect} 8 | */ 9 | export abstract class MultiContractKeyStore { 10 | abstract setKey(networkId: string, accountId: string, keyPair: KeyPair, contractId: string): Promise; 11 | abstract getKey(networkId: string, accountId: string, contractId: string): Promise; 12 | abstract removeKey(networkId: string, accountId: string, contractId: string): Promise; 13 | abstract clear(): Promise; 14 | abstract getNetworks(): Promise; 15 | abstract getAccounts(networkId: string): Promise; 16 | abstract getContracts(networkId: string, accountId: string): Promise; 17 | } 18 | -------------------------------------------------------------------------------- /packages/keystores/test/in_memory_keystore.test.ts: -------------------------------------------------------------------------------- 1 | import { beforeAll, describe } from '@jest/globals'; 2 | import { shouldStoreAndRetrieveKeys } from './keystore_common'; 3 | import { InMemoryKeyStore } from '../src'; 4 | 5 | describe('In-memory keystore', () => { 6 | const ctx: any = {}; 7 | 8 | beforeAll(async () => { 9 | ctx.keyStore = new InMemoryKeyStore(); 10 | }); 11 | 12 | shouldStoreAndRetrieveKeys(ctx); 13 | }); 14 | -------------------------------------------------------------------------------- /packages/keystores/test/merge_keystore.test.ts: -------------------------------------------------------------------------------- 1 | import { beforeAll, describe, expect, it } from '@jest/globals'; 2 | import { KeyPairEd25519 } from '@near-js/crypto'; 3 | 4 | import { InMemoryKeyStore, MergeKeyStore } from '../src'; 5 | import { shouldStoreAndRetrieveKeys } from './keystore_common'; 6 | 7 | describe('Merge keystore', () => { 8 | const ctx: any = {}; 9 | 10 | beforeAll(async () => { 11 | ctx.stores = [new InMemoryKeyStore(), new InMemoryKeyStore()]; 12 | ctx.keyStore = new MergeKeyStore(ctx.stores); 13 | }); 14 | 15 | it('looks up key from fallback key store if needed', async () => { 16 | const key1 = KeyPairEd25519.fromRandom(); 17 | await ctx.stores[1].setKey('network', 'account', key1); 18 | expect(await ctx.keyStore.getKey('network', 'account')).toEqual(key1); 19 | }); 20 | 21 | it('looks up key in proper order', async () => { 22 | const key1 = KeyPairEd25519.fromRandom(); 23 | const key2 = KeyPairEd25519.fromRandom(); 24 | await ctx.stores[0].setKey('network', 'account', key1); 25 | await ctx.stores[1].setKey('network', 'account', key2); 26 | expect(await ctx.keyStore.getKey('network', 'account')).toEqual(key1); 27 | }); 28 | 29 | it('sets keys only in first key store', async () => { 30 | const key1 = KeyPairEd25519.fromRandom(); 31 | await ctx.keyStore.setKey('network', 'account', key1); 32 | expect(await ctx.stores[0].getAccounts('network')).toHaveLength(1); 33 | expect(await ctx.stores[1].getAccounts('network')).toHaveLength(0); 34 | }); 35 | 36 | shouldStoreAndRetrieveKeys(ctx); 37 | }); 38 | -------------------------------------------------------------------------------- /packages/keystores/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/keystores/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/keystores/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/keystores/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/near-api-js/.eslintignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | nearcore/ 3 | protos.js -------------------------------------------------------------------------------- /packages/near-api-js/.gitattributes: -------------------------------------------------------------------------------- 1 | package-lock.json linguist-generated=true -diff 2 | yarn.lock linguist-generated=true -diff 3 | docs/**/*.md linguist-generated=true -diff 4 | lib/**/*.js linguist-generated=true -diff 5 | lib/**/*.d.ts linguist-generated=true -diff 6 | -------------------------------------------------------------------------------- /packages/near-api-js/README.md: -------------------------------------------------------------------------------- 1 | # NEAR JavaScript API 2 | 3 | NEAR JavaScript API is a complete library to interact with the NEAR blockchain. You can use it in the browser, or in Node.js runtime. 4 | 5 | ## Documentation 6 | 7 | - [Learn how to use](https://docs.near.org/tools/near-api-js/quick-reference) the library in your project 8 | 9 | - Read the [TypeDoc API](https://near.github.io/near-api-js/) documentation 10 | 11 | - [Cookbook](https://github.com/near/near-api-js/blob/master/packages/cookbook/README.md) with common use cases 12 | 13 | - To quickly get started with integrating NEAR in a _web browser_, read our [Web Frontend integration](https://docs.near.org/develop/integrate/frontend) article. 14 | 15 | # License 16 | 17 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 18 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 19 | -------------------------------------------------------------------------------- /packages/near-api-js/jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: 'ts-jest', 3 | testEnvironment: 'node', 4 | collectCoverage: true 5 | }; 6 | -------------------------------------------------------------------------------- /packages/near-api-js/src/.eslintrc.yml: -------------------------------------------------------------------------------- 1 | env: 2 | es6: true 3 | node: true 4 | extends: 5 | - 'eslint:recommended' 6 | - 'plugin:@typescript-eslint/eslint-recommended' 7 | - 'plugin:@typescript-eslint/recommended' 8 | parser: '@typescript-eslint/parser' 9 | rules: 10 | no-inner-declarations: off 11 | indent: 12 | - error 13 | - 4 14 | - SwitchCase: 1 15 | '@typescript-eslint/no-explicit-any': off 16 | # TODO: Clean-up code and enable following rules 17 | '@typescript-eslint/camelcase': off 18 | '@typescript-eslint/explicit-function-return-type': off 19 | '@typescript-eslint/no-use-before-define': off 20 | 21 | parserOptions: 22 | ecmaVersion: 2022 23 | sourceType: module -------------------------------------------------------------------------------- /packages/near-api-js/src/account.ts: -------------------------------------------------------------------------------- 1 | export { 2 | Account, 3 | AccountBalance, 4 | AccountAuthorizedApp, 5 | SignAndSendTransactionOptions, 6 | FunctionCallOptions, 7 | ChangeFunctionCallOptions, 8 | ViewFunctionCallOptions, 9 | } from "@near-js/accounts"; 10 | -------------------------------------------------------------------------------- /packages/near-api-js/src/account_creator.ts: -------------------------------------------------------------------------------- 1 | export { AccountCreator, LocalAccountCreator, UrlAccountCreator } from '@near-js/accounts'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/browser-connect.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Connect to NEAR using the provided configuration. 3 | * 4 | * {@link ConnectConfig#networkId} and {@link ConnectConfig#nodeUrl} are required. 5 | * 6 | * To sign transactions you can also pass: {@link ConnectConfig#keyStore} 7 | * 8 | * Both are passed they are prioritize in that order. 9 | * 10 | * @see {@link ConnectConfig} 11 | * @example 12 | * ```js 13 | * async function initNear() { 14 | * const near = await connect({ 15 | * networkId: 'testnet', 16 | * nodeUrl: 'https://rpc.testnet.near.org' 17 | * }) 18 | * } 19 | * ``` 20 | * 21 | * @module browserConnect 22 | */ 23 | import { Near, NearConfig } from './near'; 24 | 25 | export interface ConnectConfig extends NearConfig { 26 | /** @hidden */ 27 | keyPath?: string; 28 | } 29 | 30 | /** 31 | * Initialize connection to Near network. 32 | */ 33 | export async function connect(config: ConnectConfig): Promise { 34 | return new Near(config); 35 | } 36 | -------------------------------------------------------------------------------- /packages/near-api-js/src/browser-index.ts: -------------------------------------------------------------------------------- 1 | /** @hidden @module */ 2 | export * as keyStores from './key_stores/browser-index'; 3 | export * from './common-index'; 4 | export * from './browser-connect'; 5 | -------------------------------------------------------------------------------- /packages/near-api-js/src/common-index.ts: -------------------------------------------------------------------------------- 1 | /** @hidden @module */ 2 | import * as providers from './providers'; 3 | import * as utils from './utils'; 4 | import * as transactions from './transaction'; 5 | import * as validators from './validators'; 6 | 7 | import { Account } from './account'; 8 | import * as accountCreator from './account_creator'; 9 | import { Connection } from './connection'; 10 | import { Signer, KeyPairSigner } from './signer'; 11 | import { Contract } from './contract'; 12 | import { KeyPair } from './utils/key_pair'; 13 | import { Near } from './near'; 14 | 15 | export { 16 | accountCreator, 17 | providers, 18 | utils, 19 | transactions, 20 | validators, 21 | 22 | Account, 23 | Connection, 24 | Contract, 25 | KeyPairSigner, 26 | Signer, 27 | KeyPair, 28 | 29 | Near, 30 | }; 31 | -------------------------------------------------------------------------------- /packages/near-api-js/src/connection.ts: -------------------------------------------------------------------------------- 1 | export { Connection } from '@near-js/accounts'; -------------------------------------------------------------------------------- /packages/near-api-js/src/constants.ts: -------------------------------------------------------------------------------- 1 | export { DEFAULT_FUNCTION_CALL_GAS } from '@near-js/utils'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/contract.ts: -------------------------------------------------------------------------------- 1 | export { Contract, ContractMethods } from '@near-js/accounts'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/index.ts: -------------------------------------------------------------------------------- 1 | /** @ignore @module */ 2 | export * as keyStores from './key_stores/index'; 3 | export * from './common-index'; 4 | export * from './connect'; 5 | export * from './constants'; -------------------------------------------------------------------------------- /packages/near-api-js/src/key_stores/browser-index.ts: -------------------------------------------------------------------------------- 1 | /** @hidden @module */ 2 | import { KeyStore } from './keystore'; 3 | import { InMemoryKeyStore } from './in_memory_key_store'; 4 | import { BrowserLocalStorageKeyStore } from './browser_local_storage_key_store'; 5 | import { MergeKeyStore } from './merge_key_store'; 6 | 7 | export { 8 | KeyStore, 9 | InMemoryKeyStore, 10 | BrowserLocalStorageKeyStore, 11 | MergeKeyStore, 12 | }; 13 | -------------------------------------------------------------------------------- /packages/near-api-js/src/key_stores/browser_local_storage_key_store.ts: -------------------------------------------------------------------------------- 1 | export { BrowserLocalStorageKeyStore } from '@near-js/keystores-browser'; -------------------------------------------------------------------------------- /packages/near-api-js/src/key_stores/in_memory_key_store.ts: -------------------------------------------------------------------------------- 1 | export { InMemoryKeyStore } from '@near-js/keystores'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/key_stores/index.ts: -------------------------------------------------------------------------------- 1 | /** @ignore @module */ 2 | import { KeyStore } from './keystore'; 3 | import { InMemoryKeyStore } from './in_memory_key_store'; 4 | import { BrowserLocalStorageKeyStore } from './browser_local_storage_key_store'; 5 | import { UnencryptedFileSystemKeyStore } from './unencrypted_file_system_keystore'; 6 | import { MergeKeyStore } from './merge_key_store'; 7 | 8 | export { 9 | KeyStore, 10 | InMemoryKeyStore, 11 | BrowserLocalStorageKeyStore, 12 | UnencryptedFileSystemKeyStore, 13 | MergeKeyStore, 14 | }; 15 | -------------------------------------------------------------------------------- /packages/near-api-js/src/key_stores/keystore.ts: -------------------------------------------------------------------------------- 1 | export { KeyStore } from '@near-js/keystores'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/key_stores/merge_key_store.ts: -------------------------------------------------------------------------------- 1 | export { MergeKeyStore } from '@near-js/keystores'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/key_stores/unencrypted_file_system_keystore.ts: -------------------------------------------------------------------------------- 1 | export { readKeyFile, UnencryptedFileSystemKeyStore } from '@near-js/keystores-node'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/providers/failover-rpc-provider.ts: -------------------------------------------------------------------------------- 1 | export { FailoverRpcProvider } from '@near-js/providers'; -------------------------------------------------------------------------------- /packages/near-api-js/src/providers/index.ts: -------------------------------------------------------------------------------- 1 | /** @hidden @module */ 2 | 3 | import { Provider, FinalExecutionOutcome, ExecutionOutcomeWithId, getTransactionLastResult, FinalExecutionStatus, FinalExecutionStatusBasic } from './provider'; 4 | import { JsonRpcProvider, TypedError, ErrorContext } from './json-rpc-provider'; 5 | import { FailoverRpcProvider } from './failover-rpc-provider'; 6 | 7 | export { 8 | Provider, 9 | FinalExecutionOutcome, 10 | JsonRpcProvider, 11 | FailoverRpcProvider, 12 | ExecutionOutcomeWithId, 13 | FinalExecutionStatus, 14 | FinalExecutionStatusBasic, 15 | getTransactionLastResult, 16 | TypedError, 17 | ErrorContext 18 | }; 19 | -------------------------------------------------------------------------------- /packages/near-api-js/src/providers/json-rpc-provider.ts: -------------------------------------------------------------------------------- 1 | export { ErrorContext, TypedError } from '@near-js/types'; 2 | export { JsonRpcProvider } from '@near-js/providers'; 3 | -------------------------------------------------------------------------------- /packages/near-api-js/src/providers/provider.ts: -------------------------------------------------------------------------------- 1 | 2 | export { getTransactionLastResult } from '@near-js/utils'; 3 | export { Provider } from '@near-js/providers'; 4 | export { 5 | IdType, 6 | LightClientBlockLiteView, 7 | LightClientProof, 8 | LightClientProofRequest, 9 | NextLightClientBlockRequest, 10 | NextLightClientBlockResponse, 11 | 12 | AccessKeyWithPublicKey, 13 | BlockHash, 14 | BlockChange, 15 | BlockChangeResult, 16 | BlockHeader, 17 | BlockHeaderInnerLiteView, 18 | BlockHeight, 19 | BlockId, 20 | BlockReference, 21 | BlockResult, 22 | BlockShardId, 23 | ChangeResult, 24 | Chunk, 25 | ChunkHash, 26 | ChunkHeader, 27 | ChunkId, 28 | ChunkResult, 29 | Finality, 30 | GasPrice, 31 | MerkleNode, 32 | MerklePath, 33 | NearProtocolConfig, 34 | NearProtocolRuntimeConfig, 35 | NodeStatusResult, 36 | ShardId, 37 | SyncInfo, 38 | TotalWeight, 39 | ProviderTransaction as Transaction, 40 | 41 | CallFunctionRequest, 42 | RpcQueryRequest, 43 | ViewAccessKeyListRequest, 44 | ViewAccessKeyRequest, 45 | ViewAccountRequest, 46 | ViewCodeRequest, 47 | ViewStateRequest, 48 | 49 | AccessKeyInfoView, 50 | AccessKeyList, 51 | AccessKeyView, 52 | AccessKeyViewRaw, 53 | AccountView, 54 | CodeResult, 55 | ContractCodeView, 56 | ExecutionError, 57 | ExecutionOutcome, 58 | ExecutionOutcomeWithId, 59 | ExecutionOutcomeWithIdView, 60 | ExecutionStatus, 61 | ExecutionStatusBasic, 62 | FinalExecutionOutcome, 63 | FinalExecutionStatus, 64 | FinalExecutionStatusBasic, 65 | FunctionCallPermissionView, 66 | QueryResponseKind, 67 | ViewStateResult, 68 | 69 | CurrentEpochValidatorInfo, 70 | EpochValidatorInfo, 71 | NextEpochValidatorInfo, 72 | ValidatorStakeView, 73 | } from '@near-js/types'; 74 | -------------------------------------------------------------------------------- /packages/near-api-js/src/signer.ts: -------------------------------------------------------------------------------- 1 | export { KeyPairSigner, Signer } from "@near-js/signers"; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/transaction.ts: -------------------------------------------------------------------------------- 1 | export { 2 | stringifyJsonOrBytes, 3 | Action, 4 | AccessKey, 5 | AccessKeyPermission, 6 | AddKey, 7 | CreateAccount, 8 | DeleteAccount, 9 | DeleteKey, 10 | DeployContract, 11 | FullAccessPermission, 12 | FunctionCall, 13 | FunctionCallPermission, 14 | Stake, 15 | Transfer, 16 | SCHEMA, 17 | createTransaction, 18 | Signature, 19 | SignedTransaction, 20 | Transaction, 21 | encodeSignedDelegate, 22 | encodeDelegateAction, 23 | encodeTransaction 24 | } from '@near-js/transactions'; 25 | 26 | import { PublicKey } from '@near-js/crypto'; 27 | import { AccessKey, actionCreators, stringifyJsonOrBytes } from '@near-js/transactions'; 28 | 29 | export const addKey = (publicKey: PublicKey, accessKey: AccessKey) => actionCreators.addKey(publicKey, accessKey); 30 | export const createAccount = () => actionCreators.createAccount(); 31 | export const deleteAccount = (beneficiaryId: string) => actionCreators.deleteAccount(beneficiaryId); 32 | export const deleteKey = (publicKey: PublicKey) => actionCreators.deleteKey(publicKey); 33 | export const deployContract = (code: Uint8Array) => actionCreators.deployContract(code); 34 | export const fullAccessKey = () => actionCreators.fullAccessKey(); 35 | export const functionCall = (methodName: string, args: object | Uint8Array, gas: bigint, deposit: bigint, stringify?: typeof stringifyJsonOrBytes) => actionCreators.functionCall(methodName, args, gas, deposit, stringify); 36 | export const functionCallAccessKey = (receiverId: string, methodNames: string[], allowance?: bigint) => actionCreators.functionCallAccessKey(receiverId, methodNames, allowance); 37 | export const stake = (stake: bigint, publicKey: PublicKey) => actionCreators.stake(stake, publicKey); 38 | export const transfer = (deposit: bigint) => actionCreators.transfer(deposit); 39 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/enums.ts: -------------------------------------------------------------------------------- 1 | export { Enum } from '@near-js/types'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/errors.ts: -------------------------------------------------------------------------------- 1 | export { 2 | ArgumentSchemaError, 3 | ConflictingOptions, 4 | UnknownArgumentError, 5 | UnsupportedSerializationError, 6 | } from '@near-js/accounts'; 7 | export { 8 | ArgumentTypeError, 9 | ErrorContext, 10 | PositionalArgsError, 11 | TypedError, 12 | } from '@near-js/types'; 13 | export { logWarning } from '@near-js/utils'; 14 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/exponential-backoff.ts: -------------------------------------------------------------------------------- 1 | import { exponentialBackoff } from '@near-js/providers'; 2 | 3 | export default exponentialBackoff; 4 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/format.ts: -------------------------------------------------------------------------------- 1 | export { 2 | NEAR_NOMINATION, 3 | NEAR_NOMINATION_EXP, 4 | formatNearAmount, 5 | parseNearAmount, 6 | } from '@near-js/utils'; 7 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/index.ts: -------------------------------------------------------------------------------- 1 | 2 | import * as key_pair from './key_pair'; 3 | import * as serialize from './serialize'; 4 | import * as enums from './enums'; 5 | import * as format from './format'; 6 | import * as rpc_errors from './rpc_errors'; 7 | 8 | import { PublicKey, KeyPair, KeyPairString, KeyPairEd25519 } from './key_pair'; 9 | import { Logger } from './logger'; 10 | 11 | export { 12 | key_pair, 13 | serialize, 14 | enums, 15 | format, 16 | PublicKey, 17 | KeyPair, 18 | KeyPairString, 19 | KeyPairEd25519, 20 | rpc_errors, 21 | Logger 22 | }; 23 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/key_pair.ts: -------------------------------------------------------------------------------- 1 | export { 2 | KeyPair, 3 | type KeyPairString, 4 | KeyPairEd25519, 5 | KeyType, 6 | PublicKey, 7 | Signature, 8 | } from '@near-js/crypto'; 9 | 10 | export type Arrayish = string | ArrayLike; 11 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/logger.ts: -------------------------------------------------------------------------------- 1 | export { Logger } from '@near-js/utils'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/logging.ts: -------------------------------------------------------------------------------- 1 | export { printTxOutcomeLogs, printTxOutcomeLogsAndFailures } from '@near-js/utils'; 2 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/rpc_errors.ts: -------------------------------------------------------------------------------- 1 | export { 2 | parseRpcError, 3 | parseResultError, 4 | formatError, 5 | getErrorTypeFromErrorMessage, 6 | ServerError, 7 | } from '@near-js/utils'; 8 | -------------------------------------------------------------------------------- /packages/near-api-js/src/utils/serialize.ts: -------------------------------------------------------------------------------- 1 | export { 2 | serialize, 3 | deserialize, 4 | Schema, 5 | } from 'borsh'; 6 | 7 | export { 8 | baseEncode as base_encode, 9 | baseDecode as base_decode, 10 | } from '@near-js/utils'; -------------------------------------------------------------------------------- /packages/near-api-js/src/validators.ts: -------------------------------------------------------------------------------- 1 | export { 2 | diffEpochValidators, 3 | findSeatPrice, 4 | ChangedValidatorInfo, 5 | EpochValidatorsDiff, 6 | } from '@near-js/utils'; 7 | -------------------------------------------------------------------------------- /packages/near-api-js/test/.eslintrc.yml: -------------------------------------------------------------------------------- 1 | extends: '../../../.eslintrc.base.yml' 2 | env: 3 | jest: true 4 | globals: 5 | jasmine: true 6 | window: false 7 | fail: true 8 | -------------------------------------------------------------------------------- /packages/near-api-js/test/config.js: -------------------------------------------------------------------------------- 1 | const { Worker } = require('near-workspaces'); 2 | const fs = require('fs'); 3 | let worker; 4 | module.exports = async function getConfig(env) { 5 | switch (env) { 6 | case 'production': 7 | case 'mainnet': 8 | return { 9 | networkId: 'mainnet', 10 | nodeUrl: 'https://rpc.mainnet.near.org', 11 | walletUrl: 'https://wallet.near.org', 12 | helperUrl: 'https://helper.mainnet.near.org', 13 | }; 14 | case 'development': 15 | case 'testnet': 16 | return { 17 | networkId: 'default', 18 | nodeUrl: 'https://rpc.testnet.near.org', 19 | walletUrl: 'https://wallet.testnet.near.org', 20 | helperUrl: 'https://helper.testnet.near.org', 21 | masterAccount: 'test.near', 22 | }; 23 | case 'betanet': 24 | return { 25 | networkId: 'betanet', 26 | nodeUrl: 'https://rpc.betanet.near.org', 27 | walletUrl: 'https://wallet.betanet.near.org', 28 | helperUrl: 'https://helper.betanet.near.org', 29 | }; 30 | case 'local': 31 | return { 32 | networkId: 'local', 33 | nodeUrl: 'http://localhost:3030', 34 | keyPath: `${process.env.HOME}/.near/validator_key.json`, 35 | walletUrl: 'http://localhost:4000/wallet', 36 | }; 37 | case 'test': 38 | case 'ci': { 39 | if (!worker) worker = await Worker.init(); 40 | const keyFile = fs.readFileSync(`${worker.rootAccount.manager.config.homeDir}/validator_key.json`); 41 | const keyPair = JSON.parse(keyFile.toString()); 42 | return { 43 | networkId: worker.config.network, 44 | nodeUrl: worker.manager.config.rpcAddr, 45 | masterAccount: worker.rootAccount._accountId, 46 | secretKey: keyPair.secret_key || keyPair.private_key 47 | }; 48 | } 49 | default: 50 | throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`); 51 | } 52 | }; 53 | -------------------------------------------------------------------------------- /packages/near-api-js/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "esModuleInterop": true, 4 | "lib": [ 5 | "es2020", 6 | "esnext", 7 | "dom" 8 | ], 9 | "module": "commonjs", 10 | "target": "es2020", 11 | "moduleResolution": "node", 12 | "alwaysStrict": true, 13 | "outDir": "./lib", 14 | "declaration": true, 15 | "preserveSymlinks": false, 16 | "preserveWatchOutput": true, 17 | "pretty": false, 18 | "forceConsistentCasingInFileNames": true, 19 | "noFallthroughCasesInSwitch": true, 20 | "noImplicitAny": false, 21 | "noImplicitReturns": true, 22 | "noUnusedLocals": true, 23 | "experimentalDecorators": true, 24 | "resolveJsonModule": true, 25 | }, 26 | "files": [ 27 | "src/index.ts", 28 | "src/browser-index.ts", 29 | ], 30 | } -------------------------------------------------------------------------------- /packages/near-api-js/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/providers/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/providers 2 | 3 | A collection of classes, functions, and types for communicating with the NEAR blockchain directly. For use with both client- and server-side JavaScript execution contexts. 4 | 5 | ## Modules 6 | 7 | - [Provider](https://github.com/near/near-api-js/blob/master/packages/providers/src/provider.ts) abstract class for interacting with NEAR RPC 8 | - [JsonRpcProvider](https://github.com/near/near-api-js/blob/master/packages/providers/src/json-rpc-provider.ts) implementation of `Provider` for [JSON-RPC](https://www.jsonrpc.org/) 9 | - [fetch](https://github.com/near/near-api-js/blob/master/packages/providers/src/fetch.ts) NodeJS `fetch` implementation 10 | - [fetchJson](https://github.com/near/near-api-js/blob/master/packages/providers/src/fetch_json.ts) low-level function for fetching and parsing RPC data 11 | 12 | # License 13 | 14 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 15 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 16 | -------------------------------------------------------------------------------- /packages/providers/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: "ts-jest", 3 | collectCoverage: true, 4 | testEnvironment: "node", 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | "^.+\\.[tj]s$": [ 8 | "ts-jest", 9 | { 10 | tsconfig: "tsconfig.json", 11 | }, 12 | ], 13 | }, 14 | }; 15 | -------------------------------------------------------------------------------- /packages/providers/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/providers", 3 | "version": "2.0.2", 4 | "description": "Library of implementations for interfacing with the NEAR blockchain", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 13 | "test": "jest", 14 | "check-exports": "attw --pack ." 15 | }, 16 | "keywords": [], 17 | "author": "", 18 | "license": "ISC", 19 | "dependencies": { 20 | "@near-js/transactions": "workspace:*", 21 | "@near-js/types": "workspace:*", 22 | "@near-js/utils": "workspace:*", 23 | "@near-js/crypto": "workspace:*", 24 | "borsh": "1.0.0", 25 | "exponential-backoff": "^3.1.2" 26 | }, 27 | "devDependencies": { 28 | "@jest/globals": "^29.7.0", 29 | "@types/node": "20.0.0", 30 | "build": "workspace:*", 31 | "jest": "29.7.0", 32 | "near-workspaces": "5.0.0", 33 | "ts-jest": "29.2.6", 34 | "tsconfig": "workspace:*", 35 | "typescript": "5.4.5" 36 | }, 37 | "peerDependencies": { 38 | "@near-js/transactions": "^2.0.1", 39 | "@near-js/types": "^2.0.1", 40 | "@near-js/utils": "^2.0.1", 41 | "@near-js/crypto": "^2.0.1" 42 | }, 43 | "optionalDependencies": { 44 | "node-fetch": "2.6.7" 45 | }, 46 | "files": [ 47 | "lib" 48 | ], 49 | "exports": { 50 | ".": { 51 | "import": "./lib/esm/index.js", 52 | "require": "./lib/commonjs/index.cjs" 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /packages/providers/src/exponential-backoff.ts: -------------------------------------------------------------------------------- 1 | export async function exponentialBackoff(startWaitTime, retryNumber, waitBackoff, getResult) { 2 | // TODO: jitter? 3 | 4 | let waitTime = startWaitTime; 5 | for (let i = 0; i < retryNumber; i++) { 6 | const result = await getResult(); 7 | if (result) { 8 | return result; 9 | } 10 | 11 | await sleep(waitTime); 12 | waitTime *= waitBackoff; 13 | } 14 | 15 | return null; 16 | } 17 | 18 | // Sleep given number of millis. 19 | function sleep(millis: number): Promise { 20 | return new Promise(resolve => setTimeout(resolve, millis)); 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /packages/providers/src/index.ts: -------------------------------------------------------------------------------- 1 | export { exponentialBackoff } from './exponential-backoff'; 2 | export { JsonRpcProvider } from './json-rpc-provider'; 3 | export { FailoverRpcProvider } from './failover-rpc-provider'; 4 | export { Provider } from './provider'; 5 | -------------------------------------------------------------------------------- /packages/providers/test/fetch_json.test.ts: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from '@jest/globals'; 2 | import { fetchJsonRpc, retryConfig } from '../src/fetch_json'; 3 | 4 | describe('fetchJson', () => { 5 | test('string parameter in fetchJson', async () => { 6 | const RPC_URL = 'https://rpc.testnet.near.org'; 7 | const statusRequest = { 8 | jsonrpc: '2.0', 9 | id: 'dontcare', 10 | method: 'status', 11 | params: [] 12 | }; 13 | // @ts-expect-error test input 14 | const result = await fetchJsonRpc(RPC_URL, statusRequest, undefined, retryConfig()); 15 | expect(result.result.chain_id).toBe('testnet'); 16 | }); 17 | test('object parameter in fetchJson', async () => { 18 | const connection = { url: 'https://rpc.testnet.near.org' }; 19 | const statusRequest = { 20 | jsonrpc: '2.0', 21 | id: 'dontcare', 22 | method: 'status', 23 | params: [] 24 | }; 25 | // @ts-expect-error test input 26 | const result = await fetchJsonRpc(connection.url, statusRequest, undefined, retryConfig()); 27 | expect(result.result.chain_id).toBe('testnet'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /packages/providers/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/providers/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/providers/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/providers/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/signers/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/signers 2 | 3 | A collection of classes and types to facilitate cryptographic signing. 4 | 5 | ## Modules 6 | 7 | - [Signer](https://github.com/near/near-api-js/blob/master/packages/signers/src/signer.ts) abstract class for cryptographic signing 8 | - [InMemorySigner](https://github.com/near/near-api-js/blob/master/packages/signers/src/in_memory_signer.ts) implementation of `Signer` using [InMemoryKeyStore](https://github.com/near/near-api-js/blob/master/packages/keystores/src/in_memory_key_store.ts) to provide keys 9 | 10 | # License 11 | 12 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 13 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 14 | -------------------------------------------------------------------------------- /packages/signers/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/signers/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/signers", 3 | "version": "2.0.2", 4 | "description": "Modules for cryptographically signing messages", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 13 | "test": "jest", 14 | "check-exports": "attw --pack ." 15 | }, 16 | "keywords": [], 17 | "author": "", 18 | "license": "ISC", 19 | "dependencies": { 20 | "@near-js/crypto": "workspace:*", 21 | "@near-js/keystores": "workspace:*", 22 | "@near-js/transactions": "workspace:*", 23 | "@noble/hashes": "1.7.1", 24 | "borsh": "1.0.0" 25 | }, 26 | "devDependencies": { 27 | "@jest/globals": "^29.7.0", 28 | "@types/node": "20.0.0", 29 | "build": "workspace:*", 30 | "jest": "29.7.0", 31 | "ts-jest": "29.2.6", 32 | "tsconfig": "workspace:*", 33 | "typescript": "5.4.5" 34 | }, 35 | "peerDependencies": { 36 | "@near-js/crypto": "^2.0.1", 37 | "@near-js/keystores": "^2.0.1", 38 | "@near-js/transactions": "^2.0.1" 39 | }, 40 | "files": [ 41 | "lib" 42 | ], 43 | "exports": { 44 | ".": { 45 | "import": "./lib/esm/index.js", 46 | "require": "./lib/commonjs/index.cjs" 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /packages/signers/src/index.ts: -------------------------------------------------------------------------------- 1 | export { KeyPairSigner } from './key_pair_signer'; 2 | export { Signer, SignedMessage } from './signer'; 3 | -------------------------------------------------------------------------------- /packages/signers/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/signers/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/signers/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/signers/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/tokens/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @near-js/tokens 2 | 3 | ## 2.0.2 4 | 5 | ### Patch Changes 6 | 7 | - [#1560](https://github.com/near/near-api-js/pull/1560) [`3349d4b`](https://github.com/near/near-api-js/commit/3349d4b542bab2a2150326918bdc0b40e3b7fdbe) Thanks [@denbite](https://github.com/denbite)! - Fix the bug with `ft_balance_of` always returning `undefined` for FungibleToken 8 | 9 | - [#1559](https://github.com/near/near-api-js/pull/1559) [`59d3dc9`](https://github.com/near/near-api-js/commit/59d3dc9580be05662cb9a587e82359faccd69d1b) Thanks [@r-near](https://github.com/r-near)! - fix: ESM Module Resolution 10 | 11 | ## 2.0.1 12 | 13 | ### Patch Changes 14 | 15 | - [#1554](https://github.com/near/near-api-js/pull/1554) [`13f93eb`](https://github.com/near/near-api-js/commit/13f93ebdac497bb473364da66a493344d955b27f) Thanks [@denbite](https://github.com/denbite)! - Redeploy recent release as patch 16 | 17 | ## 2.0.0 18 | 19 | ### Major Changes 20 | 21 | New package, allows to work with tokens on the NEAR blockchain, this includes the Native NEAR, Fungible Tokens and Non-Fungible Tokens. 22 | 23 | Tokens implement the `getBalance` and `transfer` methods, as well as conversion utils such as `toUnits` and `toDecimal` 24 | 25 | In this first release, the following tokens are supported: 26 | 27 | - NEAR 28 | - USDT 29 | - USDC 30 | - wNEAR 31 | 32 | - [#1513](https://github.com/near/near-api-js/pull/1513) [`a8e1046`](https://github.com/near/near-api-js/commit/a8e1046d4c184700bed93229f81e7875fca11b27) Thanks [@denbite](https://github.com/denbite)! - Major update for Signer and Account APIs to streamline development 33 | -------------------------------------------------------------------------------- /packages/tokens/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/tokens 2 | 3 | A collection of standard tokens 4 | 5 | # License 6 | 7 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 8 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 9 | -------------------------------------------------------------------------------- /packages/tokens/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/tokens/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/tokens", 3 | "version": "2.0.2", 4 | "description": "Modules with tokens", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "type": "module", 8 | "scripts": { 9 | "build": "tsup", 10 | "clean": "rimraf lib/", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 13 | "test": "jest", 14 | "check-exports": "attw --pack ." 15 | }, 16 | "keywords": [], 17 | "author": "", 18 | "license": "ISC", 19 | "devDependencies": { 20 | "@jest/globals": "^29.7.0", 21 | "@near-js/types": "workspace:*", 22 | "@types/node": "20.0.0", 23 | "build": "workspace:*", 24 | "jest": "29.7.0", 25 | "ts-jest": "29.2.6", 26 | "tsconfig": "workspace:*", 27 | "typescript": "5.4.5" 28 | }, 29 | "files": [ 30 | "lib" 31 | ], 32 | "exports": { 33 | ".": { 34 | "require": "./lib/commonjs/index.cjs", 35 | "import": "./lib/esm/index.js" 36 | }, 37 | "./mainnet": { 38 | "require": "./lib/commonjs/mainnet/index.cjs", 39 | "import": "./lib/esm/mainnet/index.js" 40 | }, 41 | "./testnet": { 42 | "require": "./lib/commonjs/testnet/index.cjs", 43 | "import": "./lib/esm/testnet/index.js" 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /packages/tokens/src/index.ts: -------------------------------------------------------------------------------- 1 | export { NEAR, NativeToken, FungibleToken } from "./ft" 2 | export { NonFungibleToken, NFTContract } from "./nft" 3 | export * as mainnet from "./mainnet" 4 | export * as testnet from "./testnet" -------------------------------------------------------------------------------- /packages/tokens/src/nft/index.ts: -------------------------------------------------------------------------------- 1 | import type { AccountLike } from '@near-js/types'; 2 | 3 | interface ContractMetadata { 4 | spec?: string; 5 | name: string; 6 | symbol: string; 7 | icon?: string; 8 | baseUri?: string; 9 | reference?: string; 10 | referenceHash?: string; 11 | } 12 | 13 | interface NFTMetadata { 14 | title?: string; 15 | description?: string; 16 | media?: string; 17 | mediaHash?: string; 18 | copies?: number; 19 | issuedAt?: string; 20 | expiresAt?: string; 21 | startsAt?: string; 22 | updatedAt?: string; 23 | extra?: string; 24 | reference?: string; 25 | referenceHash?: string; 26 | } 27 | 28 | export class NFTContract { 29 | public readonly metadata: ContractMetadata; 30 | public readonly accountId: string; 31 | 32 | constructor(accountId: string, metadata: ContractMetadata) { 33 | metadata.spec = metadata.spec || 'nft-1.0.0'; 34 | this.metadata = metadata; 35 | this.accountId = accountId; 36 | } 37 | 38 | transfer({ 39 | from, 40 | receiverId, 41 | tokenId 42 | }: { 43 | from: AccountLike; 44 | receiverId: string; 45 | tokenId: string; 46 | }): Promise { 47 | return from.callFunction({ 48 | contractId: this.accountId, 49 | methodName: 'nft_transfer', 50 | args: { 51 | receiver_id: receiverId, 52 | token_id: tokenId 53 | }, 54 | deposit: 1, 55 | gas: 30000000000000 56 | }); 57 | } 58 | } 59 | 60 | export class NonFungibleToken { 61 | public readonly contractId: string; 62 | public readonly tokenId: string; 63 | public readonly ownerId: string; 64 | public readonly metadata: NFTMetadata; 65 | 66 | constructor( 67 | contractId: string, 68 | tokenId: string, 69 | ownerId: string, 70 | metadata: NFTMetadata 71 | ) { 72 | this.contractId = contractId; 73 | this.tokenId = tokenId; 74 | this.ownerId = ownerId; 75 | this.metadata = metadata; 76 | } 77 | } -------------------------------------------------------------------------------- /packages/tokens/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "include": ["src/**/*"] 8 | } 9 | -------------------------------------------------------------------------------- /packages/tokens/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "include": ["src/**/*"] 7 | } 8 | -------------------------------------------------------------------------------- /packages/tokens/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/tokens/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/transactions/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/transactions 2 | 3 | A collection of classes, functions, and types for composing, serializing, and signing NEAR transactions. 4 | 5 | ## Modules 6 | 7 | - [actionCreators](https://github.com/near/near-api-js/blob/master/packages/transactions/src/action_creators.ts) functions for composing actions 8 | - [Actions](https://github.com/near/near-api-js/blob/master/packages/transactions/src/actions.ts) classes for actions 9 | - [Schema](https://github.com/near/near-api-js/blob/master/packages/transactions/src/schema.ts) classes and types concerned with (de-)serialization of transactions 10 | - [createTransaction](https://github.com/near/near-api-js/blob/master/packages/transactions/src/create_transaction.ts) function for composing a transaction 11 | - [signTransaction](https://github.com/near/near-api-js/blob/master/packages/transactions/src/sign.ts) function for signing a transaction 12 | 13 | # License 14 | 15 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 16 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 17 | -------------------------------------------------------------------------------- /packages/transactions/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/transactions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/transactions", 3 | "version": "2.0.2", 4 | "description": "Functions and data types for transactions on NEAR", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 13 | "test": "jest", 14 | "check-exports": "attw --pack ." 15 | }, 16 | "keywords": [], 17 | "author": "", 18 | "license": "ISC", 19 | "dependencies": { 20 | "@near-js/crypto": "workspace:*", 21 | "@near-js/types": "workspace:*", 22 | "@near-js/utils": "workspace:*", 23 | "@noble/hashes": "1.7.1", 24 | "borsh": "1.0.0" 25 | }, 26 | "devDependencies": { 27 | "@jest/globals": "^29.7.0", 28 | "@near-js/keystores": "workspace:*", 29 | "@types/node": "20.0.0", 30 | "build": "workspace:*", 31 | "jest": "29.7.0", 32 | "ts-jest": "29.2.6", 33 | "tsconfig": "workspace:*", 34 | "typescript": "5.4.5" 35 | }, 36 | "peerDependencies": { 37 | "@near-js/crypto": "^2.0.1", 38 | "@near-js/types": "^2.0.1", 39 | "@near-js/utils": "^2.0.1" 40 | }, 41 | "files": [ 42 | "lib" 43 | ], 44 | "exports": { 45 | ".": { 46 | "import": "./lib/esm/index.js", 47 | "require": "./lib/commonjs/index.cjs" 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /packages/transactions/src/create_transaction.ts: -------------------------------------------------------------------------------- 1 | import { PublicKey } from '@near-js/crypto'; 2 | 3 | import { Action } from './actions'; 4 | import { Transaction } from './schema'; 5 | 6 | /** 7 | * Creates a new transaction object with the provided parameters. 8 | * @param signerId The NEAR account ID of the transaction signer. 9 | * @param publicKey The public key associated with the signer. 10 | * @param receiverId The NEAR account ID of the transaction receiver. 11 | * @param nonce The nonce value for the transaction, represented as a BN, string, or number. 12 | * @param actions An array of transaction actions to be performed. 13 | * @param blockHash The hash of the block where the transaction will be included. 14 | * @returns A new transaction object initialized with the provided parameters. 15 | */ 16 | export function createTransaction( 17 | signerId: string, 18 | publicKey: PublicKey, 19 | receiverId: string, 20 | nonce: bigint | string | number, 21 | actions: Action[], 22 | blockHash: Uint8Array 23 | ): Transaction { 24 | const txNonce = typeof nonce === 'bigint' ? nonce : BigInt(nonce); 25 | return new Transaction({ 26 | signerId, 27 | publicKey, 28 | nonce: txNonce, 29 | receiverId, 30 | actions, 31 | blockHash, 32 | }); 33 | } 34 | -------------------------------------------------------------------------------- /packages/transactions/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './action_creators'; 2 | export * from './actions'; 3 | export * from './create_transaction'; 4 | export * from './delegate'; 5 | export * from './schema'; 6 | export * from './signature'; 7 | -------------------------------------------------------------------------------- /packages/transactions/src/prefix.ts: -------------------------------------------------------------------------------- 1 | const ACTIONABLE_MESSAGE_BASE = Math.pow(2, 30); 2 | // const NON_ACTIONABLE_MESSAGE_BASE = Math.pow(2, 31); 3 | 4 | /** The set of NEPs for which an [NEP-461](https://github.com/near/NEPs/pull/461) prefix is required on the message prior to hashing **/ 5 | const NEP = { 6 | MetaTransactions: 366, 7 | }; 8 | 9 | /** Base class for NEP message prefixes **/ 10 | abstract class NEPPrefix { 11 | prefix: number; 12 | 13 | constructor({ prefix }: { prefix: number }) { 14 | this.prefix = prefix; 15 | } 16 | 17 | } 18 | 19 | /** Class for constructing prefixes on actionable (on-chain) messages **/ 20 | abstract class ActionableMessagePrefix extends NEPPrefix { 21 | /** Given the NEP number, set the prefix using 2^30 as the offset **/ 22 | protected constructor(prefix: number) { 23 | super({ prefix: ACTIONABLE_MESSAGE_BASE + prefix }); 24 | } 25 | } 26 | 27 | /** 28 | * Class for constructing prefixes on non-actionable (off-chain) messages 29 | * @todo uncomment when off-chain messages are supported 30 | * **/ 31 | // abstract class NonActionableMessagePrefix extends NEPPrefix { 32 | // /** Given the NEP number, set the prefix using 2^31 as the offset **/ 33 | // protected constructor(prefix: number) { 34 | // super({ prefix: NON_ACTIONABLE_MESSAGE_BASE + prefix }); 35 | // } 36 | // } 37 | 38 | /** Prefix for delegate actions whose signatures must always be distinguishable from valid transaction signatures **/ 39 | export class DelegateActionPrefix extends ActionableMessagePrefix { constructor() { super(NEP.MetaTransactions); } } 40 | -------------------------------------------------------------------------------- /packages/transactions/src/signature.ts: -------------------------------------------------------------------------------- 1 | import { KeyType } from '@near-js/crypto'; 2 | import { Enum } from '@near-js/types'; 3 | 4 | class ED25519Signature { keyType: KeyType = KeyType.ED25519; data: Uint8Array; } 5 | class SECP256K1Signature { keyType: KeyType = KeyType.SECP256K1; data: Uint8Array; } 6 | 7 | function resolveEnumKeyName(keyType: KeyType) { 8 | switch (keyType) { 9 | case KeyType.ED25519: { 10 | return 'ed25519Signature'; 11 | } 12 | case KeyType.SECP256K1: { 13 | return 'secp256k1Signature'; 14 | } 15 | default: { 16 | throw Error(`unknown type ${keyType}`); 17 | } 18 | } 19 | } 20 | 21 | export class Signature extends Enum { 22 | enum: string; 23 | ed25519Signature?: ED25519Signature; 24 | secp256k1Signature?: SECP256K1Signature; 25 | 26 | constructor(signature: { keyType: KeyType, data: Uint8Array }) { 27 | const keyName = resolveEnumKeyName(signature.keyType); 28 | super({ [keyName]: signature }); 29 | this[keyName] = signature; 30 | this.enum = keyName; 31 | } 32 | 33 | get signature() { 34 | return this.ed25519Signature || this.secp256k1Signature; 35 | } 36 | 37 | get signatureType(): KeyType { 38 | return this.signature.keyType; 39 | } 40 | 41 | get data(): Uint8Array { 42 | return this.signature.data; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /packages/transactions/test/data/signed_transaction1.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "SignedTransaction", 3 | "data": "09000000746573742e6e65617200917b3d268d4b58f7fec1b150bd68d69be3ee5d4cc39855e341538465bb77860d01000000000000000d00000077686174657665722e6e6561720fa473fd26901df296be6adc4cc4df34d040efa2435224b6986910e630c2fef601000000030100000000000000000000000000000000969a83332186ee9755e4839325525806e189a3d2d2bb4b4760e94443e97e1c4f22deeef0059a8e9713100eda6e19144da7e8a0ef7e539b20708ba1d8d021bd01" 4 | } 5 | -------------------------------------------------------------------------------- /packages/transactions/test/data/transaction1.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "Transaction", 3 | "data": "0000000000795cb7b5f57222e742d1759092f0e20071a0cd2bf30e1f681d800e67935e168801000000000000001000000073747564696f2d76776375396534316d4def837b838543990f3380af8e2a3817ddf70fe9960135b2add25a679b2a01ed01000000020a0000006164644d6573736167650b0000007b2274657874223a22227d80841e000000000000000000000000000000000000000000" 4 | } 5 | -------------------------------------------------------------------------------- /packages/transactions/test/transaction.test.ts: -------------------------------------------------------------------------------- 1 | import { expect, test } from '@jest/globals'; 2 | import { actionCreators } from '../src'; 3 | 4 | const { functionCall } = actionCreators; 5 | 6 | test('functionCall with already serialized args', () => { 7 | const serializedArgs = Buffer.from('{key: value}'); 8 | const action = functionCall('methodName', serializedArgs, 1n, 2n); 9 | expect(action).toMatchObject({ 10 | functionCall: { 11 | methodName: 'methodName', 12 | args: serializedArgs, 13 | gas: 1n, 14 | deposit: 2n 15 | } 16 | }); 17 | }); 18 | 19 | test('functionCall with non-serialized args', () => { 20 | const serializedArgs = Buffer.from(JSON.stringify({ key: 'value' })); 21 | const action = functionCall('methodName', { key: 'value' }, 1n, 2n); 22 | expect(action).toMatchObject({ 23 | functionCall: { 24 | methodName: 'methodName', 25 | args: serializedArgs, 26 | gas: 1n, 27 | deposit: 2n 28 | } 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /packages/transactions/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/transactions/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/transactions/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/transactions/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/tsconfig/base.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/tsconfig", 3 | "display": "Default", 4 | "compilerOptions": { 5 | "paths": { 6 | "@near-js/*": ["../packages/*"] 7 | }, 8 | "esModuleInterop": true, 9 | "module": "es2022", 10 | "target": "es2022", 11 | "lib": ["es2022", "DOM"], 12 | "moduleResolution": "node", 13 | "alwaysStrict": true, 14 | "declaration": true, 15 | "declarationMap": true, 16 | "preserveSymlinks": true, 17 | "preserveWatchOutput": true, 18 | "pretty": false, 19 | "forceConsistentCasingInFileNames": true, 20 | "noFallthroughCasesInSwitch": true, 21 | "noImplicitAny": false, 22 | "noImplicitReturns": true, 23 | "noUnusedLocals": true, 24 | "experimentalDecorators": true, 25 | "resolveJsonModule": true 26 | }, 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /packages/tsconfig/browser.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./base.json", 3 | "compilerOptions": { 4 | "lib": ["es2022", "dom"] 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /packages/tsconfig/cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./base.json", 3 | "target": "es6", 4 | "lib": ["es6"], 5 | "compilerOptions": { 6 | "module": "commonjs" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/tsconfig/esm.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./base.json" 3 | } 4 | -------------------------------------------------------------------------------- /packages/tsconfig/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tsconfig", 3 | "version": "0.0.0", 4 | "private": true, 5 | "files": [ 6 | "base.json", 7 | "nextjs.json", 8 | "react-library.json" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/types/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/types 2 | 3 | A collection of commonly-used classes and types. 4 | 5 | # License 6 | 7 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 8 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 9 | -------------------------------------------------------------------------------- /packages/types/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/types", 3 | "version": "2.0.2", 4 | "description": "TypeScript types for working with the Near JS API", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --fix", 13 | "check-exports": "attw --pack ." 14 | }, 15 | "keywords": [], 16 | "author": "", 17 | "license": "ISC", 18 | "devDependencies": { 19 | "@types/node": "20.0.0", 20 | "build": "workspace:*", 21 | "jest": "29.7.0", 22 | "ts-jest": "29.2.6", 23 | "tsconfig": "workspace:*", 24 | "typescript": "5.4.5" 25 | }, 26 | "files": [ 27 | "lib" 28 | ], 29 | "exports": { 30 | ".": { 31 | "import": "./lib/esm/index.js", 32 | "require": "./lib/commonjs/index.cjs" 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /packages/types/src/accounts.ts: -------------------------------------------------------------------------------- 1 | // TODO: Move Account and Provider to types 2 | interface ProviderLike { 3 | callFunction: (contractId, methodName, args, blockQuery?) => Promise; 4 | } 5 | 6 | export interface AccountLike { 7 | accountId: string; 8 | provider: ProviderLike; 9 | getState(): Promise; 10 | signAndSendTransaction({ 11 | receiverId, actions 12 | }): Promise; 13 | callFunction({ 14 | contractId, methodName, args, gas, deposit 15 | }): Promise; 16 | } -------------------------------------------------------------------------------- /packages/types/src/assignable.ts: -------------------------------------------------------------------------------- 1 | /* DEPRECATED - backward compatibility only */ 2 | export abstract class Assignable { 3 | constructor(properties: any) { 4 | Object.keys(properties).map((key: any) => { 5 | (this as any)[key] = properties[key]; 6 | }); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/types/src/enum.ts: -------------------------------------------------------------------------------- 1 | 2 | // TODO determine why subclassing is still necessary even though `enum` 3 | // cannot be set in the base class or it will not be borsh-serializable 4 | export abstract class Enum { 5 | abstract enum: string; 6 | 7 | constructor(properties: any) { 8 | if (Object.keys(properties).length !== 1) { 9 | throw new Error('Enum can only take single value'); 10 | } 11 | Object.keys(properties).map((key: string) => { 12 | (this as any)[key] = properties[key]; 13 | }); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /packages/types/src/errors.ts: -------------------------------------------------------------------------------- 1 | export class PositionalArgsError extends Error { 2 | constructor() { 3 | super('Contract method calls expect named arguments wrapped in object, e.g. { argName1: argValue1, argName2: argValue2 }'); 4 | } 5 | } 6 | 7 | export class ArgumentTypeError extends Error { 8 | constructor(argName: string, argType: string, argValue: any) { 9 | super(`Expected ${argType} for '${argName}' argument, but got '${JSON.stringify(argValue)}'`); 10 | } 11 | } 12 | 13 | export class TypedError extends Error { 14 | type: string; 15 | context?: ErrorContext; 16 | constructor(message?: string, type?: string, context?: ErrorContext) { 17 | super(message); 18 | this.type = type || 'UntypedError'; 19 | this.context = context; 20 | } 21 | } 22 | 23 | export class ErrorContext { 24 | transactionHash?: string; 25 | constructor(transactionHash?: string) { 26 | this.transactionHash = transactionHash; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /packages/types/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './accounts'; 2 | export * from './assignable'; 3 | export * from './enum'; 4 | export * from './errors'; 5 | export * from './provider'; 6 | -------------------------------------------------------------------------------- /packages/types/src/provider/index.ts: -------------------------------------------------------------------------------- 1 | 2 | export { 3 | IdType, 4 | LightClientBlockLiteView, 5 | LightClientProof, 6 | LightClientProofRequest, 7 | NextLightClientBlockRequest, 8 | NextLightClientBlockResponse, 9 | } from './light_client'; 10 | export { 11 | AccessKeyWithPublicKey, 12 | BlockHash, 13 | BlockChange, 14 | BlockChangeResult, 15 | BlockHeader, 16 | BlockHeaderInnerLiteView, 17 | BlockHeight, 18 | BlockId, 19 | BlockReference, 20 | BlockResult, 21 | BlockShardId, 22 | ChangeResult, 23 | Chunk, 24 | ChunkHash, 25 | ChunkHeader, 26 | ChunkId, 27 | ChunkResult, 28 | Finality, 29 | FinalityReference, 30 | GasPrice, 31 | MerkleNode, 32 | MerklePath, 33 | NearProtocolConfig, 34 | NearProtocolRuntimeConfig, 35 | NodeStatusResult, 36 | ShardId, 37 | SyncInfo, 38 | TotalWeight, 39 | Transaction as ProviderTransaction, 40 | TxExecutionStatus, 41 | } from './protocol'; 42 | export { 43 | CallFunctionRequest, 44 | RpcQueryRequest, 45 | ViewAccessKeyListRequest, 46 | ViewAccessKeyRequest, 47 | ViewAccountRequest, 48 | ViewCodeRequest, 49 | ViewStateRequest, 50 | } from './request'; 51 | export { 52 | AccessKeyInfoView, 53 | AccessKeyList, 54 | AccessKeyView, 55 | AccessKeyViewRaw, 56 | AccountView, 57 | AccountViewRaw, 58 | AccountBalanceInfo, 59 | CodeResult, 60 | ContractCodeView, 61 | ContractCodeViewRaw, 62 | ExecutionError, 63 | ExecutionOutcome, 64 | ExecutionOutcomeWithId, 65 | ExecutionOutcomeWithIdView, 66 | ExecutionStatus, 67 | ExecutionStatusBasic, 68 | FinalExecutionOutcome, 69 | FinalExecutionStatus, 70 | FinalExecutionStatusBasic, 71 | FunctionCallPermissionView, 72 | QueryResponseKind, 73 | SerializedReturnValue, 74 | ViewStateResult, 75 | ExecutionOutcomeReceiptDetail, 76 | ContractStateView, 77 | CallContractViewFunctionResultRaw, 78 | } from './response'; 79 | export { 80 | CurrentEpochValidatorInfo, 81 | EpochValidatorInfo, 82 | NextEpochValidatorInfo, 83 | StakedAccount, 84 | ValidatorStakeView 85 | } from './validator'; 86 | -------------------------------------------------------------------------------- /packages/types/src/provider/light_client.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * NEAR RPC API request types and responses 3 | * @module 4 | */ 5 | 6 | import { BlockHeaderInnerLiteView, MerklePath } from './protocol'; 7 | import { ExecutionOutcomeWithIdView } from './response'; 8 | import { ValidatorStakeView } from './validator'; 9 | 10 | export interface LightClientBlockLiteView { 11 | prev_block_hash: string; 12 | inner_rest_hash: string; 13 | inner_lite: BlockHeaderInnerLiteView; 14 | } 15 | 16 | export interface LightClientProof { 17 | outcome_proof: ExecutionOutcomeWithIdView; 18 | outcome_root_proof: MerklePath; 19 | block_header_lite: LightClientBlockLiteView; 20 | block_proof: MerklePath; 21 | } 22 | 23 | export enum IdType { 24 | Transaction = 'transaction', 25 | Receipt = 'receipt', 26 | } 27 | 28 | export interface LightClientProofRequest { 29 | type: IdType; 30 | light_client_head: string; 31 | transaction_hash?: string; 32 | sender_id?: string; 33 | receipt_id?: string; 34 | receiver_id?: string; 35 | } 36 | 37 | export interface NextLightClientBlockResponse { 38 | prev_block_hash: string; 39 | next_block_inner_hash: string; 40 | inner_lite: BlockHeaderInnerLiteView; 41 | inner_rest_hash: string; 42 | next_bps?: ValidatorStakeView[]; 43 | approvals_after_next: (string | null)[]; 44 | } 45 | 46 | export interface NextLightClientBlockRequest { 47 | last_block_hash: string; 48 | } 49 | -------------------------------------------------------------------------------- /packages/types/src/provider/request.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * NEAR RPC API request types and responses 3 | * @module 4 | */ 5 | 6 | import { BlockReference } from './protocol'; 7 | 8 | export interface ViewAccountRequest { 9 | request_type: 'view_account'; 10 | account_id: string; 11 | } 12 | 13 | export interface ViewCodeRequest { 14 | request_type: 'view_code'; 15 | account_id: string; 16 | } 17 | 18 | export interface ViewStateRequest { 19 | request_type: 'view_state'; 20 | account_id: string; 21 | prefix_base64: string; 22 | } 23 | 24 | export interface ViewAccessKeyRequest { 25 | request_type: 'view_access_key'; 26 | account_id: string; 27 | public_key: string; 28 | } 29 | 30 | export interface ViewAccessKeyListRequest { 31 | request_type: 'view_access_key_list'; 32 | account_id: string; 33 | } 34 | 35 | export interface CallFunctionRequest { 36 | request_type: 'call_function'; 37 | account_id: string; 38 | method_name: string; 39 | args_base64: string; 40 | } 41 | 42 | export type RpcQueryRequest = (ViewAccountRequest | 43 | ViewCodeRequest | 44 | ViewStateRequest | 45 | ViewAccountRequest | 46 | ViewAccessKeyRequest | 47 | ViewAccessKeyListRequest | 48 | CallFunctionRequest) & BlockReference 49 | -------------------------------------------------------------------------------- /packages/types/src/provider/validator.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * NEAR RPC API request types and responses 3 | * @module 4 | */ 5 | 6 | export interface CurrentEpochValidatorInfo { 7 | account_id: string; 8 | public_key: string; 9 | is_slashed: boolean; 10 | stake: string; 11 | shards: number[]; 12 | num_produced_blocks: number; 13 | num_expected_blocks: number; 14 | } 15 | 16 | export interface NextEpochValidatorInfo { 17 | account_id: string; 18 | public_key: string; 19 | stake: string; 20 | shards: number[]; 21 | } 22 | 23 | export interface ValidatorStakeView { 24 | account_id: string; 25 | public_key: string; 26 | stake: string; 27 | validator_stake_struct_version: string; 28 | } 29 | 30 | export interface EpochValidatorInfo { 31 | // Validators for the current epoch. 32 | next_validators: NextEpochValidatorInfo[]; 33 | // Validators for the next epoch. 34 | current_validators: CurrentEpochValidatorInfo[]; 35 | // Fishermen for the current epoch. 36 | next_fisherman: ValidatorStakeView[]; 37 | // Fishermen for the next epoch. 38 | current_fisherman: ValidatorStakeView[]; 39 | // Proposals in the current epoch. 40 | current_proposals: ValidatorStakeView[]; 41 | // Kickout in the previous epoch. 42 | prev_epoch_kickout: ValidatorStakeView[]; 43 | // Epoch start height. 44 | epoch_start_height: number; 45 | } 46 | 47 | export interface StakedAccount { 48 | account_id: string; 49 | unstaked_balance: string; 50 | staked_balance: string; 51 | can_withdraw: boolean; 52 | } 53 | -------------------------------------------------------------------------------- /packages/types/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/types/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/types/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*.ts"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | }, { 15 | splitting: false, 16 | bundle: false, 17 | entryPoints: ["src/**/*.ts"], 18 | format: ["esm"], 19 | outDir: "lib/esm", 20 | dts: true, 21 | clean: true, 22 | target: "es2022", 23 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 24 | }]); 25 | -------------------------------------------------------------------------------- /packages/types/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src", "src/provider" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /packages/utils/README.md: -------------------------------------------------------------------------------- 1 | # @near-js/utils 2 | 3 | A collection of commonly-used functions and constants. 4 | 5 | ## Modules 6 | 7 | - [Format](https://github.com/near/near-api-js/blob/master/packages/utils/src/format.ts) NEAR denomination formatting functions 8 | - [Logging](https://github.com/near/near-api-js/blob/master/packages/utils/src/logging.ts) functions for printing formatted RPC output 9 | - [Provider](https://github.com/near/near-api-js/blob/master/packages/utils/src/provider.ts) functions for parsing RPC output 10 | - [Validators](https://github.com/near/near-api-js/blob/master/packages/utils/src/validators.ts) functions for querying blockchain validators 11 | 12 | # License 13 | 14 | This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 15 | See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. 16 | -------------------------------------------------------------------------------- /packages/utils/fetch_error_schema.js: -------------------------------------------------------------------------------- 1 | const https = require('https'); 2 | const fs = require('fs'); 3 | 4 | const ERROR_SCHEMA_URL = 5 | 'https://raw.githubusercontent.com/near/nearcore/master/chain/jsonrpc/res/rpc_errors_schema.json'; 6 | const TARGET_SCHEMA_FILE_PATH = `${process.argv[2] || process.cwd()}/src/errors/rpc_error_schema.json`; 7 | 8 | https 9 | .get(ERROR_SCHEMA_URL, resp => { 10 | let data = ''; 11 | 12 | resp.on('data', chunk => { 13 | data += chunk; 14 | }); 15 | 16 | resp.on('end', () => { 17 | fs.writeFileSync(TARGET_SCHEMA_FILE_PATH, data); 18 | }); 19 | }) 20 | .on('error', err => { 21 | console.log('Unable to fetch schema file: ' + err.message); 22 | }); 23 | -------------------------------------------------------------------------------- /packages/utils/jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | preset: 'ts-jest', 3 | collectCoverage: true, 4 | testEnvironment: 'node', 5 | testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.[jt]sx?$", 6 | transform: { 7 | '^.+\\.[tj]s$': ['ts-jest', { 8 | tsconfig: { 9 | allowJs: true, 10 | }, 11 | }], 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /packages/utils/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@near-js/utils", 3 | "version": "2.0.2", 4 | "description": "Common methods and constants for the NEAR API JavaScript client", 5 | "main": "lib/commonjs/index.cjs", 6 | "module": "lib/esm/index.js", 7 | "types": "./lib/esm/index.d.ts", 8 | "type": "module", 9 | "scripts": { 10 | "build": "tsup", 11 | "lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc", 12 | "lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix", 13 | "test": "jest", 14 | "check-exports": "attw --pack ." 15 | }, 16 | "keywords": [], 17 | "author": "", 18 | "license": "ISC", 19 | "dependencies": { 20 | "@near-js/types": "workspace:*", 21 | "@scure/base": "^1.2.4", 22 | "depd": "2.0.0", 23 | "mustache": "4.0.0" 24 | }, 25 | "devDependencies": { 26 | "@jest/globals": "^29.7.0", 27 | "@types/node": "20.0.0", 28 | "build": "workspace:*", 29 | "glob": "^11.0.2", 30 | "jest": "29.7.0", 31 | "ts-jest": "29.2.6", 32 | "tsconfig": "workspace:*", 33 | "typescript": "5.4.5" 34 | }, 35 | "peerDependencies": { 36 | "@near-js/types": "^2.0.1" 37 | }, 38 | "files": [ 39 | "lib" 40 | ], 41 | "exports": { 42 | ".": { 43 | "import": "./lib/esm/index.js", 44 | "require": "./lib/commonjs/index.cjs" 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /packages/utils/src/constants.ts: -------------------------------------------------------------------------------- 1 | // Default amount of gas to be sent with the function calls. Used to pay for the fees 2 | // incurred while running the contract execution. The unused amount will be refunded back to 3 | // the originator. 4 | // Due to protocol changes that charge upfront for the maximum possible gas price inflation due to 5 | // full blocks, the price of max_prepaid_gas is decreased to `300 * 10**12`. 6 | // For discussion see https://github.com/nearprotocol/NEPs/issues/67 7 | export const DEFAULT_FUNCTION_CALL_GAS = 30000000000000n; -------------------------------------------------------------------------------- /packages/utils/src/errors/errors.ts: -------------------------------------------------------------------------------- 1 | import ErrorMessagesJson from './error_messages.json'; 2 | 3 | export const ErrorMessages: { [error: string]: string } = ErrorMessagesJson; 4 | -------------------------------------------------------------------------------- /packages/utils/src/errors/index.ts: -------------------------------------------------------------------------------- 1 | export { ErrorMessages } from './errors'; 2 | export { 3 | ServerError, 4 | formatError, 5 | getErrorTypeFromErrorMessage, 6 | parseResultError, 7 | parseRpcError, 8 | } from './rpc_errors'; 9 | -------------------------------------------------------------------------------- /packages/utils/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './constants'; 2 | export * from './errors'; 3 | export * from './format'; 4 | export * from './logging'; 5 | export * from './provider'; 6 | export * from './validators'; 7 | export * from './logger'; 8 | export * from './utils'; -------------------------------------------------------------------------------- /packages/utils/src/logger/console.logger.ts: -------------------------------------------------------------------------------- 1 | import type { LoggerService, LogLevel } from './interface'; 2 | 3 | export class ConsoleLogger implements LoggerService { 4 | public constructor(protected readonly logLevels: LogLevel[]) {} 5 | 6 | public isLevelEnabled = (level: LogLevel): boolean => { 7 | return this.logLevels.includes(level); 8 | }; 9 | 10 | private print( 11 | level: LogLevel, 12 | message: any, 13 | ...optionalParams: any[] 14 | ): void { 15 | switch (level) { 16 | case 'error': 17 | case 'fatal': 18 | return console.error(message, ...optionalParams); 19 | case 'warn': 20 | return console.warn(message, ...optionalParams); 21 | case 'log': 22 | return console.log(message, ...optionalParams); 23 | case 'debug': 24 | case 'verbose': 25 | return console.debug(message, ...optionalParams); 26 | } 27 | } 28 | 29 | verbose(message: any, ...optionalParams: any[]) { 30 | if (!this.isLevelEnabled('verbose')) return; 31 | 32 | this.print('verbose', message, ...optionalParams); 33 | } 34 | 35 | debug(message: any, ...optionalParams: any[]) { 36 | if (!this.isLevelEnabled('debug')) return; 37 | 38 | this.print('debug', message, ...optionalParams); 39 | } 40 | 41 | log(message: any, ...optionalParams: any[]) { 42 | if (!this.isLevelEnabled('log')) return; 43 | 44 | this.print('log', message, ...optionalParams); 45 | } 46 | 47 | warn(message: any, ...optionalParams: any[]) { 48 | if (!this.isLevelEnabled('warn')) return; 49 | 50 | this.print('warn', message, ...optionalParams); 51 | } 52 | 53 | error(message: any, ...optionalParams: any[]) { 54 | if (!this.isLevelEnabled('error')) return; 55 | 56 | this.print('error', message, ...optionalParams); 57 | } 58 | 59 | fatal(message: any, ...optionalParams: any[]) { 60 | if (!this.isLevelEnabled('fatal')) return; 61 | 62 | this.print('fatal', message, ...optionalParams); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /packages/utils/src/logger/index.ts: -------------------------------------------------------------------------------- 1 | export { ConsoleLogger } from './console.logger'; 2 | export { Logger } from './logger'; 3 | export type { LoggerService } from './interface'; 4 | -------------------------------------------------------------------------------- /packages/utils/src/logger/interface.ts: -------------------------------------------------------------------------------- 1 | export type LogLevel = 'log' | 'error' | 'warn' | 'debug' | 'verbose' | 'fatal'; 2 | 3 | export interface LoggerService { 4 | /** 5 | * Write a 'log' level log. 6 | */ 7 | log(message: any, ...optionalParams: any[]): any; 8 | 9 | /** 10 | * Write an 'error' level log. 11 | */ 12 | error(message: any, ...optionalParams: any[]): any; 13 | 14 | /** 15 | * Write a 'warn' level log. 16 | */ 17 | warn(message: any, ...optionalParams: any[]): any; 18 | 19 | /** 20 | * Write a 'debug' level log. 21 | */ 22 | debug?(message: any, ...optionalParams: any[]): any; 23 | 24 | /** 25 | * Write a 'verbose' level log. 26 | */ 27 | verbose?(message: any, ...optionalParams: any[]): any; 28 | 29 | /** 30 | * Write a 'fatal' level log. 31 | */ 32 | fatal?(message: any, ...optionalParams: any[]): any; 33 | } 34 | -------------------------------------------------------------------------------- /packages/utils/src/logger/logger.ts: -------------------------------------------------------------------------------- 1 | import { ConsoleLogger } from './console.logger'; 2 | import type { LogLevel, LoggerService } from './interface'; 3 | 4 | const DEFAULT_LOG_LEVELS: LogLevel[] = [ 5 | 'verbose', 6 | 'debug', 7 | 'log', 8 | 'warn', 9 | 'error', 10 | 'fatal', 11 | ]; 12 | 13 | const DEFAULT_LOGGER = new ConsoleLogger(DEFAULT_LOG_LEVELS); 14 | 15 | /** 16 | * Used to log the library messages 17 | */ 18 | export class Logger { 19 | protected static instanceRef?: LoggerService = DEFAULT_LOGGER; 20 | 21 | public static overrideLogger = (logger?: LoggerService): void => { 22 | this.instanceRef = logger; 23 | }; 24 | 25 | /** 26 | * Write an 'error' level log. 27 | */ 28 | public static error(message: any, stack?: string): void; 29 | public static error(message: any, ...optionalParams: [string, ...any[]]): void; 30 | public static error(message: any, ...optionalParams: any[]) { 31 | this.instanceRef?.error(message, ...optionalParams); 32 | } 33 | 34 | /** 35 | * Write a 'log' level log. 36 | */ 37 | public static log(message: any, ...optionalParams: any[]) { 38 | this.instanceRef?.log(message, ...optionalParams); 39 | } 40 | 41 | /** 42 | * Write a 'warn' level log. 43 | */ 44 | public static warn(message: any, ...optionalParams: any[]) { 45 | this.instanceRef?.warn(message, ...optionalParams); 46 | } 47 | 48 | /** 49 | * Write a 'debug' level log. 50 | */ 51 | public static debug(message: any, ...optionalParams: any[]) { 52 | this.instanceRef?.debug?.(message, ...optionalParams); 53 | } 54 | 55 | /** 56 | * Write a 'verbose' level log. 57 | */ 58 | public static verbose(message: any, ...optionalParams: any[]) { 59 | this.instanceRef?.verbose?.(message, ...optionalParams); 60 | } 61 | 62 | /** 63 | * Write a 'fatal' level log. 64 | */ 65 | public static fatal(message: any, stack?: string): void; 66 | public static fatal(message: any, ...optionalParams: [string, ...any[]]): void; 67 | public static fatal(message: any, ...optionalParams: any[]) { 68 | this.instanceRef?.fatal?.(message, ...optionalParams); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /packages/utils/src/provider.ts: -------------------------------------------------------------------------------- 1 | import { FinalExecutionOutcome } from '@near-js/types'; 2 | 3 | /** @hidden */ 4 | export function getTransactionLastResult(txResult: FinalExecutionOutcome): Exclude { 5 | if (typeof txResult.status === 'object' && typeof txResult.status.SuccessValue === 'string') { 6 | const value = Buffer.from(txResult.status.SuccessValue, 'base64').toString(); 7 | try { 8 | return JSON.parse(value); 9 | } catch (e) { 10 | return value; 11 | } 12 | } 13 | return null; 14 | } 15 | -------------------------------------------------------------------------------- /packages/utils/src/utils.ts: -------------------------------------------------------------------------------- 1 | export function sortBigIntAsc(a: bigint, b: bigint) { 2 | return (a < b ? -1 : a > b ? 1 : 0) 3 | } -------------------------------------------------------------------------------- /packages/utils/test/logger.test.ts: -------------------------------------------------------------------------------- 1 | import { beforeEach, describe, expect, test } from '@jest/globals'; 2 | import { Logger } from '../src'; 3 | 4 | 5 | describe('logger', () => { 6 | let logs; 7 | 8 | beforeEach(async () => { 9 | logs = []; 10 | 11 | const custom = { 12 | verbose: (...args) => { 13 | logs.push(args.join('')); 14 | }, 15 | debug: (...args) => { 16 | logs.push(args.join('')); 17 | }, 18 | log: (...args) => { 19 | logs.push(args.join('')); 20 | }, 21 | warn: (...args) => { 22 | logs.push(args.join('')); 23 | }, 24 | error: (...args) => { 25 | logs.push(args.join('')); 26 | }, 27 | fatal: (...args) => { 28 | logs.push(args.join('')); 29 | }, 30 | }; 31 | 32 | Logger.overrideLogger(custom); 33 | }); 34 | 35 | test('test logger can be overrided', async () => { 36 | Logger.log('111'); 37 | Logger.debug('11s1'); 38 | Logger.warn('1111'); 39 | Logger.error('1131'); 40 | Logger.log('112'); 41 | 42 | expect(logs.length).toBe(5); 43 | }); 44 | 45 | test('test logger can be disabled', async () => { 46 | Logger.overrideLogger(undefined); 47 | 48 | Logger.log('111'); 49 | Logger.log('222'); 50 | 51 | expect(logs.length).toBe(0); 52 | }); 53 | 54 | test('test logged data is accurate', async () => { 55 | Logger.log('lol'); 56 | expect(logs[0]).toEqual('lol'); 57 | 58 | Logger.log('his name is ', 'test'); 59 | expect(logs[1]).toEqual('his name is test'); 60 | }); 61 | }); 62 | -------------------------------------------------------------------------------- /packages/utils/test/validator.test.ts: -------------------------------------------------------------------------------- 1 | import { expect, test } from '@jest/globals'; 2 | import { diffEpochValidators, findSeatPrice } from '../src'; 3 | 4 | test('find seat price', async () => { 5 | expect(findSeatPrice( 6 | // @ts-expect-error test input 7 | [{ stake: '1000000' }, { stake: '1000000' }, { stake: '100' }], 2, [1, 6250], 49 8 | )).toEqual(101n); 9 | expect(findSeatPrice( 10 | // @ts-expect-error test input 11 | [{ stake: '1000000' }, { stake: '1000000' }, { stake: '100' }], 3, [1, 6250] 12 | )).toEqual(101n); 13 | expect(findSeatPrice( 14 | // @ts-expect-error test input 15 | [{ stake: '1000000' }, { stake: '1000000' }, { stake: '100' }], 4, [1, 6250], 49 16 | )).toEqual(320n); 17 | expect(findSeatPrice( 18 | // @ts-expect-error test input 19 | [{ stake: '1000000' }, { stake: '1000000' }, { stake: '100' }], 4, [1, 6250], 48 20 | )).toEqual(500000n); 21 | expect(findSeatPrice( 22 | // @ts-expect-error test input 23 | [{ stake: '1000' }, { stake: '1000' }, { stake: '200' }], 100, [1, 25] 24 | )).toEqual(88n); 25 | }); 26 | 27 | test('diff validators', async () => { 28 | expect(diffEpochValidators( 29 | // @ts-expect-error test input 30 | [{ account_id: 'x', stake: '10' }], 31 | [{ account_id: 'x', stake: '10' }] 32 | )).toEqual({ newValidators: [], removedValidators: [], changedValidators: [] }); 33 | expect(diffEpochValidators( 34 | // @ts-expect-error test input 35 | [{ account_id: 'x', stake: '10' }, { account_id: 'y', stake: '10' }], 36 | [{ account_id: 'x', stake: '11' }, { account_id: 'z', stake: '11' }] 37 | )).toEqual({ 38 | newValidators: [{ account_id: 'z', stake: '11' }], 39 | removedValidators: [{ account_id: 'y', stake: '10' }], 40 | changedValidators: [{ 41 | current: { account_id: 'x', stake: '10' }, 42 | next: { account_id: 'x', stake: '11' } 43 | }] 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /packages/utils/tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/cjs.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/commonjs", 5 | "lib": ["es2022", "dom"] 6 | }, 7 | "files": [ 8 | "src/index.ts" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /packages/utils/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tsconfig/esm.json", 3 | "compilerOptions": { 4 | "outDir": "./lib/esm", 5 | }, 6 | "files": [ 7 | "src/index.ts" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/utils/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | import { fixFolderImportsPlugin, fixExtensionsPlugin } from 'esbuild-fix-imports-plugin'; 3 | 4 | export default defineConfig([{ 5 | splitting: false, 6 | bundle: false, 7 | entryPoints: ["src/**/*"], 8 | format: ["cjs"], 9 | outDir: "lib/commonjs", 10 | clean: true, 11 | dts: true, 12 | target: "es2022", 13 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 14 | loader: { 15 | ".json": "copy", 16 | } 17 | }, { 18 | splitting: false, 19 | bundle: false, 20 | entryPoints: ["src/**/*"], 21 | format: ["esm"], 22 | outDir: "lib/esm", 23 | dts: true, 24 | clean: true, 25 | target: "es2022", 26 | esbuildPlugins: [fixFolderImportsPlugin(), fixExtensionsPlugin()], 27 | loader: { 28 | ".json": "copy", 29 | } 30 | }]); 31 | -------------------------------------------------------------------------------- /packages/utils/typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "../../typedoc.json" 4 | ], 5 | "entryPoints": [ 6 | "src" 7 | ], 8 | "entryPointStrategy": "expand", 9 | } -------------------------------------------------------------------------------- /pnpm-workspace.yaml: -------------------------------------------------------------------------------- 1 | packages: 2 | - "packages/*" 3 | -------------------------------------------------------------------------------- /turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://turborepo.org/schema.json", 3 | "tasks": { 4 | "build": { 5 | "dependsOn": ["^build"], 6 | "outputs": ["dist/**", "lib/**"] 7 | }, 8 | "test": { 9 | "dependsOn": ["^transit"], 10 | "outputs": [] 11 | }, 12 | "lint": { 13 | "dependsOn": ["^transit"], 14 | "outputs": [] 15 | }, 16 | "lint:fix": { 17 | "dependsOn": ["^transit"], 18 | "outputs": [] 19 | }, 20 | "transit": { 21 | "dependsOn": ["^transit"] 22 | }, 23 | "clean": { 24 | "outputs": [], 25 | "cache": false 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /typedoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "includeVersion": true, 3 | "entryPoints": [ 4 | "packages/near-api-js", 5 | "packages/accounts", 6 | "packages/biometric-ed25519", 7 | "packages/crypto", 8 | "packages/iframe-rpc", 9 | "packages/keystores", 10 | "packages/keystores-browser", 11 | "packages/keystores-node", 12 | "packages/providers", 13 | "packages/signers", 14 | "packages/tokens", 15 | "packages/transactions", 16 | "packages/types", 17 | "packages/utils", 18 | "packages/wallet-account" 19 | ], 20 | "entryPointStrategy": "packages", 21 | "githubPages": true, 22 | "validation": { 23 | "notExported": false 24 | }, 25 | "out": "docs", 26 | "readme": "./docs/README_TYPEDOC.md" 27 | } --------------------------------------------------------------------------------