diff --git a/packages/@magic-ext/hedera/.eslintignore b/packages/@magic-ext/hedera/.eslintignore new file mode 100644 index 000000000..5807c501b --- /dev/null +++ b/packages/@magic-ext/hedera/.eslintignore @@ -0,0 +1,5 @@ +/node_modules +/coverage +/dist +/.eslintrc.js +/jest.config.ts diff --git a/packages/@magic-ext/hedera/.eslintrc.js b/packages/@magic-ext/hedera/.eslintrc.js new file mode 100644 index 000000000..19dc27005 --- /dev/null +++ b/packages/@magic-ext/hedera/.eslintrc.js @@ -0,0 +1,7 @@ +module.exports = { + extends: ['../../../.eslintrc.js'], + parserOptions: { + project: ['./tsconfig.json'], + tsconfigRootDir: __dirname, + }, +} diff --git a/packages/@magic-ext/hedera/.lintstagedrc.yml b/packages/@magic-ext/hedera/.lintstagedrc.yml new file mode 100644 index 000000000..1c250ad65 --- /dev/null +++ b/packages/@magic-ext/hedera/.lintstagedrc.yml @@ -0,0 +1,2 @@ +'*.{ts,tsx}': + - eslint --fix diff --git a/packages/@magic-ext/hedera/.prettierrc.js b/packages/@magic-ext/hedera/.prettierrc.js new file mode 100644 index 000000000..6177cac66 --- /dev/null +++ b/packages/@magic-ext/hedera/.prettierrc.js @@ -0,0 +1 @@ +module.exports = require('../../../.prettierrc.js'); diff --git a/packages/@magic-ext/hedera/CHANGELOG.md b/packages/@magic-ext/hedera/CHANGELOG.md new file mode 100644 index 000000000..e69de29bb diff --git a/packages/@magic-ext/hedera/LICENSE b/packages/@magic-ext/hedera/LICENSE new file mode 100644 index 000000000..81d6d2df4 --- /dev/null +++ b/packages/@magic-ext/hedera/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018-present Magic Labs, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/@magic-ext/hedera/package.json b/packages/@magic-ext/hedera/package.json new file mode 100644 index 000000000..c48d762b6 --- /dev/null +++ b/packages/@magic-ext/hedera/package.json @@ -0,0 +1,33 @@ +{ + "name": "@magic-ext/hedera", + "version": "0.1.0", + "description": "magic hedera extension", + "author": "Magic (https://magic.link/)", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/magiclabs/magic-js" + }, + "files": [ + "dist" + ], + "target": "neutral", + "cdnGlobalName": "MagicHederaExtension", + "main": "./dist/cjs/index.js", + "module": "./dist/es/index.js", + "types": "./dist/types/index.d.ts", + "jsdelivr": "./dist/extension.js", + "react-native": "./dist/react-native/index.native.js", + "exports": { + "import": "./dist/es/index.mjs", + "require": "./dist/cjs/index.js" + }, + "externals": { + "include": [ + "@magic-sdk/commons" + ] + }, + "devDependencies": { + "@magic-sdk/commons": "^14.6.0" + } +} diff --git a/packages/@magic-ext/hedera/src/index.cdn.ts b/packages/@magic-ext/hedera/src/index.cdn.ts new file mode 100644 index 000000000..a5602cd0f --- /dev/null +++ b/packages/@magic-ext/hedera/src/index.cdn.ts @@ -0,0 +1,3 @@ +import { HederaExtension } from './index'; + +export default HederaExtension; diff --git a/packages/@magic-ext/hedera/src/index.native.ts b/packages/@magic-ext/hedera/src/index.native.ts new file mode 100644 index 000000000..ea465c2a3 --- /dev/null +++ b/packages/@magic-ext/hedera/src/index.native.ts @@ -0,0 +1 @@ +export * from './index'; diff --git a/packages/@magic-ext/hedera/src/index.ts b/packages/@magic-ext/hedera/src/index.ts new file mode 100644 index 000000000..a17acf22f --- /dev/null +++ b/packages/@magic-ext/hedera/src/index.ts @@ -0,0 +1,30 @@ +import { Extension } from '@magic-sdk/commons'; +import { HederaConfig, HederaPayloadMethod } from './types'; + +export * from './utils'; + +export class HederaExtension extends Extension.Internal<'hedera', any> { + name = 'hedera' as const; + config: any = {}; + network: string; + + constructor(public hederaConfig: HederaConfig) { + super(); + + this.network = hederaConfig.network; + this.config = { + chainType: 'HEDERA', + options: { + network: hederaConfig.network, + }, + }; + } + + public async getPublicKey() { + return this.request(this.utils.createJsonRpcRequestPayload(HederaPayloadMethod.HederaGetPublicKey, [])); + } + + public async sign(message: Uint8Array) { + return this.request(this.utils.createJsonRpcRequestPayload(HederaPayloadMethod.HederaSign, [{ message }])); + } +} diff --git a/packages/@magic-ext/hedera/src/types.ts b/packages/@magic-ext/hedera/src/types.ts new file mode 100644 index 000000000..45cdd756b --- /dev/null +++ b/packages/@magic-ext/hedera/src/types.ts @@ -0,0 +1,8 @@ +export interface HederaConfig { + network: string; +} + +export enum HederaPayloadMethod { + HederaSign = 'hedera_sign', + HederaGetPublicKey = 'hedera_getPublicKey', +} diff --git a/packages/@magic-ext/hedera/src/utils.ts b/packages/@magic-ext/hedera/src/utils.ts new file mode 100644 index 000000000..3f394f37d --- /dev/null +++ b/packages/@magic-ext/hedera/src/utils.ts @@ -0,0 +1,35 @@ +/** + * @param {Uint8Array} data + * @returns {string} + */ +export function encode(data: Uint8Array) { + return Buffer.from(data).toString('hex'); +} + +/** + * @param {string} text + * @returns {Uint8Array} + */ +export function decode(text: string) { + const str = text.startsWith('0x') ? text.substring(2) : text; + return Buffer.from(str, 'hex'); +} + +export function shuffle(array: any) { + let currentIndex = array.length; + let temporaryValue; + let randomIndex; + + // While there remain elements to shuffle... + while (currentIndex !== 0) { + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + + // And swap it with the current element. + temporaryValue = array[currentIndex]; + // eslint-disable-next-line no-param-reassign + array[currentIndex] = array[randomIndex]; + // eslint-disable-next-line no-param-reassign + array[randomIndex] = temporaryValue; + } +} diff --git a/packages/@magic-ext/hedera/tsconfig.json b/packages/@magic-ext/hedera/tsconfig.json new file mode 100644 index 000000000..15ac3df48 --- /dev/null +++ b/packages/@magic-ext/hedera/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../tsconfig.settings.json", + "exclude": [ + "temple" + ] +} + diff --git a/yarn.lock b/yarn.lock index 1ee922691..615b5a63a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2817,7 +2817,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/algorand@workspace:packages/@magic-ext/algorand" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2826,8 +2826,8 @@ __metadata: resolution: "@magic-ext/aptos@workspace:packages/@magic-ext/aptos" dependencies: "@aptos-labs/wallet-adapter-core": ^2.2.0 - "@magic-sdk/commons": ^14.6.0 - "@magic-sdk/provider": ^18.6.0 + "@magic-sdk/commons": ^15.0.0 + "@magic-sdk/provider": ^19.0.0 aptos: ^1.8.5 peerDependencies: "@aptos-labs/wallet-adapter-core": ^2.2.0 @@ -2839,7 +2839,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/auth@workspace:packages/@magic-ext/auth" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2847,7 +2847,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/avalanche@workspace:packages/@magic-ext/avalanche" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2855,7 +2855,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/bitcoin@workspace:packages/@magic-ext/bitcoin" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2863,7 +2863,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/conflux@workspace:packages/@magic-ext/conflux" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2871,7 +2871,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/cosmos@workspace:packages/@magic-ext/cosmos" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2879,7 +2879,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/ed25519@workspace:packages/@magic-ext/ed25519" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2887,7 +2887,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/flow@workspace:packages/@magic-ext/flow" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 "@onflow/fcl": ^1.4.1 "@onflow/types": ^1.1.0 peerDependencies: @@ -2900,8 +2900,8 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/gdkms@workspace:packages/@magic-ext/gdkms" dependencies: - "@magic-sdk/commons": ^14.6.0 - "@magic-sdk/types": ^15.8.0 + "@magic-sdk/commons": ^15.0.0 + "@magic-sdk/types": ^16.0.0 "@peculiar/webcrypto": ^1.4.3 languageName: unknown linkType: soft @@ -2909,6 +2909,14 @@ __metadata: "@magic-ext/harmony@workspace:packages/@magic-ext/harmony": version: 0.0.0-use.local resolution: "@magic-ext/harmony@workspace:packages/@magic-ext/harmony" + dependencies: + "@magic-sdk/commons": ^15.0.0 + languageName: unknown + linkType: soft + +"@magic-ext/hedera@workspace:packages/@magic-ext/hedera": + version: 0.0.0-use.local + resolution: "@magic-ext/hedera@workspace:packages/@magic-ext/hedera" dependencies: "@magic-sdk/commons": ^14.6.0 languageName: unknown @@ -2918,7 +2926,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/icon@workspace:packages/@magic-ext/icon" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2926,18 +2934,18 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/near@workspace:packages/@magic-ext/near" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft -"@magic-ext/oauth@^12.6.0, @magic-ext/oauth@workspace:packages/@magic-ext/oauth": +"@magic-ext/oauth@^13.0.0, @magic-ext/oauth@workspace:packages/@magic-ext/oauth": version: 0.0.0-use.local resolution: "@magic-ext/oauth@workspace:packages/@magic-ext/oauth" dependencies: - "@magic-sdk/types": ^15.8.0 + "@magic-sdk/types": ^16.0.0 "@types/crypto-js": ~3.1.47 crypto-js: ^3.3.0 - magic-sdk: ^18.6.0 + magic-sdk: ^19.0.0 languageName: unknown linkType: soft @@ -2945,7 +2953,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/oidc@workspace:packages/@magic-ext/oidc" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2953,7 +2961,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/polkadot@workspace:packages/@magic-ext/polkadot" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -2961,7 +2969,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/react-native-bare-oauth@workspace:packages/@magic-ext/react-native-bare-oauth" dependencies: - "@magic-sdk/react-native-bare": ^19.6.0 + "@magic-sdk/react-native-bare": ^20.0.0 "@magic-sdk/types": ^10.0.1 "@types/crypto-js": ~3.1.47 crypto-js: ^3.3.0 @@ -2977,7 +2985,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/react-native-expo-oauth@workspace:packages/@magic-ext/react-native-expo-oauth" dependencies: - "@magic-sdk/react-native-expo": ^19.6.0 + "@magic-sdk/react-native-expo": ^20.0.0 "@magic-sdk/types": ^10.0.0 "@types/crypto-js": ~3.1.47 crypto-js: ^3.3.0 @@ -2992,7 +3000,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/solana@workspace:packages/@magic-ext/solana" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -3000,7 +3008,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/taquito@workspace:packages/@magic-ext/taquito" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -3008,7 +3016,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/terra@workspace:packages/@magic-ext/terra" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -3016,7 +3024,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/tezos@workspace:packages/@magic-ext/tezos" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -3024,7 +3032,7 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/webauthn@workspace:packages/@magic-ext/webauthn" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft @@ -3032,22 +3040,32 @@ __metadata: version: 0.0.0-use.local resolution: "@magic-ext/zilliqa@workspace:packages/@magic-ext/zilliqa" dependencies: - "@magic-sdk/commons": ^14.6.0 + "@magic-sdk/commons": ^15.0.0 languageName: unknown linkType: soft -"@magic-sdk/commons@^14.6.0, @magic-sdk/commons@workspace:packages/@magic-sdk/commons": +"@magic-sdk/commons@^15.0.0, @magic-sdk/commons@workspace:packages/@magic-sdk/commons": version: 0.0.0-use.local resolution: "@magic-sdk/commons@workspace:packages/@magic-sdk/commons" dependencies: - "@magic-sdk/provider": ^18.6.0 - "@magic-sdk/types": ^15.8.0 + "@magic-sdk/provider": ^19.0.0 + "@magic-sdk/types": ^16.0.0 peerDependencies: "@magic-sdk/provider": ">=18.6.0" "@magic-sdk/types": ">=15.8.0" languageName: unknown linkType: soft +"@magic-sdk/commons@npm:^14.6.0": + version: 14.6.0 + resolution: "@magic-sdk/commons@npm:14.6.0" + peerDependencies: + "@magic-sdk/provider": ">=4.3.0" + "@magic-sdk/types": ">=3.1.1" + checksum: 43c71771ddf3355d16fe1eed29d778cdb01b40b2af5f67a07173d0c41b3bc3f14a8513221948a84191805372b2d34f1097e8f0de771ed867c3485ffb1805dcd8 + languageName: node + linkType: hard + "@magic-sdk/pnp@workspace:packages/@magic-sdk/pnp": version: 0.0.0-use.local resolution: "@magic-sdk/pnp@workspace:packages/@magic-sdk/pnp" @@ -3055,17 +3073,17 @@ __metadata: "@babel/core": ^7.9.6 "@babel/plugin-proposal-optional-chaining": ^7.9.0 "@babel/runtime": ^7.9.6 - "@magic-ext/oauth": ^12.6.0 - magic-sdk: ^18.6.0 + "@magic-ext/oauth": ^13.0.0 + magic-sdk: ^19.0.0 languageName: unknown linkType: soft -"@magic-sdk/provider@^18.6.0, @magic-sdk/provider@workspace:packages/@magic-sdk/provider": +"@magic-sdk/provider@^19.0.0, @magic-sdk/provider@workspace:packages/@magic-sdk/provider": version: 0.0.0-use.local resolution: "@magic-sdk/provider@workspace:packages/@magic-sdk/provider" dependencies: "@babel/plugin-transform-modules-commonjs": ^7.9.6 - "@magic-sdk/types": ^15.8.0 + "@magic-sdk/types": ^16.0.0 "@peculiar/webcrypto": ^1.1.7 eventemitter3: ^4.0.4 localforage: ^1.7.4 @@ -3077,7 +3095,7 @@ __metadata: languageName: unknown linkType: soft -"@magic-sdk/react-native-bare@^19.6.0, @magic-sdk/react-native-bare@workspace:packages/@magic-sdk/react-native-bare": +"@magic-sdk/react-native-bare@^20.0.0, @magic-sdk/react-native-bare@workspace:packages/@magic-sdk/react-native-bare": version: 0.0.0-use.local resolution: "@magic-sdk/react-native-bare@workspace:packages/@magic-sdk/react-native-bare" dependencies: @@ -3085,9 +3103,9 @@ __metadata: "@babel/core": ^7.15.0 "@babel/plugin-transform-flow-strip-types": ^7.14.5 "@babel/runtime": ~7.10.4 - "@magic-sdk/commons": ^14.6.0 - "@magic-sdk/provider": ^18.6.0 - "@magic-sdk/types": ^15.8.0 + "@magic-sdk/commons": ^15.0.0 + "@magic-sdk/provider": ^19.0.0 + "@magic-sdk/types": ^16.0.0 "@react-native-async-storage/async-storage": ^1.15.5 "@types/lodash": ^4.14.158 buffer: ~5.6.0 @@ -3113,7 +3131,7 @@ __metadata: languageName: unknown linkType: soft -"@magic-sdk/react-native-expo@^19.6.0, @magic-sdk/react-native-expo@workspace:packages/@magic-sdk/react-native-expo": +"@magic-sdk/react-native-expo@^20.0.0, @magic-sdk/react-native-expo@workspace:packages/@magic-sdk/react-native-expo": version: 0.0.0-use.local resolution: "@magic-sdk/react-native-expo@workspace:packages/@magic-sdk/react-native-expo" dependencies: @@ -3121,9 +3139,9 @@ __metadata: "@babel/core": ^7.15.0 "@babel/plugin-transform-flow-strip-types": ^7.14.5 "@babel/runtime": ~7.10.4 - "@magic-sdk/commons": ^14.6.0 - "@magic-sdk/provider": ^18.6.0 - "@magic-sdk/types": ^15.8.0 + "@magic-sdk/commons": ^15.0.0 + "@magic-sdk/provider": ^19.0.0 + "@magic-sdk/types": ^16.0.0 "@react-native-async-storage/async-storage": ^1.15.5 "@types/lodash": ^4.14.158 buffer: ~5.6.0 @@ -3149,7 +3167,7 @@ __metadata: languageName: unknown linkType: soft -"@magic-sdk/types@^15.8.0, @magic-sdk/types@workspace:packages/@magic-sdk/types": +"@magic-sdk/types@^16.0.0, @magic-sdk/types@workspace:packages/@magic-sdk/types": version: 0.0.0-use.local resolution: "@magic-sdk/types@workspace:packages/@magic-sdk/types" languageName: unknown @@ -4121,13 +4139,20 @@ __metadata: languageName: node linkType: hard -"@types/lodash@npm:^4.14.158, @types/lodash@npm:^4.14.172": +"@types/lodash@npm:^4.14.158": version: 4.14.191 resolution: "@types/lodash@npm:4.14.191" checksum: ba0d5434e10690869f32d5ea49095250157cae502f10d57de0a723fd72229ce6c6a4979576f0f13e0aa9fbe3ce2457bfb9fa7d4ec3d6daba56730a51906d1491 languageName: node linkType: hard +"@types/lodash@npm:^4.14.172": + version: 4.14.172 + resolution: "@types/lodash@npm:4.14.172" + checksum: 5fc51ea40d7e455bd1c760bcc60c9e81f82d85988666d4d4969bd8311367c63bbf1fe2be48755ba87836710e45fff94014ec164ebe6ccaa72815014981152434 + languageName: node + linkType: hard + "@types/minimatch@npm:*, @types/minimatch@npm:^5.1.2": version: 5.1.2 resolution: "@types/minimatch@npm:5.1.2" @@ -12801,16 +12826,16 @@ fsevents@^2.3.2: languageName: unknown linkType: soft -"magic-sdk@^18.6.0, magic-sdk@workspace:packages/magic-sdk": +"magic-sdk@^19.0.0, magic-sdk@workspace:packages/magic-sdk": version: 0.0.0-use.local resolution: "magic-sdk@workspace:packages/magic-sdk" dependencies: "@babel/core": ^7.9.6 "@babel/plugin-proposal-optional-chaining": ^7.9.0 "@babel/runtime": ^7.9.6 - "@magic-sdk/commons": ^14.6.0 - "@magic-sdk/provider": ^18.6.0 - "@magic-sdk/types": ^15.8.0 + "@magic-sdk/commons": ^15.0.0 + "@magic-sdk/provider": ^19.0.0 + "@magic-sdk/types": ^16.0.0 localforage: ^1.7.4 localforage-driver-memory: ^1.0.5 languageName: unknown