Skip to content

Commit

Permalink
Merge branch 'dev' into 'master'
Browse files Browse the repository at this point in the history
feat(watcher-service): add allowed origin config

See merge request ergo/rosen-bridge/watcher!202
  • Loading branch information
vorujack committed Dec 19, 2023
2 parents abe2afc + a055d38 commit 07415dc
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 607 deletions.
775 changes: 190 additions & 585 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions packages/transactions/commitment-tx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
"node": ">=18.12.0"
},
"dependencies": {
"@rosen-bridge/abstract-logger": "^0.3.0",
"@rosen-bridge/observation-extractor": "^4.0.0",
"@rosen-bridge/rwt-repo": "^1.0.0",
"@rosen-bridge/ergo-box-selection": "^0.1.0",
"@rosen-bridge/abstract-logger": "^1.0.0",
"@rosen-bridge/observation-extractor": "^4.1.1",
"@rosen-bridge/rwt-repo": "^2.0.0",
"@rosen-bridge/ergo-box-selection": "^0.3.0",
"@rosen-bridge/json-bigint": "^0.1.0",
"blakejs": "^1.2.1",
"ergo-lib-wasm-nodejs": "^0.24.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/transactions/trigger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"dependencies": {
"@rosen-bridge/json-bigint": "^0.1.0",
"@rosen-bridge/abstract-logger": "^1.0.0",
"@rosen-bridge/observation-extractor": "^4.0.0",
"@rosen-bridge/rwt-repo": "^1.0.0",
"@rosen-bridge/observation-extractor": "^4.1.1",
"@rosen-bridge/rwt-repo": "^2.0.0",
"blakejs": "^1.2.1",
"ergo-lib-wasm-nodejs": "^0.26.0"
}
Expand Down
13 changes: 7 additions & 6 deletions services/watcher/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ cardano:
hash: '' # initial block hash, only used for ogmios
height: -1 # initial height of scanning, only used for koios
koios:
interval: 10 # koios scanning interval (in seconds)
interval: 20 # koios scanning interval (in seconds)
timeout: 10 # koios request timeout (in seconds)
url: 'https://api.koios.rest/api/v1' # koios url
url: 'https://api.koios.rest/api/beta' # koios url
ogmios:
host: '' # ogmios node host address
port: 1337 # ogmios port
useTls: false # connect to ogmios using https or not
ergo:
network: 'Mainnet' # ergo network type. testnet or mainnet
type: 'node' # ergo scanner type. options: node, explorer
fee: '1100000' # transaction fee
minBoxValue: '1100000' # minimum box value
fee: '2000000' # transaction fee
minBoxValue: '2000000' # minimum box value
mnemonic: '' # watcher mnemonic
initialHeight: 0 # scanner initial height
explorer:
Expand Down Expand Up @@ -64,6 +64,7 @@ database:
# name: '' # database name (for postgres)
api:
port: 3000 # port used to run express server
allowedOrigins: [] # list of allowed origins for CORS requests
healthCheck:
interval: 60 # health check update interval (in seconds)
asset:
Expand All @@ -81,8 +82,8 @@ healthCheck:
warnDifference: 2 # warning difference between existing and scanned blocks height
criticalDifference: 600 # critical difference between existing and scanned blocks height
permit:
warnCommitmentCount: 100 # warning remaining permits for creating commitment
criticalCommitmentCount: 10 # critical remaining permits for creating commitment
warnCommitmentCount: 4 # warning remaining permits for creating commitment
criticalCommitmentCount: 0 # critical remaining permits for creating commitment
errorLog:
maxAllowedCount: 2 # maximum allowed error log lines
duration: 100000 # error log duration time check in milliseconds
Expand Down
2 changes: 2 additions & 0 deletions services/watcher/config/local-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ ergo:
node:
url: ''
mnemonic: ''
fee: '1100000'
minBoxValue: '1100000'
path:
addresses: tests/config
tokens: tests/config/tokens.test.json
Expand Down
8 changes: 4 additions & 4 deletions services/watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
},
"dependencies": {
"@emurgo/cip14-js": "^3.0.1",
"@rosen-bridge/address-extractor": "^3.0.0",
"@rosen-bridge/address-extractor": "^3.1.0",
"@rosen-bridge/health-check": "^3.0.1",
"@rosen-bridge/minimum-fee": "^0.1.13",
"@rosen-bridge/observation-extractor": "^4.0.0",
"@rosen-bridge/scanner": "^3.0.0",
"@rosen-bridge/watcher-data-extractor": "^4.0.0",
"@rosen-bridge/observation-extractor": "^4.1.1",
"@rosen-bridge/scanner": "^3.1.0",
"@rosen-bridge/watcher-data-extractor": "^4.1.0",
"@rosen-bridge/winston-logger": "0.2.1",
"axios": "^0.26.1",
"bip39": "^3.1.0",
Expand Down
11 changes: 9 additions & 2 deletions services/watcher/src/api/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,14 +769,17 @@ export class Transaction {
amount: AddressBalance,
toAddress: string
): Promise<string> => {
const config = getConfig();
const fee = BigInt(config.general.fee);
const minBoxValue = BigInt(config.general.minBoxValue);
const assetsMap = new Map<string, bigint>();
amount.tokens.forEach((token) => {
assetsMap.set(token.tokenId, token.amount);
});

try {
const coveringBoxes = await Transaction.boxes.getCoveringBoxes(
amount.nanoErgs,
amount.nanoErgs + fee + minBoxValue,
assetsMap
);
const height = await ErgoNetwork.getHeight();
Expand All @@ -791,7 +794,11 @@ export class Transaction {
// create output box
const userBox = Transaction.boxes.createCustomBox(
wasm.Contract.pay_to_address(address),
amount,
{
nanoErgs:
amount.nanoErgs > minBoxValue ? amount.nanoErgs : minBoxValue,
tokens: [...amount.tokens],
},
height
);
const candidates = [userBox];
Expand Down
13 changes: 13 additions & 0 deletions services/watcher/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class Config {
rosenConfigPath: string;
rosenTokensPath: string;
apiPort: number;
apiAllowedOrigins: string[];

constructor() {
this.networkType = getRequiredString('ergo.network').toLowerCase();
Expand Down Expand Up @@ -205,6 +206,18 @@ class Config {
path.join(this.rosenConfigPath, 'tokens.json')
);
this.apiPort = getOptionalNumber('api.port', 3000);
this.apiAllowedOrigins = config.get<string[]>('api.allowedOrigins');
if (
!Array.isArray(this.apiAllowedOrigins) ||
this.apiAllowedOrigins.some((origin) => typeof origin !== 'string')
) {
throw new Error('ImproperlyConfigured. Api allowed origins is invalid.');
}
if (this.apiAllowedOrigins.find((origin) => origin === '*')) {
console.warn(
'An allowed origin header with value "*" will cause all origins to be able to request this service, which may cause security issues'
);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion services/watcher/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ const init = async () => {
const initExpress = () => {
const app = express();
app.use(express.json());
app.use(cors());
const allowedOrigins = getConfig().general.apiAllowedOrigins;
app.use(
cors({
origin: allowedOrigins.includes('*') ? '*' : allowedOrigins,
})
);

const router = Router();
router.use('/address', addressRouter);
Expand Down
6 changes: 3 additions & 3 deletions services/watcher/tests/api/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Transaction', () => {
let watcherDb: WatcherDataBase, boxes: Boxes;
before(async () => {
const ORM = await loadDataBase();
await fillORM(ORM);
await fillORM(ORM, true);
watcherDb = ORM.DB;
boxes = new Boxes(watcherDb);
chai.spy.on(boxes, 'getRepoBox', () => {
Expand Down Expand Up @@ -144,15 +144,15 @@ describe('Transaction', () => {

// run the function and expect the error
const amount: AddressBalance = {
nanoErgs: 1200000n,
nanoErgs: 1861100000n,
tokens: [],
};
const address = '9gwWZGZgZhGjp1ZKNQ5rtxNELamz6trq9tigDsKRy71boWq3Fqq';
await expect(
TransactionTest.getInstance().withdrawFromWallet(amount, address)
).to.rejectedWith(
NotEnoughFund,
'Not enough fund to create the transaction. Uncovered value: 100000, Uncovered assets: []'
'Not enough fund to create the transaction. Uncovered value: 2200000, Uncovered assets: []'
);
});

Expand Down

0 comments on commit 07415dc

Please sign in to comment.