-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanctions.utils.js
127 lines (113 loc) · 3.34 KB
/
sanctions.utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const {
PrivateKey,
PublicKey,
Signature,
CircuitString,
Field,
Bool
} = require('o1js')
require('dotenv').config()
const verifyOracleData = (data) => {
const PUBLIC_KEY = 'B62qmXFNvz2sfYZDuHaY5htPGkx1u2E2Hn3rWuDWkE11mxRmpijYzWN'
const signature = Signature.fromJSON(data.signature)
const validSignature = signature.verify(PublicKey.fromBase58(PUBLIC_KEY), [
...CircuitString.fromString(data.data.name).toFields(),
...CircuitString.fromString(data.data.surname).toFields(),
...CircuitString.fromString(data.data.country).toFields(),
...CircuitString.fromString(data.data.pno).toFields(),
Field(data.data.currentDate),
])
return validSignature.toBoolean()
}
const transformData = (data) => {
/**
* Transforms input data from one structure to another.
*
* This function takes an input data structure representing personal information
* and transforms it into a new structure conforming to a different data schema.
*
* Input Structure:
* {
* name: string,
* surname: string,
* country: string,
* pno: string,
* currentDate: number
* }
*
* Output Structure:
* {
* name: string, // Full name combining the 'name' and 'surname' from the input
* dob: string, // Date of birth extracted from the 'pno'
* citizenship: string, // Citizenship mapped from the 'country'
* gender: string // Gender extracted from the 'pno'
* }
*/
// Helper to map chars to indices
// Personal Number: PNOLT-36203292548
// Index: 01234567890123456
function mapPNOToDOB(pno) {
const startIdx = pno.indexOf('-') + 1
const pnoDigits = pno.substring(startIdx)
const century = Math.floor(Number(pnoDigits.charAt(0)) / 2) + 18
const yy = pnoDigits.substr(1, 2)
const mm = pnoDigits.substr(3, 2)
const dd = pnoDigits.substr(5, 2)
return `${century}${yy}-${mm}-${dd}`
}
function mapPNOToCitizenship(pno) {
const mapping = {
EE: 'Estonia',
LV: 'Latvia',
LT: 'Lithuania',
}
const countryCode = pno.slice(3, 5)
return mapping[countryCode]
}
function getGenderFromPersonalNumber(pno) {
const firstDigit = Number(pno.charAt(6))
return firstDigit % 2 === 1 ? 'male' : 'female'
}
return {
name: data.name + ' ' + data.surname,
dob: mapPNOToDOB(data.pno),
citizenship: mapPNOToCitizenship(data.pno),
gender: getGenderFromPersonalNumber(data.pno)
}
}
const searchOFAC = async (minScore, cases) => {
const url = 'https://search.ofac-api.com/v3'
const body = {
apiKey: process.env.OFAC_API_KEY,
source: ['SDN'], // 'UK'
minScore: minScore,
cases: [ cases ]
}
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
const response_ = await response.json()
return response_
}
const getOFACOracleSignature = (isMatched, minScore, currentDate) => {
const privateKey = PrivateKey.fromBase58(process.env.PRIVATE_KEY)
const publicKey = privateKey.toPublicKey()
// encode and sign the data
const mergedArrayOfFields = [
Bool(isMatched).toField(),
Field(minScore),
Field(currentDate),
]
const signature = Signature.create(privateKey, mergedArrayOfFields)
return [signature, publicKey]
}
module.exports = {
verifyOracleData,
transformData,
searchOFAC,
getOFACOracleSignature,
}