11// dec2hex :: Integer -> String
22// i.e. 0-255 -> '00'-'ff'
33function dec2hex ( dec ) {
4- return ( '0' + dec . toString ( 16 ) ) . substr ( - 2 ) ;
4+ return ( '0' + dec . toString ( 16 ) ) . substr ( - 2 ) ;
55}
66
77// generateId :: Integer -> String
88function generateId ( len ) {
9- var arr = new Uint8Array ( ( len || 10 ) / 2 ) ;
10- window . crypto . getRandomValues ( arr ) ;
11- return `"${ Array . from ( arr , dec2hex ) . join ( '' ) } "` ;
9+ var arr = new Uint8Array ( ( len || 10 ) / 2 ) ;
10+ window . crypto . getRandomValues ( arr ) ;
11+ return `"${ Array . from ( arr , dec2hex ) . join ( '' ) } "` ;
1212}
1313
1414// getAukaTimestamp :: Void -> String
1515function getAukaTimestamp ( ) {
16- const d = new Date ( ) ;
17- const fYear = d . getUTCFullYear ( ) . toString ( ) ;
18- const month = ( d . getUTCMonth ( ) + 1 ) . toString ( ) ;
19- const day = d . getUTCDate ( ) . toString ( ) ;
20- const hours = d . getUTCHours ( ) . toString ( ) ;
21- const minutes = parseInt ( d . getUTCMinutes ( ) ) . toString ( ) ;
22- const seconds = parseInt ( d . getUTCSeconds ( ) ) . toString ( ) ;
23-
24- const fMonth = month . length == 2 ? month : `0${ month } ` ;
25- const fDay = day . length == 2 ? day : `0${ day } ` ;
26- const fHours = hours . length == 2 ? hours : `0${ hours } ` ;
27- const fMinutes = minutes . length == 2 ? minutes : `0${ minutes } ` ;
28- const fSeconds = seconds . length == 2 ? seconds : `0${ seconds } ` ;
29-
30- return `${ fYear } -${ fMonth } -${ fDay } ${ fHours } :${ fMinutes } :${ fSeconds } ` ;
16+ const d = new Date ( ) ;
17+ const fYear = d . getUTCFullYear ( ) . toString ( ) ;
18+ const month = ( d . getUTCMonth ( ) + 1 ) . toString ( ) ;
19+ const day = d . getUTCDate ( ) . toString ( ) ;
20+ const hours = d . getUTCHours ( ) . toString ( ) ;
21+ const minutes = parseInt ( d . getUTCMinutes ( ) ) . toString ( ) ;
22+ const seconds = parseInt ( d . getUTCSeconds ( ) ) . toString ( ) ;
23+
24+ const fMonth = month . length == 2 ? month : `0${ month } ` ;
25+ const fDay = day . length == 2 ? day : `0${ day } ` ;
26+ const fHours = hours . length == 2 ? hours : `0${ hours } ` ;
27+ const fMinutes = minutes . length == 2 ? minutes : `0${ minutes } ` ;
28+ const fSeconds = seconds . length == 2 ? seconds : `0${ seconds } ` ;
29+
30+ return `${ fYear } -${ fMonth } -${ fDay } ${ fHours } :${ fMinutes } :${ fSeconds } ` ;
3131}
3232
3333// generatePubKey :: String -> Void
3434function generatePubKey ( privateKey ) {
35- console . log ( 'Generating new public key from private key' ) ;
36- const crypt = new JSEncrypt ( ) ;
37- crypt . setKey ( privateKey ) ;
38- document . getElementById ( 'public_key' ) . value = crypt . getPublicKey ( ) ;
35+ console . log ( 'Generating new public key from private key' ) ;
36+ const crypt = new JSEncrypt ( ) ;
37+ crypt . setKey ( privateKey ) ;
38+ document . getElementById ( 'public_key' ) . value = crypt . getPublicKey ( ) ;
3939}
4040
4141// getHash :: String -> String
4242function getHash ( body ) {
43- return CryptoJS . SHA256 ( body ) . toString ( CryptoJS . enc . Base64 ) ;
43+ return CryptoJS . SHA256 ( body ) . toString ( CryptoJS . enc . Base64 ) ;
4444}
4545
4646// getSignedMessage :: String -> String
4747function getSignedMessage ( privateKey , message ) {
48- const crypt = new JSEncrypt ( ) ;
49- crypt . setKey ( privateKey ) ;
50- return crypt . sign ( message , CryptoJS . SHA256 , 'sha256' ) ;
48+ const crypt = new JSEncrypt ( ) ;
49+ crypt . setKey ( privateKey ) ;
50+ return crypt . sign ( message , CryptoJS . SHA256 , 'sha256' ) ;
5151}
5252
5353// triggerAnimation :: Object -> Void
5454function triggerAnimation ( element ) {
55- element . classList . remove ( 'change-animation' ) ;
56- void element . offsetWidth ;
57- element . classList . add ( 'change-animation' ) ;
55+ element . classList . remove ( 'change-animation' ) ;
56+ void element . offsetWidth ;
57+ element . classList . add ( 'change-animation' ) ;
5858}
5959
6060// copyToClipboard :: Object -> Void
6161function copyToClipboard ( event ) {
62- const element = event . target ;
63- void triggerAnimation ( element ) ;
64- console . log ( `Copying contents of ${ element . name } to clipboard` ) ;
65- element . select ( ) ;
66- document . execCommand ( 'copy' ) ;
67- window . getSelection ( ) . removeAllRanges ( ) ;
62+ const element = event . target ;
63+ void triggerAnimation ( element ) ;
64+ console . log ( `Copying contents of ${ element . name } to clipboard` ) ;
65+ element . select ( ) ;
66+ document . execCommand ( 'copy' ) ;
67+ window . getSelection ( ) . removeAllRanges ( ) ;
6868}
6969
7070let isIntegrator = false ;
7171
7272// setDefaultValues :: Void
7373function setDefaultValues ( ) {
74- const method = '' ;
75- const url = '' ;
76- const merchantID = '' ;
77- const apiUser = '' ;
78- const integratorID = '' ;
79- const privateKey = '' ;
80- const body = '' ;
81-
82- document . getElementById ( 'private_key' ) . value = privateKey ;
83- document . getElementById ( 'method' ) . value = method ;
84- document . getElementById ( 'url' ) . value = url ;
85- document . getElementById ( 'body' ) . value = body ;
86- document . getElementById ( 'merchant_id' ) . value = merchantID ;
87- document . getElementById ( 'api_user' ) . value = apiUser ;
88- document . getElementById ( 'X_Auka_Integrator' ) . value = integratorID ;
74+ const method = '' ;
75+ const url = '' ;
76+ const merchantID = '' ;
77+ const apiUser = '' ;
78+ const integratorID = '' ;
79+ const privateKey = '' ;
80+ const body = '' ;
81+
82+ document . getElementById ( 'private_key' ) . value = privateKey ;
83+ document . getElementById ( 'method' ) . value = method ;
84+ document . getElementById ( 'url' ) . value = url ;
85+ document . getElementById ( 'body' ) . value = body ;
86+ document . getElementById ( 'merchant_id' ) . value = merchantID ;
87+ document . getElementById ( 'api_user' ) . value = apiUser ;
88+ document . getElementById ( 'X_Auka_Integrator' ) . value = integratorID ;
89+ }
90+
91+ // for testing purposes only
92+ function setDefaultValuesDuringTesting ( ) {
93+ const method = 'GET' ;
94+ const url = 'https://api.sandbox.settle.eu/merchant/v1/merchant/fzkmhy0q/balance/' ;
95+ const merchantID = 'fzkmhy0q' ;
96+ const apiUser = 'tg9afjrg' ;
97+ const integratorID = '7e3cbb17' ;
98+ const privateKey = '-----BEGIN RSA PRIVATE KEY-----\
99+ MIICWgIBAAKBgGeEsepm1Dm0LMW9H4cgO8+RpyiQh6JcWKlKfGZahTo3iXq55wGh\
100+ DLOHVP1i5ULPuz8IA3HG1W481AlBeIvT/fmlKy/zjNUebAClvujpKjMRkn2p0Npg\
101+ kyC4b17ZtoEkmixM2SrVhxBpy1PJoLFNKILqOGF+nFJ3Du/AEDOTNMrzAgMBAAEC\
102+ gYAJYriW3hfj23grvYf8Qmnp2fTj8qa5i9HmF4DL7u0haCOo4u4U8bsrE9wa1Tqg\
103+ IiGCB4H4cOStCArZg/wgAWqHeHKyn5+U74hbUnVMwj79zPgySJ/olFtrMptS8Jwe\
104+ 28zGDua91T7a8y/12HY+a4EGQd/K6S8z3lgnMBFcebI84QJBAKS1wVM0Nu4RZmSS\
105+ 50GWWui3sdHxe8OxyyiDUdBxajTuIuIH7/A6SOILRGs7cSF3ST9BVk0Lsx+jejvE\
106+ rg30E9sCQQCg5KBqp93nZs0ky+DuVK63HYVo9+AF6r7bNXvvX9L0V0FaaUgqdjeL\
107+ aonhBQ0VvmtCya+6poyptSbAVEmA09zJAkBI6VhaD6wdOMCd1tXeF8PIbsCdkgta\
108+ dpLbLT6DSiFcqunwKtlQ+0wWHCy+V0LeMKLRCIg+dOZnJAPQ/2CZNqmvAkAl2yVT\
109+ cwPnOmzyR3Y5HXuuYifNtuTi/4TAlyj9/ZHpI86gszzjoMUY7IxcgY++mfsqz8Gl\
110+ LSLTm2fuwOY6hZ7hAkAmww+iGigsQK/qFUenQ1afn9hQxsLrriOgBKNZygqFBHh4\
111+ 4u6VK3BHZuYEpMcEzY6JSEKxucN7rZ8CulNO9A0w\
112+ -----END RSA PRIVATE KEY-----' ;
113+ const body = '' ;
114+
115+ document . getElementById ( 'private_key' ) . value = privateKey ;
116+ document . getElementById ( 'method' ) . value = method ;
117+ document . getElementById ( 'url' ) . value = url ;
118+ document . getElementById ( 'body' ) . value = body ;
119+ document . getElementById ( 'merchant_id' ) . value = merchantID ;
120+ document . getElementById ( 'api_user' ) . value = apiUser ;
121+ document . getElementById ( 'X_Auka_Integrator' ) . value = integratorID ;
89122}
90123
91124document . getElementById ( 'controllGroupIntegrator' ) . style . display = 'none' ;
92125
93126function integratorFalse ( ) {
94- document . getElementById ( 'controllGroupApiUser' ) . style . display = 'block' ;
95- document . getElementById ( 'controllGroupIntegrator' ) . style . display = 'none' ;
96- isIntegrator = false ;
97- // console.log(isIntegrator);
98- generateHeaders ( ) ;
127+ document . getElementById ( 'controllGroupApiUser' ) . style . display = 'block' ;
128+ document . getElementById ( 'controllGroupIntegrator' ) . style . display = 'none' ;
129+ isIntegrator = false ;
130+ // console.log(isIntegrator);
131+ generateHeaders ( ) ;
99132}
100133
101134function integratorTrue ( ) {
102- document . getElementById ( 'controllGroupApiUser' ) . style . display = 'none' ;
103- document . getElementById ( 'controllGroupIntegrator' ) . style . display = 'block' ;
104- isIntegrator = true ;
105- // console.log(isIntegrator);
106- generateHeaders ( ) ;
135+ document . getElementById ( 'controllGroupApiUser' ) . style . display = 'none' ;
136+ document . getElementById ( 'controllGroupIntegrator' ) . style . display = 'block' ;
137+ isIntegrator = true ;
138+ // console.log(isIntegrator);
139+ generateHeaders ( ) ;
107140}
108141
109142// generateHeaders :: Void
110143function generateHeaders ( ) {
111- // console.log("Generating hedears");
112- const element = document . getElementById ( 'bulk_headers' ) ;
113- void triggerAnimation ( element ) ;
114-
115- if ( isIntegrator == false ) {
116- console . log ( 'Generating hedears with X-Auka-User' ) ;
117-
118- const privateKey = document . getElementById ( 'private_key' ) . value ;
119- const method = document . getElementById ( 'method' ) . value ;
120- const url = document . getElementById ( 'url' ) . value ;
121- const merchantID = document . getElementById ( 'merchant_id' ) . value ;
122- const timestamp = getAukaTimestamp ( ) ;
123- const apiUser = document . getElementById ( 'api_user' ) . value ;
124- const body = document . getElementById ( 'body' ) . value ;
125- const bodyHash = getHash ( body ) ;
126- const contentDigest = `SHA256=${ bodyHash } ` ;
127- const headers = `X-AUKA-CONTENT-DIGEST=${ contentDigest } &X-AUKA-MERCHANT=${ merchantID } &X-AUKA-TIMESTAMP=${ timestamp } &X-AUKA-USER=${ apiUser } ` ;
128- const message = `${ method } |${ url } |${ headers } ` ;
129- const signature = getSignedMessage ( privateKey , message ) ;
130- const authorization = 'RSA-SHA256 ' + signature ;
131- const bulkHeaders = `Accept:application/vnd.mcash.api.merchant.v1+json
144+ // console.log("Generating hedears");
145+ const element = document . getElementById ( 'bulk_headers' ) ;
146+ void triggerAnimation ( element ) ;
147+
148+ if ( isIntegrator == false ) {
149+ console . log ( 'Generating hedears with X-Auka-User' ) ;
150+
151+ const privateKey = document . getElementById ( 'private_key' ) . value ;
152+ const method = document . getElementById ( 'method' ) . value ;
153+ const url = document . getElementById ( 'url' ) . value ;
154+ const merchantID = document . getElementById ( 'merchant_id' ) . value ;
155+ const timestamp = getAukaTimestamp ( ) ;
156+ const apiUser = document . getElementById ( 'api_user' ) . value ;
157+ const body = document . getElementById ( 'body' ) . value ;
158+ const bodyHash = getHash ( body ) ;
159+ const contentDigest = `SHA256=${ bodyHash } ` ;
160+ const headers = `X-AUKA-CONTENT-DIGEST=${ contentDigest } &X-AUKA-MERCHANT=${ merchantID } &X-AUKA-TIMESTAMP=${ timestamp } &X-AUKA-USER=${ apiUser } ` ;
161+ const message = `${ method } |${ url } |${ headers } ` ;
162+ const signature = getSignedMessage ( privateKey , message ) ;
163+ const authorization = 'RSA-SHA256 ' + signature ;
164+ const bulkHeaders = `Accept:application/vnd.mcash.api.merchant.v1+json
132165Content-Type:application/json
133166X-Auka-Merchant:${ merchantID }
134167X-Auka-User:${ apiUser }
135168X-Auka-Timestamp:${ timestamp }
136169X-Auka-Content-Digest:${ contentDigest }
137170Authorization:${ authorization } ` ;
138171
139- element . value = bulkHeaders ;
140- } else {
141- console . log ( 'Generating hedears with X-Auka-Integrator' ) ;
142-
143- const privateKey = document . getElementById ( 'private_key' ) . value ;
144- const method = document . getElementById ( 'method' ) . value ;
145- const url = document . getElementById ( 'url' ) . value ;
146- const merchantID = document . getElementById ( 'merchant_id' ) . value ;
147- const timestamp = getAukaTimestamp ( ) ;
148- const integratorID = document . getElementById ( 'X_Auka_Integrator' ) . value ;
149- const body = document . getElementById ( 'body' ) . value ;
150- const bodyHash = getHash ( body ) ;
151- const contentDigest = `SHA256=${ bodyHash } ` ;
152- const headers = `X-AUKA-CONTENT-DIGEST=${ contentDigest } &X-AUKA-MERCHANT =${ merchantID } &X-AUKA-TIMESTAMP =${ timestamp } &X-Auka-Integrator =${ integratorID } ` ;
153- const message = `${ method } |${ url } |${ headers } ` ;
154- const signature = getSignedMessage ( privateKey , message ) ;
155- const authorization = 'RSA-SHA256 ' + signature ;
156- const bulkHeaders = `Accept:application/vnd.mcash.api.merchant.v1+json
172+ element . value = bulkHeaders ;
173+ } else {
174+ console . log ( 'Generating hedears with X-Auka-Integrator' ) ;
175+
176+ const privateKey = document . getElementById ( 'private_key' ) . value ;
177+ const method = document . getElementById ( 'method' ) . value ;
178+ const url = document . getElementById ( 'url' ) . value ;
179+ const merchantID = document . getElementById ( 'merchant_id' ) . value ;
180+ const timestamp = getAukaTimestamp ( ) ;
181+ const integratorID = document . getElementById ( 'X_Auka_Integrator' ) . value ;
182+ const body = document . getElementById ( 'body' ) . value ;
183+ const bodyHash = getHash ( body ) ;
184+ const contentDigest = `SHA256=${ bodyHash } ` ;
185+ const headers = `X-AUKA-CONTENT-DIGEST=${ contentDigest } &X-AUKA-INTEGRATOR =${ integratorID } &X-AUKA-MERCHANT =${ merchantID } &X-AUKA-TIMESTAMP =${ timestamp } ` ;
186+ const message = `${ method } |${ url } |${ headers } ` ;
187+ const signature = getSignedMessage ( privateKey , message ) ;
188+ const authorization = 'RSA-SHA256 ' + signature ;
189+ const bulkHeaders = `Accept:application/vnd.mcash.api.merchant.v1+json
157190Content-Type:application/json
158191X-Auka-Merchant:${ merchantID }
159192X-Auka-Integrator:${ integratorID }
160193X-Auka-Timestamp:${ timestamp }
161194X-Auka-Content-Digest:${ contentDigest }
162195Authorization:${ authorization } ` ;
163196
164- element . value = bulkHeaders ;
165- }
197+ element . value = bulkHeaders ;
198+ }
166199}
167200
168201// Main script
@@ -171,9 +204,10 @@ document.getElementById('method').onchange = () => generateHeaders();
171204document . getElementById ( 'url' ) . onchange = ( ) => generateHeaders ( ) ;
172205document . getElementById ( 'body' ) . onchange = ( ) => generateHeaders ( ) ;
173206document . getElementById ( 'bulk_headers' ) . onclick = ( event ) =>
174- copyToClipboard ( event ) ;
207+ copyToClipboard ( event ) ;
175208
176209setDefaultValues ( ) ;
210+ // setDefaultValuesDuringTesting();
177211generateHeaders ( ) ;
178212
179213// console.log(isIntegrator);
0 commit comments