Skip to content

Commit

Permalink
Merge pull request #597 from magiclabs/makrandgupta-sc-82686-revive-h…
Browse files Browse the repository at this point in the history
…edera-extension

[82686] revive hedera extension
  • Loading branch information
hcote committed Aug 3, 2023
2 parents 81160c6 + 0a975fc commit daf7d9a
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 48 deletions.
5 changes: 5 additions & 0 deletions packages/@magic-ext/hedera/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/coverage
/dist
/.eslintrc.js
/jest.config.ts
7 changes: 7 additions & 0 deletions packages/@magic-ext/hedera/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: ['../../../.eslintrc.js'],
parserOptions: {
project: ['./tsconfig.json'],
tsconfigRootDir: __dirname,
},
}
2 changes: 2 additions & 0 deletions packages/@magic-ext/hedera/.lintstagedrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'*.{ts,tsx}':
- eslint --fix
1 change: 1 addition & 0 deletions packages/@magic-ext/hedera/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../../.prettierrc.js');
Empty file.
21 changes: 21 additions & 0 deletions packages/@magic-ext/hedera/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions packages/@magic-ext/hedera/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@magic-ext/hedera",
"version": "0.1.0",
"description": "magic hedera extension",
"author": "Magic <[email protected]> (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"
}
}
3 changes: 3 additions & 0 deletions packages/@magic-ext/hedera/src/index.cdn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { HederaExtension } from './index';

export default HederaExtension;
1 change: 1 addition & 0 deletions packages/@magic-ext/hedera/src/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index';
30 changes: 30 additions & 0 deletions packages/@magic-ext/hedera/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 }]));
}
}
8 changes: 8 additions & 0 deletions packages/@magic-ext/hedera/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface HederaConfig {
network: string;
}

export enum HederaPayloadMethod {
HederaSign = 'hedera_sign',
HederaGetPublicKey = 'hedera_getPublicKey',
}
35 changes: 35 additions & 0 deletions packages/@magic-ext/hedera/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions packages/@magic-ext/hedera/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tsconfig.settings.json",
"exclude": [
"temple"
]
}

Loading

0 comments on commit daf7d9a

Please sign in to comment.