MC/website/node/postbank-catalogue.mjs
2026-11-27 23:43:05 +00:00

30 lines
3.5 KiB
JavaScript

import {createHash} from 'node:crypto';
export const BANK_VERSION='postbank-foundation-2026-11-13';
export const PACKAGES=Object.freeze([
{key:'pb_start',name:'Start',kinds:['person'],monthly:0,code:'108',extra:0,extraFee:0,instant:0},
{key:'pb_flow',name:'Flow',kinds:['person'],monthly:500,code:'102',extra:1,extraFee:300,instant:5,savings:true},
{key:'pb_pro',name:'Pro',kinds:['person'],monthly:1500,code:'102',extra:3,extraFee:200,instant:null,unlimitedInstant:true,savings:true},
{key:'pb_connect',name:'Connect',kinds:['clan','company'],monthly:1000,code:'105',extra:2,extraFee:300,instant:15},
{key:'pb_scale',name:'Scale',kinds:['clan','company'],monthly:2500,code:'105',extra:9,extraFee:200,instant:40},
{key:'pb_civic',name:'Civic',kinds:['house','office'],monthly:0,code:'107',extra:0,extraFee:0,instant:10},
{key:'pb_public',name:'Public',kinds:['staff'],monthly:0,code:'979',extra:0,extraFee:0,instant:25}
].map(Object.freeze));
export const ENTITY_PREFIXES=Object.freeze({clan:'CL',house:'HO',office:'PO',company:'CO',staff:'ST',server:'OF'});
export const ACCOUNT_TYPES=Object.freeze({'102':'Residential current account','103':'Savings account','104':'Savings+','105':'Corporate current account','106':'Blocked collateral account','107':'Civic current account','108':'Start account','979':'Official current account'});
export function clientCandidate(kind,key,attempt=0){
if(!['person',...Object.keys(ENTITY_PREFIXES)].includes(kind)||typeof key!=='string'||!key||key.length>96||!Number.isInteger(attempt)||attempt<0||attempt>31)throw Error('Invalid client identity.');
const hash=BigInt('0x'+createHash('sha256').update('postbank:client:v2:'+kind+':'+key.toLowerCase()+':'+attempt).digest('hex'));
return kind==='person'?(hash%1000000000000n).toString().padStart(12,'0'):ENTITY_PREFIXES[kind]+(hash%10000000000n).toString().padStart(10,'0');
}
export function bankNumber(code,id){
if(!Object.hasOwn(ACCOUNT_TYPES,code)||!Number.isSafeInteger(id)||id<1||id>999999999)throw Error('Invalid account number.');
const bban=code+String(id).padStart(9,'0'),digits=String(98n-BigInt(bban+'132700')%97n).padStart(2,'0');return 'DR'+digits+bban;
}
export function validBankNumber(number){return typeof number==='string'&&/^DR\d{14}$/.test(number)&&Object.hasOwn(ACCOUNT_TYPES,number.slice(4,7))&&BigInt(number.slice(4)+'1327'+number.slice(2,4))%97n===1n;}
export const formatBankNumber=n=>validBankNumber(n)?n.match(/.{4}/g).join(' '):'Not activated';
export function minorAmount(value){if(typeof value!=='string'||! /^(?:0|[1-9]\d{0,6})(?:\.\d{1,2})?$/.test(value))throw Error('Use an amount with at most two decimals.');const [a,b='']=value.split('.'),n=BigInt(a)*100n+BigInt(b.padEnd(2,'0'));if(n<=0n||n>100000000n)throw Error('Amount must be between 0.01 and 1,000,000 DM.');return n;}
export const money=n=>{const a=BigInt(n),v=a<0n?-a:a;return(a<0n?'-':'')+(v/100n)+'.'+String(v%100n).padStart(2,'0');};
export const availablePackages=kind=>PACKAGES.filter(p=>p.kinds.includes(kind));
export const canInvest=()=>false;
// No commercial transfer ceiling; retain the signed 64-bit ledger boundary.
export function transferAmount(value){if(typeof value!=='string'||!/^(?:0|[1-9]\d{0,16})(?:\.\d{1,2})?$/.test(value))throw Error('Use a positive amount with at most two decimals.');const [whole,fraction='']=value.split('.'),amount=BigInt(whole)*100n+BigInt(fraction.padEnd(2,'0'));if(amount<=0n||amount>9223372036854775807n)throw Error('The amount cannot be represented safely by the bank ledger.');return amount;}