From be11be68f89d4a159fc4d01fcc8adb7bbea02762 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Tue, 12 Nov 2024 17:07:28 +0100 Subject: [PATCH 01/16] First version of extension for Coinbase V3 API --- README.md | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2a11ee8..fc49d4b 100644 --- a/README.md +++ b/README.md @@ -8,19 +8,41 @@ As of version 2.0, currencies whose number is `0` will no longer be displayed, p However, the entire history for the Coinbase account remains. +# Coinbase V3 API + +Coinbase has released their new Advanced Trade API, which can be accessed at the endpoint: [https://api.coinbase.com/api/v3/brokerage/](https://api.coinbase.com/api/v3/brokerage/). The API documentation can be found here: [https://docs.cdp.coinbase.com/advanced-trade/docs/welcome](https://docs.cdp.coinbase.com/advanced-trade/docs/welcome). + +The main challenge in developing this extension was implementing their new authentication method, which is based on JWT tokens. The JWT token is generated by the extension and sent to the API in the header of each request. JWT signing is done using Elliptic Curve Cryptography (ECC) with the `prime256v1` curve. Therefore, the extension makes use of some undocumented `MM` functions to sign the JWT token. The token is generated using the key name and private key provided by the user. + +To avoid disrupting users who are still using the old API, this extension is created as a new extension and explicitly called `Coinbase V3`. + +**Important:** + +1. This extension will only work with the new Advanced Trade API and not with the old V2 API. +2. You must provide your credentials as a key name and a private key. In MoneyMoney, you need to enter the **key name** as the **username** and the **private key** as the **password**. + +If you download your API credentials from Coinbase in JSON format, they will look like this: + +```json +{ + "name": "organizations/.../apiKeys/...", + "privateKey": "-----BEGIN EC PRIVATE KEY-----\nM...9\nA...e\nu...A==\n-----END EC PRIVATE KEY-----\n" +} + + ## Extension Setup You can get a signed version of this extension from * the `dist` directory in this repository -Once downloaded, move `Coinbase.lua` to your MoneyMoney Extensions folder. +Once downloaded, move `Coinbase.lua` for V2 or `CoinbaseV3.lua` for V3 to your MoneyMoney Extensions folder. **Note:** This extension requires MoneyMoney Version 2.2.18 (288) or newer. ## Account Setup -### Coinbase +### Coinbase V2 API 1. Log in to your Coinbase account 2. Go to Settings → API @@ -29,6 +51,17 @@ Once downloaded, move `Coinbase.lua` to your MoneyMoney Extensions folder. 5. Under "API v2 Permissions", check "wallet:user:read" and "wallet:accounts:read" (the others aren’t needed) 5. Click "Create" +### Coinbase V3 API + +1. Log in to the developer portal at https://portal.cdp.coinbase.com/ +2. Create or select a project +3. Go to API Keys +3. Click "Create API Key" +4. Give the key a name, e.g. "MoneyMoney" +5. Make sure that only `View (read-only)` is selected (we don't need to trade) +5. Click "Create & download" + + ### MoneyMoney Add a new account (type "Coinbase Account") and use your Coinbase API key as username and your Coinbase API secret as password. From 5e61d075341c68301b2895dc377208bb56082391 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Tue, 12 Nov 2024 17:09:44 +0100 Subject: [PATCH 02/16] First version --- src/CoinbaseV3.lua | 238 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 src/CoinbaseV3.lua diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua new file mode 100644 index 0000000..0f6b0a6 --- /dev/null +++ b/src/CoinbaseV3.lua @@ -0,0 +1,238 @@ +-- Inofficial Coinbase Extension (www.coinbase.com) for MoneyMoney +-- Fetches balances from Coinbase API V3 and returns them as securities +-- +-- Username: Coinbase Key Name +-- Password: Coinbase Private Key +-- +-- Copyright (c) 2024 Felix Nensa +-- Copyright (c) 2020-2022 Martin Wilhelmi +-- Copyright (c) 2017 Nico Lindemann +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in all +-- copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +-- SOFTWARE. + +WebBanking { + version = 1.0, + url = "https://api.coinbase.com", + description = "Fetch balances from Coinbase API V3 and list them as securities", + services = { "Coinbase V3" } +} + +local apiKey +local apiSecret +local currency +local balances +local prices +local api_host = "api.coinbase.com" +local api_path = "/api/v3/brokerage/" +local market = "Coinbase" +local accountNumber = "Main" + +function SupportsBank (protocol, bankCode) + return protocol == ProtocolWebBanking and bankCode == "Coinbase V3" +end + +function InitializeSession (protocol, bankCode, username, username2, password, username3) + apiKey = username + apiSecret = password + -- Take currency of first fiat account, ignores pagination limit (default: 49) + -- See: https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getaccounts/ + local accounts = queryPrivate("accounts") + local size = accounts["size"] + for i = 1, size do + local account = accounts["accounts"][i] + if account["type"] == "ACCOUNT_TYPE_FIAT" then + currency = account["currency"] + break + end + end +end + +function ListAccounts (knownAccounts_notused) + local account = { + name = market, + accountNumber = accountNumber, + currency = currency, + portfolio = true, + type = "AccountTypePortfolio" + } + return {account} +end + +function RefreshAccount(account_notused, since_notused) + local s = {} + local accounts = queryPrivate("accounts") + + local size = accounts["size"] + for i = 1, size do + local account = accounts["accounts"][i] + if account["type"] == "ACCOUNT_TYPE_FIAT" then + s[#s+1] = { + name = account["name"], + market = market, + currency = account["currency"], + amount = account["available_balance"]["value"] + } + else + local prices = nil + -- Answer is 'Unauthorized' for BSV which makes MM exit with an error (double fail) + if account["currency"] ~= 'BSV' and account["currency"] ~= 'ETH2' then + prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) + end + + if prices == nil or prices["error"] then + s[#s+1] = { + name = account["name"], + market = market, + currency = account["currency"], + quantity = account["available_balance"]["value"], + amount = nil, + price = nil + } + else + s[#s+1] = { + name = account["name"], + market = market, + currency = account["currency"], + quantity = account["available_balance"]["value"], + amount = account["available_balance"]["value"] * prices["price"], + price = prices["price"] + } + end + end + end + + return {securities = s} +end + +function EndSession () +end + +-------------------- Helper functions -------------------- + +-- Function to pad a byte string to a specific length with leading zeros +local function pad_to_length(data, length) + if #data < length then + data = string.rep("\0", length - #data) .. data + elseif #data > length then + data = data:sub(-length) -- Trim to the required length if longer + end + return data +end + +-- Function to convert a DER-encoded signature to concatenated r || s format +-- Based on https://stackoverflow.com/a/69109085/5347900 +local function der_to_concat_rs(der_signature) + local pos = 1 + assert(der_signature:byte(pos) == 0x30, "Expected SEQUENCE") + pos = pos + 1 -- Skip SEQUENCE tag + + local length = der_signature:byte(pos) + pos = pos + 1 + + -- Extract r value + assert(der_signature:byte(pos) == 0x02, "Expected INTEGER for r") + pos = pos + 1 + local r_len = der_signature:byte(pos) + pos = pos + 1 + local r = der_signature:sub(pos, pos + r_len - 1) + pos = pos + r_len + + -- Extract s value + assert(der_signature:byte(pos) == 0x02, "Expected INTEGER for s") + pos = pos + 1 + local s_len = der_signature:byte(pos) + pos = pos + 1 + local s = der_signature:sub(pos, pos + s_len - 1) + pos = pos + s_len + + -- Ensure r and s are 32 bytes by adding leading zeros if necessary + r = pad_to_length(r, 32) + s = pad_to_length(s, 32) + + -- Concatenate r || s + return r .. s +end + +-- Function to create JWT token using ES256 and MM helper functions +function create_jwt(apiSecret, header, payload) + local json_header = JSON():set(header):json() + local json_payload = JSON():set(payload):json() + + -- Ensure header and payload are BASE64 encoded + local encoded_header = MM.base64urlencode(json_header) + local encoded_payload = MM.base64urlencode(json_payload) + + local signature_input = encoded_header .. "." .. encoded_payload + + -- Load and parse the EC private key + local key = apiSecret + key = string.match(key, "BEGIN EC PRIVATE KEY%-%-%-%-%-%s*(.*)%-%-%-%-%-END EC PRIVATE KEY") + key = string.gsub(key, "%s+", "") + key = MM.base64decode(key) + + local der = MM.derdecode(MM.derdecode(key)[1][2]) + local d = der[2][2] + local p = MM.derdecode(der[4][2])[1][2] + local x = string.sub(p, string.len(p) - 63, string.len(p) - 32) + local y = string.sub(p, string.len(p) - 31) + + -- Sign the data using MM.ecSign + local signature = MM.ecSign({curve="prime256v1", d=d, x=x, y=y}, signature_input, "ecdsa sha256") + local rs_signature = der_to_concat_rs(signature) + local encoded_signature = MM.base64urlencode(rs_signature) + + -- Construct and return the JWT + return encoded_header .. "." .. encoded_payload .. "." .. encoded_signature +end + +-- Generate a hexadecimal nonce +local function generate_hex_nonce(length) + local res = {} + local hex_chars = '0123456789abcdef' + + for i = 1, length do + local rand_index = math.random(1, #hex_chars) + table.insert(res, hex_chars:sub(rand_index, rand_index)) + end + + return table.concat(res) +end + +-- Query the advanced trade API with a private method +-- See: https://docs.cdp.coinbase.com/advanced-trade/reference/ +function queryPrivate(method) + local request_method = "GET" + local nonce = generate_hex_nonce(64) + local uri = request_method .. " " .. api_host .. api_path .. method + local nbf = os.time() + local exp = nbf + 120 + local header = {alg = "ES256", kid = apiKey, nonce = nonce, typ = "JWT"} + local payload = {sub = apiKey, iss = "cdp", nbf = nbf, exp = exp, uri = uri} + local jwt_token = create_jwt(apiSecret, header, payload) + + local path = api_path .. method + local headers = {} + headers["Authorization"] = "Bearer " .. jwt_token + + local connection = Connection() + local content = connection:request("GET", url .. path, nil, nil, headers) + + local json = JSON(content) + return json:dictionary() +end \ No newline at end of file From 180453131f4f50fbaa6dd1e90c090ef327556a6a Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Tue, 12 Nov 2024 17:12:38 +0100 Subject: [PATCH 03/16] Typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fc49d4b..e73eeaf 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ If you download your API credentials from Coinbase in JSON format, they will loo ```json { "name": "organizations/.../apiKeys/...", - "privateKey": "-----BEGIN EC PRIVATE KEY-----\nM...9\nA...e\nu...A==\n-----END EC PRIVATE KEY-----\n" + "privateKey": "-----BEGIN EC PRIVATE KEY-----\nM...9\nA...e\nu...==\n-----END EC PRIVATE KEY-----\n" } - +``` ## Extension Setup From 9b9ee1748231e705e44b5f307cdc6ecd0b2863f1 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Tue, 12 Nov 2024 17:14:43 +0100 Subject: [PATCH 04/16] small improvement on instructions --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e73eeaf..9633ddf 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ If you download your API credentials from Coinbase in JSON format, they will loo } ``` +Use the name as the key name and the privateKey as the private key. Do not change anything, and make sure to copy the entire key, including the `-----BEGIN EC PRIVATE KEY-----` and `-----END EC PRIVATE KEY-----` lines. Ensure that you do not include any accidental whitespace at the beginning or end of the key. + ## Extension Setup You can get a signed version of this extension from From e4b62cab1b7a4967606988897ae7aa0a0cb05135 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Tue, 12 Nov 2024 17:18:59 +0100 Subject: [PATCH 05/16] Delgate none generation to MM functions --- src/CoinbaseV3.lua | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index 0f6b0a6..dd0b697 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -201,24 +201,11 @@ function create_jwt(apiSecret, header, payload) return encoded_header .. "." .. encoded_payload .. "." .. encoded_signature end --- Generate a hexadecimal nonce -local function generate_hex_nonce(length) - local res = {} - local hex_chars = '0123456789abcdef' - - for i = 1, length do - local rand_index = math.random(1, #hex_chars) - table.insert(res, hex_chars:sub(rand_index, rand_index)) - end - - return table.concat(res) -end - -- Query the advanced trade API with a private method -- See: https://docs.cdp.coinbase.com/advanced-trade/reference/ function queryPrivate(method) local request_method = "GET" - local nonce = generate_hex_nonce(64) + local nonce = string.lower(MM.binToHex(MM.random(32))) local uri = request_method .. " " .. api_host .. api_path .. method local nbf = os.time() local exp = nbf + 120 From c9d652d84d913d30b47116a4516a02ba48dbcb3f Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Tue, 12 Nov 2024 18:57:24 +0100 Subject: [PATCH 06/16] Set Accept: application/json which makes MM.Connection() to behave differently and not stop the whole execution on HTTP errors --- src/CoinbaseV3.lua | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index dd0b697..6484b3f 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -27,7 +27,7 @@ -- SOFTWARE. WebBanking { - version = 1.0, + version = 2.0, url = "https://api.coinbase.com", description = "Fetch balances from Coinbase API V3 and list them as securities", services = { "Coinbase V3" } @@ -89,12 +89,9 @@ function RefreshAccount(account_notused, since_notused) amount = account["available_balance"]["value"] } else - local prices = nil - -- Answer is 'Unauthorized' for BSV which makes MM exit with an error (double fail) - if account["currency"] ~= 'BSV' and account["currency"] ~= 'ETH2' then - prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) - end - + + local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) + if prices == nil or prices["error"] then s[#s+1] = { name = account["name"], @@ -201,11 +198,24 @@ function create_jwt(apiSecret, header, payload) return encoded_header .. "." .. encoded_payload .. "." .. encoded_signature end +-- Generate a hexadecimal nonce +local function generate_hex_nonce(length) + local res = {} + local hex_chars = '0123456789abcdef' + + for i = 1, length do + local rand_index = math.random(1, #hex_chars) + table.insert(res, hex_chars:sub(rand_index, rand_index)) + end + + return table.concat(res) +end + -- Query the advanced trade API with a private method -- See: https://docs.cdp.coinbase.com/advanced-trade/reference/ function queryPrivate(method) local request_method = "GET" - local nonce = string.lower(MM.binToHex(MM.random(32))) + local nonce = generate_hex_nonce(64) local uri = request_method .. " " .. api_host .. api_path .. method local nbf = os.time() local exp = nbf + 120 @@ -215,6 +225,7 @@ function queryPrivate(method) local path = api_path .. method local headers = {} + headers["Accept"] = "application/json" headers["Authorization"] = "Bearer " .. jwt_token local connection = Connection() From ab0906f707898f784d2c668896fce919c623a382 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sat, 16 Nov 2024 15:58:31 +0100 Subject: [PATCH 07/16] Just bump V2 to V3 to keep history in old V2 accounts --- src/CoinbaseV3.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index 6484b3f..4eac91d 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -27,10 +27,10 @@ -- SOFTWARE. WebBanking { - version = 2.0, + version = 3.0, url = "https://api.coinbase.com", description = "Fetch balances from Coinbase API V3 and list them as securities", - services = { "Coinbase V3" } + services = { "Coinbase Account" } } local apiKey @@ -44,7 +44,7 @@ local market = "Coinbase" local accountNumber = "Main" function SupportsBank (protocol, bankCode) - return protocol == ProtocolWebBanking and bankCode == "Coinbase V3" + return protocol == ProtocolWebBanking and bankCode == "Coinbase Account" end function InitializeSession (protocol, bankCode, username, username2, password, username3) From 444a6a60c1c83cc86fde25806f319554bdcf7cd0 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sat, 16 Nov 2024 16:15:37 +0100 Subject: [PATCH 08/16] filter accounts that are empty or have zero balance --- src/CoinbaseV3.lua | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index 4eac91d..61f071c 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -92,24 +92,27 @@ function RefreshAccount(account_notused, since_notused) local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) - if prices == nil or prices["error"] then - s[#s+1] = { - name = account["name"], - market = market, - currency = account["currency"], - quantity = account["available_balance"]["value"], - amount = nil, - price = nil - } - else - s[#s+1] = { - name = account["name"], - market = market, - currency = account["currency"], - quantity = account["available_balance"]["value"], - amount = account["available_balance"]["value"] * prices["price"], - price = prices["price"] - } + -- filter accounts that are empty or have zero balance + if type(account["available_balance"]["value"]) == "number" and account["available_balance"]["value"] ~= 0 then + if prices == nil or prices["error"] then + s[#s+1] = { + name = account["name"], + market = market, + currency = account["currency"], + quantity = account["available_balance"]["value"], + amount = nil, + price = nil + } + else + s[#s+1] = { + name = account["name"], + market = market, + currency = account["currency"], + quantity = account["available_balance"]["value"], + amount = account["available_balance"]["value"] * prices["price"], + price = prices["price"] + } + end end end end From 921f5cc8faf0d691745bef8ad0ce6903d54e793b Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sat, 16 Nov 2024 17:59:01 +0100 Subject: [PATCH 09/16] also remove empty fiat accounts from list --- src/CoinbaseV3.lua | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index 61f071c..cc1f95e 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -81,19 +81,18 @@ function RefreshAccount(account_notused, since_notused) local size = accounts["size"] for i = 1, size do local account = accounts["accounts"][i] - if account["type"] == "ACCOUNT_TYPE_FIAT" then - s[#s+1] = { - name = account["name"], - market = market, - currency = account["currency"], - amount = account["available_balance"]["value"] - } - else - - local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) - - -- filter accounts that are empty or have zero balance - if type(account["available_balance"]["value"]) == "number" and account["available_balance"]["value"] ~= 0 then + -- filter accounts that are empty or have zero balance + if type(account["available_balance"]["value"]) == "number" and account["available_balance"]["value"] ~= 0 then + if account["type"] == "ACCOUNT_TYPE_FIAT" then + s[#s+1] = { + name = account["name"], + market = market, + currency = account["currency"], + amount = account["available_balance"]["value"] + } + else + + local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) if prices == nil or prices["error"] then s[#s+1] = { name = account["name"], From 2811747fd8c2660b2605e05030a1b0dc1783ac9c Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sat, 16 Nov 2024 18:05:06 +0100 Subject: [PATCH 10/16] More rigorous filtering of empty accounts --- src/CoinbaseV3.lua | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index cc1f95e..d0a6074 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -81,37 +81,25 @@ function RefreshAccount(account_notused, since_notused) local size = accounts["size"] for i = 1, size do local account = accounts["accounts"][i] - -- filter accounts that are empty or have zero balance - if type(account["available_balance"]["value"]) == "number" and account["available_balance"]["value"] ~= 0 then - if account["type"] == "ACCOUNT_TYPE_FIAT" then + if account["type"] == "ACCOUNT_TYPE_FIAT" and tonumber(account["available_balance"]["value"]) > 0 then + s[#s+1] = { + name = account["name"], + market = market, + currency = account["currency"], + amount = tonumber(account["available_balance"]["value"]) + } + else + local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) + + if prices ~= nil and prices["error"] == nil and tonumber(account["available_balance"]["value"]) > 0 then s[#s+1] = { name = account["name"], market = market, - currency = account["currency"], - amount = account["available_balance"]["value"] + -- currency = account["currency"], + quantity = tonumber(account["available_balance"]["value"]), + amount = tonumber(account["available_balance"]["value"]) * tonumber(prices["price"]), + price = tonumber(prices["price"]) } - else - - local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) - if prices == nil or prices["error"] then - s[#s+1] = { - name = account["name"], - market = market, - currency = account["currency"], - quantity = account["available_balance"]["value"], - amount = nil, - price = nil - } - else - s[#s+1] = { - name = account["name"], - market = market, - currency = account["currency"], - quantity = account["available_balance"]["value"], - amount = account["available_balance"]["value"] * prices["price"], - price = prices["price"] - } - end end end end From f7418c874c5a085a1e7364614b1d1cda1fe09c0c Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sat, 16 Nov 2024 22:54:21 +0100 Subject: [PATCH 11/16] Only consider accounts with a balance != 0 --- src/CoinbaseV3.lua | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index d0a6074..3b62dc9 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -81,25 +81,26 @@ function RefreshAccount(account_notused, since_notused) local size = accounts["size"] for i = 1, size do local account = accounts["accounts"][i] - if account["type"] == "ACCOUNT_TYPE_FIAT" and tonumber(account["available_balance"]["value"]) > 0 then - s[#s+1] = { - name = account["name"], - market = market, - currency = account["currency"], - amount = tonumber(account["available_balance"]["value"]) - } - else - local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) - - if prices ~= nil and prices["error"] == nil and tonumber(account["available_balance"]["value"]) > 0 then + if math.abs(tonumber(account["available_balance"]["value"])) > 0 then + if account["type"] == "ACCOUNT_TYPE_FIAT" then s[#s+1] = { name = account["name"], market = market, - -- currency = account["currency"], - quantity = tonumber(account["available_balance"]["value"]), - amount = tonumber(account["available_balance"]["value"]) * tonumber(prices["price"]), - price = tonumber(prices["price"]) + currency = account["currency"], + amount = tonumber(account["available_balance"]["value"]) } + else + local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) + if prices ~= nil and prices["error"] == nil then + s[#s+1] = { + name = account["name"], + market = market, + -- currency = account["currency"], + quantity = tonumber(account["available_balance"]["value"]), + amount = tonumber(account["available_balance"]["value"]) * tonumber(prices["price"]), + price = tonumber(prices["price"]) + } + end end end end From c61fe92118dc9e28a9b9f507e146f018c9c26ffd Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sat, 16 Nov 2024 22:56:12 +0100 Subject: [PATCH 12/16] Math module import --- src/CoinbaseV3.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index 3b62dc9..b8bd0d3 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -43,6 +43,9 @@ local api_path = "/api/v3/brokerage/" local market = "Coinbase" local accountNumber = "Main" +-- Import the math module +local math = require("math") + function SupportsBank (protocol, bankCode) return protocol == ProtocolWebBanking and bankCode == "Coinbase Account" end From 41a7017a13c312e278354e619bdf29ee0d627c41 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sun, 17 Nov 2024 11:37:44 +0100 Subject: [PATCH 13/16] Make sure that available_balance is a number --- src/CoinbaseV3.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index b8bd0d3..0b9e2fd 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -84,13 +84,14 @@ function RefreshAccount(account_notused, since_notused) local size = accounts["size"] for i = 1, size do local account = accounts["accounts"][i] - if math.abs(tonumber(account["available_balance"]["value"])) > 0 then + local available_balance = tonumber(account["available_balance"]["value"]) + if available_balance ~= nil and math.abs(available_balance) > 0 then if account["type"] == "ACCOUNT_TYPE_FIAT" then s[#s+1] = { name = account["name"], market = market, currency = account["currency"], - amount = tonumber(account["available_balance"]["value"]) + amount = available_balance } else local prices = queryPrivate("market/products/" .. account["currency"] .. "-" .. currency) @@ -99,8 +100,8 @@ function RefreshAccount(account_notused, since_notused) name = account["name"], market = market, -- currency = account["currency"], - quantity = tonumber(account["available_balance"]["value"]), - amount = tonumber(account["available_balance"]["value"]) * tonumber(prices["price"]), + quantity = available_balance, + amount = available_balance * tonumber(prices["price"]), price = tonumber(prices["price"]) } end From d06811862d029c162bdbf9ba280db85ff038a2a2 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sun, 17 Nov 2024 19:01:44 +0100 Subject: [PATCH 14/16] Local implementation of abs() function --- src/CoinbaseV3.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index 0b9e2fd..c54754e 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -43,9 +43,6 @@ local api_path = "/api/v3/brokerage/" local market = "Coinbase" local accountNumber = "Main" --- Import the math module -local math = require("math") - function SupportsBank (protocol, bankCode) return protocol == ProtocolWebBanking and bankCode == "Coinbase Account" end @@ -85,7 +82,7 @@ function RefreshAccount(account_notused, since_notused) for i = 1, size do local account = accounts["accounts"][i] local available_balance = tonumber(account["available_balance"]["value"]) - if available_balance ~= nil and math.abs(available_balance) > 0 then + if abs(available_balance) > 0 then if account["type"] == "ACCOUNT_TYPE_FIAT" then s[#s+1] = { name = account["name"], @@ -99,7 +96,6 @@ function RefreshAccount(account_notused, since_notused) s[#s+1] = { name = account["name"], market = market, - -- currency = account["currency"], quantity = available_balance, amount = available_balance * tonumber(prices["price"]), price = tonumber(prices["price"]) @@ -117,6 +113,14 @@ end -------------------- Helper functions -------------------- +-- Define a local abs() function +function abs(x) + if x ~= nil then + return (x < 0) and -x or x + end + return nil +end + -- Function to pad a byte string to a specific length with leading zeros local function pad_to_length(data, length) if #data < length then From 7565c2a2d7aaa0010480da83bfa110ec42039e5d Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sun, 17 Nov 2024 21:17:45 +0100 Subject: [PATCH 15/16] replace n with actual line breaks in private key --- src/CoinbaseV3.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index c54754e..a3690a8 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -121,6 +121,11 @@ function abs(x) return nil end +-- Replace all "\n" with actual line breaks +local function replace_newlines(str) + return str:gsub("\\n", "\n") +end + -- Function to pad a byte string to a specific length with leading zeros local function pad_to_length(data, length) if #data < length then @@ -178,6 +183,7 @@ function create_jwt(apiSecret, header, payload) -- Load and parse the EC private key local key = apiSecret + key = replace_newlines(key) -- Replace "\n" with actual line breaks key = string.match(key, "BEGIN EC PRIVATE KEY%-%-%-%-%-%s*(.*)%-%-%-%-%-END EC PRIVATE KEY") key = string.gsub(key, "%s+", "") key = MM.base64decode(key) From d5b71ebf51311f131573b39bc709179606456ae8 Mon Sep 17 00:00:00 2001 From: Felix Nensa Date: Sun, 17 Nov 2024 21:46:29 +0100 Subject: [PATCH 16/16] default to EUR if no fiat account is found --- src/CoinbaseV3.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CoinbaseV3.lua b/src/CoinbaseV3.lua index a3690a8..4bcae75 100644 --- a/src/CoinbaseV3.lua +++ b/src/CoinbaseV3.lua @@ -52,6 +52,8 @@ function InitializeSession (protocol, bankCode, username, username2, password, u apiSecret = password -- Take currency of first fiat account, ignores pagination limit (default: 49) -- See: https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getaccounts/ + -- default to EUR if no fiat account is found + currency = "EUR" local accounts = queryPrivate("accounts") local size = accounts["size"] for i = 1, size do