· 5 years ago · May 16, 2020, 09:06 AM
1@ExperimentalStdlibApi
2class CoinbaseAuthHelper(private val logger: Logger,
3 private val token: Token,
4 clock: Clock,
5 private val httpMethod: String,
6 private val path: String,
7 private val body: String) {
8
9 val coinbaseSignature: String = "CB-ACCESS-SIGN"
10 val coinbaseKey: String = "CB-ACCESS-KEY"
11 val coinbaseTimestamp: String = "CB-ACCESS-TIMESTAMP"
12 val coinbasePassphrase: String = "CB-ACCESS-PASSPHRASE"
13 private val algorithm = "HmacSHA256"
14 private val sha256: Mac = Mac.getInstance(algorithm)
15
16 val timestamp = clock.instant().toEpochMilli().toString()
17 private val preHash: String
18
19 init {
20 preHash = configurePreHashMessage()
21 }
22
23 private fun configurePreHashMessage(): String = timestamp + httpMethod + path + body
24
25 fun getSignature(): String {
26 val secretKey = SecretKeySpec(Base64.getDecoder().decode(token.apiSecret), algorithm)
27 sha256.init(secretKey)
28
29 return Base64.getEncoder().encodeToString(sha256.doFinal(preHash.toByteArray()))
30 }
31}