Skip to content

Commit

Permalink
Merge pull request #295 from HathorNetwork/dev
Browse files Browse the repository at this point in the history
Release v1.21.0-alpha
  • Loading branch information
andreabadesso authored Nov 29, 2022
2 parents d85d642 + 82f6abd commit 1a5a75b
Show file tree
Hide file tree
Showing 23 changed files with 1,843 additions and 13 deletions.
12 changes: 6 additions & 6 deletions .codebuild/buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ phases:
build:
commands:
- |
set -e;
if expr "${GIT_REF_TO_DEPLOY}" : "dev" >/dev/null; then
# Gets all env vars with `dev_` prefix and re-exports them without the prefix
for var in "${!dev_@}"; do
Expand All @@ -102,8 +103,8 @@ phases:
make migrate;
make deploy-lambdas-dev-testnet;
fi
- |
fi;
if expr "${GIT_REF_TO_DEPLOY}" : "master" >/dev/null; then
# Gets all env vars with `testnet_` prefix and re-exports them without the prefix
for var in "${!testnet_@}"; do
Expand All @@ -112,8 +113,8 @@ phases:
make migrate;
make deploy-lambdas-testnet;
fi
- |
fi;
if expr "${GIT_REF_TO_DEPLOY}" : "v.*" >/dev/null; then
# Gets all env vars with `mainnet_` prefix and re-exports them without the prefix
for var in "${!mainnet_@}"; do
Expand All @@ -122,5 +123,4 @@ phases:
make migrate;
make deploy-lambdas-mainnet;
fi
fi;
5 changes: 5 additions & 0 deletions .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: PR Validation
on:
pull_request:
types: [ready_for_review, synchronize]
branches: [master, dev]
paths-ignore:
- '**.md'
push:
Expand Down Expand Up @@ -80,3 +81,7 @@ jobs:
WS_DOMAIN: ws.ci.wallet-service.hathor.network
EXPLORER_SERVICE_LAMBDA_ENDPOINT: https://lambda.eu-central-1.amazonaws.com
WALLET_SERVICE_LAMBDA_ENDPOINT: https://lambda.eu-central-1.amazonaws.com
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
verbose: true
13 changes: 13 additions & 0 deletions DATABASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ CREATE TABLE `transaction` (
PRIMARY KEY (`tx_id`)
);
CREATE TABLE `push_devices` (
`device_id` varchar(256) NOT NULL,
`push_provider` enum('ios','android') NOT NULL,
`wallet_id` varchar(64) NOT NULL,
`enable_push` tinyint(1) NOT NULL DEFAULT '0',
`enable_show_amounts` tinyint(1) NOT NULL DEFAULT '0',
`enable_only_new_tx` tinyint(1) NOT NULL DEFAULT '0',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`device_id`),
KEY `wallet_id` (`wallet_id`),
CONSTRAINT `push_devices_ibfk_1` FOREIGN KEY (`wallet_id`) REFERENCES `wallet` (`id`)
);
CREATE INDEX transaction_version_idx USING HASH ON `transaction`(`version`);
CREATE INDEX tx_output_heightlock_idx USING HASH ON `tx_output`(`heightlock`);
CREATE INDEX tx_output_timelock_idx USING HASH ON `tx_output`(`timelock`);
Expand Down
20 changes: 20 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
codecov:
branch:

coverage:
status:
project:
default:
# minimum coverage ratio that the commit must meet to be considered a success
target: 88%
if_ci_failed: error
only_pulls: true
patch:
default:
# minimum coverage ratio that the commit must meet to be considered a success
target: 88%
if_ci_failed: error
only_pulls: true

