Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[82686] revive hedera extension #597

Merged
merged 8 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
hedera extension
  • Loading branch information
makrandgupta committed Jul 28, 2023
commit 5e674f853142c718672c113dc652660e67ff1f62
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.
34 changes: 34 additions & 0 deletions packages/@magic-ext/hedera/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"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": "^5.0.0"
},
"dependencies": {}
}
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',
}
36 changes: 36 additions & 0 deletions packages/@magic-ext/hedera/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @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) {
// Pick a remaining element...
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;
}
}
102 changes: 102 additions & 0 deletions packages/@magic-ext/hedera/temple/MagicProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
Client,
AccountBalanceQuery,
AccountInfoQuery,
AccountRecordsQuery,
TransactionReceiptQuery,
} from "@hashgraph/sdk";

export class MagicProvider {
constructor(hedera_network) {
if (!hedera_network) {
throw new Error(
"LocalProvider requires the `HEDERA_NETWORK` environment variable to be set"
);
}

this._client = Client.forName(hedera_network);
}

/**
* @returns {LedgerId?}
*/
getLedgerId() {
return this._client.ledgerId;
}

/**
* @returns {{[key: string]: (string | AccountId)}}
*/
getNetwork() {
return this._client.network;
}

/**
* @returns {string[]}
*/
getMirrorNetwork() {
return this._client.mirrorNetwork;
}

/**
* @param {AccountId | string} accountId
* @returns {Promise<AccountBalance>}
*/
getAccountBalance(accountId) {
return new AccountBalanceQuery()
.setAccountId(accountId)
.execute(this._client);
}

/**
* @param {AccountId | string} accountId
* @returns {Promise<AccountInfo>}
*/
getAccountInfo(accountId) {
return new AccountInfoQuery()
.setAccountId(accountId)
.execute(this._client);
}

/**
* @param {AccountId | string} accountId
* @returns {Promise<TransactionRecord[]>}
*/
getAccountRecords(accountId) {
return new AccountRecordsQuery()
.setAccountId(accountId)
.execute(this._client);
}

/**
* @param {TransactionId | string} transactionId
* @returns {Promise<TransactionReceipt>}
*/
getTransactionReceipt(transactionId) {
return new TransactionReceiptQuery()
.setTransactionId(transactionId)
.execute(this._client);
}

/**
* @param {TransactionResponse} response
* @returns {Promise<TransactionReceipt>}
*/
waitForReceipt(response) {
return new TransactionReceiptQuery()
.setNodeAccountIds([response.nodeId])
.setTransactionId(response.transactionId)
.execute(this._client);
}

/**
* @template RequestT
* @template ResponseT
* @template OutputT
* @param {Executable<RequestT, ResponseT, OutputT>} request
* @returns {Promise<OutputT>}
*/
call(request) {
return request.execute(this._client);
}
}
Loading