Skip to content

Commit

Permalink
Merge pull request #435 from HathorNetwork/master
Browse files Browse the repository at this point in the history
Release-candidate 1.27.0-rc.1+beta
  • Loading branch information
andreabadesso authored Aug 3, 2023
2 parents 8e3cd42 + d662883 commit 6283aac
Show file tree
Hide file tree
Showing 22 changed files with 442 additions and 36 deletions.
7 changes: 7 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if [[ $(type -t use_flake) != function ]]; then
echo "ERROR: use_flake function missing."
echo "Please update direnv to v2.30.0 or later."
exit 1
fi

use flake
9 changes: 4 additions & 5 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
### Acceptance Criteria
- Include here all things that this PR should solve
Please go the the `Preview` tab and select the appropriate Pull Request template:


### Security Checklist
- [ ] Make sure you do not include new dependencies in the project unless strictly necessary and do not include dev-dependencies as production ones. More dependencies increase the possibility of one of them being hijacked and affecting us.
* [Feature Branch](?expand=1&template=feature_branch_pr_template.md) - Use this PR template when you are merging a feature branch into `master`
* [Release Candidate](?expand=1&template=release_candidate_pr_template.md) - Use this PR template when you are merging `master` into `release-candidate`
* [Release](?expand=1&template=release_pr_template.md) - Use this PR template when you are merging `release-candidate` into `release`
12 changes: 12 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE/feature_branch_pr_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Motivation

What was the motivation for the changes in this PR?

### Acceptance Criteria

- Include here all things that this PR should solve

### Checklist
- [ ] If you are requesting a merge into `master`, confirm this code is production-ready and can be included in future releases as soon as it gets merged
- [ ] Make sure either the unit tests and/or the QA tests are capable of testing the new features
- [ ] Make sure you do not include new dependencies in the project unless strictly necessary and do not include dev-dependencies as production ones. More dependencies increase the possibility of one of them being hijacked and affecting us.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changes

Link here all the PRs that are included in this release candidate

# Checklist

- [ ] I've read and followed the release candidate process described in https://github.com/HathorNetwork/ops-tools/blob/master/docs/release-guides/wallet-service.md#release-candidate
- [ ] I confirm this release candidate only includes production-ready changes
12 changes: 12 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE/release_pr_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changes

Link here all the PRs that are included in this release

# Release-candidates

Link here the release-candidates that were deployed as part of this release

# Checklist

- [ ] I've read and followed the release process described in https://github.com/HathorNetwork/ops-tools/blob/master/docs/release-guides/wallet-service.md#stable-release
- [ ] The QA process was run successfully during the tests of the corresponding release-candidate(s)
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ coverage

# fcm account config
**/fcm.config.json

.direnv
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,16 @@ Sometimes, jest will use old cached js files, even after you modified the typesc
## Standard Operating Procedures

Check it in [docs/SOP.md](docs/SOP.md)


## Nix flakes

## Using this project

