-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from trustenterprises/feature/bequest_account
Create and bequest tokens to an account
- Loading branch information
Showing
13 changed files
with
1,908 additions
and
2 deletions.
There are no files selected for viewing
1,641 changes: 1,641 additions & 0 deletions
1,641
Serverless Hedera Consensus.postman_collection.json
Large diffs are not rendered by default.
Oops, something went wrong.
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,30 @@ | ||
import bequestTokenRequest from "app/validators/bequestTokenRequest" | ||
import Response from "app/response" | ||
|
||
async function BequestTokenHandler(req, res) { | ||
const validationErrors = bequestTokenRequest(req.body) | ||
|
||
if (validationErrors) { | ||
return Response.unprocessibleEntity(res, validationErrors) | ||
} | ||
|
||
const { encrypted_receiver_key, token_id, receiver_id, amount } = req.body | ||
const bequestPayload = { | ||
encrypted_receiver_key, | ||
token_id, | ||
receiver_id, | ||
amount | ||
} | ||
|
||
const { hashgraphClient } = req.context | ||
const bequestResponse = await hashgraphClient.bequestToken(bequestPayload) | ||
|
||
if (bequestResponse) { | ||
return Response.json(res, bequestResponse) | ||
} | ||
|
||
// This has to be bolstered up with correct error handling | ||
return Response.badRequest(res) | ||
} | ||
|
||
export default BequestTokenHandler |
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,10 @@ | ||
import Response from "app/response" | ||
|
||
async function CreateAccountHandler(req, res) { | ||
const { hashgraphClient } = req.context | ||
const account = await hashgraphClient.createAccount() | ||
|
||
Response.json(res, account) | ||
} | ||
|
||
export default CreateAccountHandler |
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,19 @@ | ||
import Language from "app/constants/language" | ||
import Response from "app/response" | ||
import Config from "app/config" | ||
|
||
const { noEncryptionKey } = Language.middleware.ensureEncryptionKey | ||
|
||
const ENC_KEY_LEN = 32 | ||
|
||
function ensureEncryptionKey(handler) { | ||
return async (req, res) => { | ||
if (Config.encryptionKey?.length === ENC_KEY_LEN) { | ||
return handler(req, res) | ||
} | ||
|
||
return Response.unprocessibleEntity(res, noEncryptionKey) | ||
} | ||
} | ||
|
||
export default ensureEncryptionKey |
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,39 @@ | ||
import crypto from "crypto" | ||
import Config from "app/config" | ||
|
||
const IV_LENGTH = 16 // For AES, this is always 16 | ||
|
||
function encrypt(text, encryptionKey = Config.encryptionKey) { | ||
const iv = Buffer.from(crypto.randomBytes(IV_LENGTH)) | ||
.toString("hex") | ||
.slice(0, IV_LENGTH) | ||
const cipher = crypto.createCipheriv( | ||
"aes-256-cbc", | ||
Buffer.from(encryptionKey), | ||
iv | ||
) | ||
const encrypted = cipher.update(text) | ||
const buffer = Buffer.concat([encrypted, cipher.final()]) | ||
|
||
return iv + ":" + buffer.toString("hex") | ||
} | ||
|
||
function decrypt(text, encryptionKey = Config.encryptionKey) { | ||
const textParts = text.includes(":") ? text.split(":") : [] | ||
const iv = Buffer.from(textParts.shift() || "", "binary") | ||
const encryptedText = Buffer.from(textParts.join(":"), "hex") | ||
const decipher = crypto.createDecipheriv( | ||
"aes-256-cbc", | ||
Buffer.from(encryptionKey), | ||
iv | ||
) | ||
const decrypted = decipher.update(encryptedText) | ||
const buffer = Buffer.concat([decrypted, decipher.final()]) | ||
|
||
return buffer.toString() | ||
} | ||
|
||
export default { | ||
decrypt, | ||
encrypt | ||
} |
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,20 @@ | ||
const Joi = require("@hapi/joi") | ||
|
||
const schema = Joi.object({ | ||
encrypted_receiver_key: Joi.string() | ||
.length(241) | ||
.required(), | ||
token_id: Joi.string().required(), | ||
receiver_id: Joi.string().required(), | ||
amount: Joi.number().required() | ||
}) | ||
|
||
function consensusMessageRequest(candidate = {}) { | ||
const validation = schema.validate(candidate || {}) | ||
|
||
if (validation.error) { | ||
return validation.error.details.map(error => error.message) | ||
} | ||
} | ||
|
||
export default consensusMessageRequest |
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,13 @@ | ||
import onlyPost from "app/middleware/onlyPost" | ||
import withAuthentication from "app/middleware/withAuthentication" | ||
import useHashgraphContext from "app/context/useHashgraphContext" | ||
import prepare from "app/utils/prepare" | ||
import CreateAccountHandler from "app/handler/createAccountHandler" | ||
import ensureEncryptionKey from "app/middleware/ensureEncryptionKey" | ||
|
||
export default prepare( | ||
onlyPost, | ||
ensureEncryptionKey, | ||
withAuthentication, | ||
useHashgraphContext | ||
)(CreateAccountHandler) |
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,13 @@ | ||
import onlyPost from "app/middleware/onlyPost" | ||
import withAuthentication from "app/middleware/withAuthentication" | ||
import useHashgraphContext from "app/context/useHashgraphContext" | ||
import prepare from "app/utils/prepare" | ||
import BequestTokenHandler from "app/handler/bequestTokenHandler" | ||
import ensureEncryptionKey from "app/middleware/ensureEncryptionKey" | ||
|
||
export default prepare( | ||
onlyPost, | ||
ensureEncryptionKey, | ||
withAuthentication, | ||
useHashgraphContext | ||
)(BequestTokenHandler) |