github_checks:
annotations: true
49 changes: 49 additions & 0 deletions db/migrations/20221108235926-create-pushdevices.js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('push_devices', {
device_id: {
type: Sequelize.STRING(256),
allowNull: false,
primaryKey: true,
},
push_provider: {
type: Sequelize.ENUM(['ios', 'android']),
allowNull: false,
},
wallet_id: {
type: Sequelize.STRING(64),
allowNull: false,
references: {
model: 'wallet',
key: 'id',
},
},
enable_push: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
enable_show_amounts: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
enable_only_new_tx: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
updated_at: {
type: 'TIMESTAMP',
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
});
},
async down(queryInterface) {
await queryInterface.dropTable('push_devices');
},
};
65 changes: 65 additions & 0 deletions db/models/pushdevices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';
const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
class PushDevices extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
}
PushDevices.init(
{
device_id: {
type: DataTypes.STRING(256),
allowNull: false,
primaryKey: true,
},
push_provider: {
type: DataTypes.ENUM(['ios', 'android']),
allowNull: false,
},
wallet_id: {
type: DataTypes.STRING(64),
allowNull: false,
references: {
model: 'wallet',
key: 'id',
},
},
enable_push: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
enable_show_amounts: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
enable_only_new_tx: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
updated_at: {
type: 'TIMESTAMP',
allowNull: false,
defaultValue: DataTypes.literal(
'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
),
},
},
{
sequelize,
modelName: 'PushDevices',
tableName: 'push_devices',
timestamps: false,
},
);
return PushDevices;
};
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.19.0-alpha",
"version": "1.21.0-alpha",
"description": "",
"scripts": {
"postinstall": "npm dedupe",
Expand Down
44 changes: 44 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ functions:
warmup:
walletWarmer:
enabled: true
checkAddressMineApi:
handler: src/api/addresses.checkMine
events:
- http:
path: wallet/addresses/check_mine
method: post
cors: true
authorizer: ${self:custom.authorizer.walletBearer}
warmup:
walletWarmer:
enabled: false
getAddressesApi:
handler: src/api/addresses.get
events:
Expand Down Expand Up @@ -448,3 +459,36 @@ functions:
warmup:
walletWarmer:
enabled: false
pushRegister:
handler: src/api/pushRegister.register
events:
- http:
path: push/register
method: post
cors: true
authorizer: ${self:custom.authorizer.walletBearer}
warmup:
walletWarmer:
enabled: false
pushUpdate:
handler: src/api/pushUpdate.update
events:
- http:
path: push/update
method: put
cors: true
authorizer: ${self:custom.authorizer.walletBearer}
warmup:
walletWarmer:
enabled: false
pushUnregister:
handler: src/api/pushUnregister.unregister
events:
- http:
path: push/unregister
method: delete
cors: true
authorizer: ${self:custom.authorizer.walletBearer}
warmup:
walletWarmer:
enabled: false
71 changes: 71 additions & 0 deletions src/api/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,91 @@

import 'source-map-support/register';

import Joi from 'joi';
import { APIGatewayProxyHandler } from 'aws-lambda';
import { ApiError } from '@src/api/errors';
import { closeDbAndGetError, warmupMiddleware } from '@src/api/utils';
import {
getWallet,
getWalletAddresses,
} from '@src/db';
import { AddressInfo } from '@src/types';
import { closeDbConnection, getDbConnection } from '@src/utils';
import { walletIdProxyHandler } from '@src/commons';
import middy from '@middy/core';
import cors from '@middy/http-cors';

const mysql = getDbConnection();

const checkMineBodySchema = Joi.object({
addresses: Joi.array()
// Validate that addresses are a base58 string and exactly 34 in length
.items(Joi.string().regex(/^[A-HJ-NP-Za-km-z1-9]*$/).min(34).max(34))
.min(1)
.max(512) // max number of addresses in a tx (256 outputs and 256 inputs)
.required(),
});

/*
* Check if a list of addresses belong to the caller wallet
*
* This lambda is called by API Gateway on POST /addresses/check_mine
*/
export const checkMine: APIGatewayProxyHandler = middy(walletIdProxyHandler(async (walletId, event) => {
const status = await getWallet(mysql, walletId);

// If the wallet is not started or ready, we can skip the query on the address table
if (!status) {
return closeDbAndGetError(mysql, ApiError.WALLET_NOT_FOUND);
}

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

const eventBody = (function parseBody(body) {
try {
return JSON.parse(body);
} catch (e) {
return null;
}
}(event.body));

const { value, error } = checkMineBodySchema.validate(eventBody, {
abortEarly: false,
convert: false,
});

if (error) {
const details = error.details.map((err) => ({
message: err.message,
path: err.path,
}));

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

const sentAddresses = value.addresses;
const dbWalletAddresses: AddressInfo[] = await getWalletAddresses(mysql, walletId, sentAddresses);
const walletAddresses: Set<string> = dbWalletAddresses.reduce((acc, { address }) => acc.add(address), new Set([]));

await closeDbConnection(mysql);

const addressBelongMap = sentAddresses.reduce((acc: {string: boolean}, address: string) => {
acc[address] = walletAddresses.has(address);

return acc;
}, {});

return {
statusCode: 200,
body: JSON.stringify({
success: true,
addresses: addressBelongMap,
}),
};
})).use(cors());

/*
* Get the addresses of a wallet
*
Expand Down
1 change: 1 addition & 0 deletions src/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export enum ApiError {
TOKEN_NOT_FOUND = 'token-not-found',
FORBIDDEN = 'forbidden',
UNAUTHORIZED = 'unauthorized',
DEVICE_NOT_FOUND = 'device-not-found',
}
Loading

0 comments on commit 1a5a75b

Please sign in to comment.