This project uses [Nix](https://nixos.org/) with [direnv](https://direnv.net/) to help with dependencies, including Node.js. To get started, you need to have Nix and direnv installed.

1. Install [Nix](https://nixos.org/download.html) and [Direnv](https://direnv.net/docs/installation.html).
2. Enable flake support in Nix: `nix-env -iA nixpkgs.nixUnstable`
3. Allow direnv to work in your shell by running `direnv allow`

Now, every time you enter the project directory, direnv will automatically activate the environment from flake.nix, including the specific version of Node.js specified there. When you leave the directory, it will deactivate. This ensures a consistent and isolated environment per project.
17 changes: 17 additions & 0 deletions db/migrations/20230601151507-address_index_idx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

module.exports = {
up: async (queryInterface) => {
await queryInterface.addIndex(
'address',
['index'], {
name: 'address_index_idx',
fields: 'index',
},
);
},

down: async (queryInterface) => {
await queryInterface.removeIndex('address', 'address_index_idx');
},
};
2 changes: 2 additions & 0 deletions events/nftCreationTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Transaction } from '@src/types';
*/
export const nftCreationTx = {
tx_id: '0025a6488045d7466639ead179a7f6beb188320f41cdb6df3a971db2ee86dbc3',
signal_bits: 0,
version: 2,
weight: 8.000001,
timestamp: 1656543561,
Expand Down Expand Up @@ -119,6 +120,7 @@ export function getTransaction(): Transaction {
tx_id: nftCreationTx.tx_id,
nonce: 1,
timestamp: nftCreationTx.timestamp,
signal_bits: nftCreationTx.signal_bits,
version: nftCreationTx.version,
weight: nftCreationTx.weight,
parents: nftCreationTx.parents,
Expand Down
92 changes: 92 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This flake.nix file creates a virtual environment with the desired dependencies
in a reproducible way using Nix and Nix flakes (https://nixos.wiki/wiki/Flakes)
Flakes are a feature in Nix that allows you to specify the dependencies of your
project in a declarative and reproducible manner. It allows for better isolation,
reproducibility, and more reliable upgrades.
`direnv` is an environment switcher for the shell. It knows how to hook into
multiple shells (like bash, zsh, fish, etc...) to load or unload environment
variables depending on the current directory. This allows project-specific
environment variables without cluttering the "~/.profile" file.
This flake file creates a shell with nodejs v14.x installed and should work
on macOs, linux and windows
*/
{
description = "Flake that installs Node.js 14.x via direnv";

inputs.devshell.url = "github:numtide/devshell";
inputs.flake-utils.url = "github:numtide/flake-utils";

outputs = { self, flake-utils, devshell, nixpkgs }:

flake-utils.lib.eachDefaultSystem (system: {
devShell =
let pkgs = import nixpkgs {
inherit system;

overlays = [ devshell.overlay ];
};
in
pkgs.devshell.mkShell {
packages = with pkgs; [
nodejs-14_x
];
};
});
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hathor-wallet-service",
"version": "1.26.0+beta",
"version": "1.27.0+beta",
"description": "",
"scripts": {
"postinstall": "npm dedupe",
Expand Down
4 changes: 4 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ functions:
method: get
cors: true
authorizer: ${self:custom.authorizer.walletBearer}
request:
parameters:
paths:
index: false
warmup:
walletWarmer:
enabled: true
Expand Down
89 changes: 71 additions & 18 deletions src/api/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

import 'source-map-support/register';

import Joi from 'joi';
import Joi, { ValidationError } from 'joi';
import { APIGatewayProxyHandler } from 'aws-lambda';
import { ApiError } from '@src/api/errors';
import { closeDbAndGetError, warmupMiddleware } from '@src/api/utils';
import {
getWallet,
getWalletAddresses,
getAddressAtIndex as dbGetAddressAtIndex,
} from '@src/db';
import { AddressInfo } from '@src/types';
import { AddressInfo, AddressAtIndexRequest } from '@src/types';
import { closeDbConnection, getDbConnection } from '@src/utils';
import { walletIdProxyHandler } from '@src/commons';
import middy from '@middy/core';
Expand All @@ -32,6 +33,19 @@ const checkMineBodySchema = Joi.object({
.required(),
});

class AddressAtIndexValidator {
static readonly bodySchema = Joi.object({
index: Joi.number().min(0).optional(),
});

static validate(payload: unknown): { value: AddressAtIndexRequest, error: ValidationError} {
return AddressAtIndexValidator.bodySchema.validate(payload, {
abortEarly: false, // We want it to return all the errors not only the first
convert: true, // We need to convert as parameters are sent on the QueryString
}) as { value: AddressAtIndexRequest, error: ValidationError };
}
}

/*
* Check if a list of addresses belong to the caller wallet
*
Expand Down Expand Up @@ -93,27 +107,66 @@ export const checkMine: APIGatewayProxyHandler = middy(walletIdProxyHandler(asyn
})).use(cors());

/*
* Get the addresses of a wallet
* Get the addresses of a wallet, allowing an index filter
* Notice: If the index filter is passed, it will only find addresses
* that are already in our database, this will not derive new addresses
*
* This lambda is called by API Gateway on GET /addresses
*/
export const get: APIGatewayProxyHandler = middy(walletIdProxyHandler(async (walletId) => {
const status = await getWallet(mysql, walletId);
export const get: APIGatewayProxyHandler = middy(
walletIdProxyHandler(async (walletId, event) => {
const status = await getWallet(mysql, walletId);

if (!status) {
return closeDbAndGetError(mysql, ApiError.WALLET_NOT_FOUND);
}
if (!status.readyAt) {
return closeDbAndGetError(mysql, ApiError.WALLET_NOT_READY);
}
if (!status) {
return closeDbAndGetError(mysql, ApiError.WALLET_NOT_FOUND);
}

const addresses = await getWalletAddresses(mysql, walletId);
if (!status.readyAt) {
return closeDbAndGetError(mysql, ApiError.WALLET_NOT_READY);
}

await closeDbConnection(mysql);
const { value: body, error } = AddressAtIndexValidator.validate(event.pathParameters);

return {
statusCode: 200,
body: JSON.stringify({ success: true, addresses }),
};
})).use(cors())
if (error) {
const details = error.details.map((err) => ({
message: err.message,
path: err.path,
}));

return closeDbAndGetError(mysql, ApiError.INVALID_PAYLOAD, { details });
}

let response = null;

if ('index' in body) {
const address: AddressInfo | null = await dbGetAddressAtIndex(mysql, walletId, body.index);

if (!address) {
return closeDbAndGetError(mysql, ApiError.ADDRESS_NOT_FOUND);
}

response = {
statusCode: 200,
body: JSON.stringify({
success: true,
addresses: [address],
}),
};
} else {
// Searching for multiple addresses
const addresses = await getWalletAddresses(mysql, walletId);
response = {
statusCode: 200,
body: JSON.stringify({
success: true,
addresses,
}),
};
}

await closeDbConnection(mysql);

return response;
}),
).use(cors())
.use(warmupMiddleware());
Loading

0 comments on commit 6283aac

Please sign in to comment.