-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rework code + new features (#61)
* feat: rework code + new features - rework code to make it easier to follow - throw errors if chain props are not fetched - render json errors instead of plain text * fix: tests * fix: lint * fix: ci with yarn 4.1.1 * fix: test and publish workflow * fix: ci
- Loading branch information
1 parent
f76627c
commit 6e223ee
Showing
18 changed files
with
352 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Chain, getChains } from '../utils/chains' | ||
import { Request, Response } from 'express' | ||
|
||
export const chains = (req: Request, res: Response) => { | ||
const chains = getChains() | ||
const chainsFiltered = chains.map(({ name, id, url }: Chain) => { | ||
return { | ||
name, | ||
id, | ||
url: url, | ||
} | ||
}) | ||
|
||
res.status(200).json({ chains: chainsFiltered }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Chain, getChains } from '../utils/chains' | ||
import { renderChainNotFoundError, renderInternalError } from '../utils/errors' | ||
import { cacheMetadata } from '../utils/metadata' | ||
import { Request, Response } from 'express' | ||
import { ChainConfig } from '../utils/types' | ||
|
||
export const nodeMetadata = async (req: Request, res: Response) => { | ||
const chains = getChains() | ||
const { id: chainId }: ChainConfig = req.body | ||
|
||
const chain = chains.find((b: Chain) => b.id === chainId) | ||
if (!chain) { | ||
renderChainNotFoundError(res) | ||
return | ||
} | ||
|
||
if (chain.metadata) { | ||
res.status(200).json({ metadata: chain.metadata }) | ||
return | ||
} | ||
|
||
try { | ||
await cacheMetadata(chain) | ||
} catch (e) { | ||
renderInternalError(res, e) | ||
return | ||
} | ||
|
||
res.status(200).json({ metadata: chain.metadata }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Chain, getChains } from '../utils/chains' | ||
import { renderChainNotFoundError } from '../utils/errors' | ||
import { Request, Response } from 'express' | ||
import { ChainConfig } from '../utils/types' | ||
|
||
export const nodeMetadataFlush = (req: Request, res: Response) => { | ||
const chains = getChains() | ||
const { id: chainId }: ChainConfig = req.body | ||
|
||
const chain = chains.find((b: Chain) => b.id === chainId) | ||
if (!chain) { | ||
renderChainNotFoundError(res) | ||
return | ||
} | ||
|
||
chain.metadata = undefined | ||
chain.metadataHex = undefined | ||
chain.props = undefined | ||
|
||
res.status(200).json() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Chain, getChains } from '../utils/chains' | ||
import { renderChainNotFoundError, renderGetMetadataFirstError, renderInternalError } from '../utils/errors' | ||
import { cacheMetadata } from '../utils/metadata' | ||
import { getMetadataDigest } from '../../rust' | ||
import { Request, Response } from 'express' | ||
import { ChainConfig } from '../utils/types' | ||
|
||
export const nodeMetadataHash = async (req: Request, res: Response) => { | ||
const chains = getChains() | ||
const { id: chainId }: ChainConfig = req.body | ||
|
||
const chain = chains.find((b: Chain) => b.id === chainId) | ||
if (!chain) { | ||
renderChainNotFoundError(res) | ||
return | ||
} | ||
|
||
let { props, metadataHex } = chain | ||
if (!props || !metadataHex) { | ||
try { | ||
await cacheMetadata(chain) | ||
} catch (e) { | ||
renderInternalError(res, e) | ||
return | ||
} | ||
} | ||
|
||
;({ props, metadataHex } = chain) | ||
if (!props || !metadataHex) { | ||
renderGetMetadataFirstError(res) | ||
return | ||
} | ||
|
||
const metadataHash = getMetadataDigest({ metadata: metadataHex, props }) | ||
|
||
res.status(200).json({ metadataHash }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Chain, getChains } from '../utils/chains' | ||
import { renderChainNotFoundError, renderGetMetadataFirstError, renderInternalError } from '../utils/errors' | ||
import { cacheMetadata } from '../utils/metadata' | ||
import { Request, Response } from 'express' | ||
import { ChainConfig } from '../utils/types' | ||
|
||
export const nodeProps = async (req: Request, res: Response) => { | ||
const chains = getChains() | ||
|
||
const { id: chainId }: ChainConfig = req.body | ||
|
||
const chain = chains.find((b: Chain) => b.id === chainId) | ||
if (!chain) { | ||
renderChainNotFoundError(res) | ||
return | ||
} | ||
|
||
let { props } = chain | ||
if (!props) { | ||
try { | ||
await cacheMetadata(chain) | ||
} catch (e) { | ||
renderInternalError(res, e) | ||
return | ||
} | ||
} | ||
|
||
;({ props } = chain) | ||
if (!props) { | ||
renderGetMetadataFirstError(res) | ||
return | ||
} | ||
|
||
res.status(200).json({ props: { ...props } }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Response, Request } from 'express' | ||
import { Chain, getChains } from '../utils/chains' | ||
import { renderChainNotFoundError, renderGetMetadataFirstError, renderInternalError, renderMissingTxBlobError } from '../utils/errors' | ||
import { cacheMetadata } from '../utils/metadata' | ||
import { getShortMetadataFromTxBlob } from '../../rust' | ||
import { TxToSign } from '../utils/types' | ||
|
||
export const transactionMetadata = async (req: Request, res: Response) => { | ||
const chains = getChains() | ||
|
||
const { | ||
chain: { id: chainId }, | ||
}: TxToSign = req.body | ||
let { txBlob }: TxToSign = req.body | ||
|
||
const chain = chains.find((b: Chain) => b.id === chainId) | ||
if (!chain) { | ||
renderChainNotFoundError(res) | ||
return | ||
} | ||
|
||
let { props, metadataHex } = chain | ||
if (!props || !metadataHex === undefined) { | ||
try { | ||
await cacheMetadata(chain) | ||
} catch (e) { | ||
renderInternalError(res, e) | ||
return | ||
} | ||
} | ||
|
||
;({ props, metadataHex } = chain) | ||
if (!props || !metadataHex) { | ||
renderGetMetadataFirstError(res) | ||
return | ||
} | ||
|
||
if (!txBlob) { | ||
renderMissingTxBlobError(res) | ||
return | ||
} | ||
|
||
try { | ||
if (txBlob.substring(0, 2) == '0x') { | ||
txBlob = txBlob.substring(2) | ||
} | ||
|
||
const txMetadata = Buffer.from(getShortMetadataFromTxBlob({ txBlob, metadata: metadataHex, props }), 'hex') | ||
res.status(200).send({ txMetadata: '0x' + txMetadata.toString('hex') }) | ||
} catch (e) { | ||
renderInternalError(res, e) | ||
} | ||
} |
Oops, something went wrong.