Skip to content

Commit 55b80bb

Browse files
committed
build: update fcm script to work with HTTP API v1 and add a script to emulate a buy
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent e50cea7 commit 55b80bb

2 files changed

Lines changed: 73 additions & 16 deletions

File tree

scripts/fcm-buy-token.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3+
4+
DEVICE_TOKEN=$1
5+
MINT=$2
6+
7+
encode_payload() {
8+
local mint="$1"
9+
python3 -c "
10+
import base64, base58
11+
def varint(v):
12+
r = b''
13+
while v > 0x7F:
14+
r += bytes([(v & 0x7F) | 0x80]); v >>= 7
15+
return r + bytes([v & 0x7F])
16+
def field(num, data):
17+
return varint((num << 3) | 2) + varint(len(data)) + data
18+
mint = base58.b58decode('$mint')
19+
assert len(mint) == 32, f'Expected 32-byte key, got {len(mint)}'
20+
print(base64.b64encode(field(1, field(1, field(1, mint)))).decode())
21+
"
22+
}
23+
24+
PAYLOAD=$(encode_payload "$MINT")
25+
26+
JSON_PAYLOAD=$(jq -n \
27+
--arg payload "$PAYLOAD" \
28+
--arg title "Purchase Successful" \
29+
--arg body "You received 100.0 of some token" \
30+
'{
31+
flipcash_payload: $payload,
32+
push_notification_title: $title,
33+
push_notification_body: $body
34+
}')
35+
36+
"$SCRIPT_DIR/fcm.sh" "$DEVICE_TOKEN" "$JSON_PAYLOAD"

scripts/fcm.sh

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
#!/bin/bash
22

3-
key="$1"
4-
token="$2"
3+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4+
5+
source "$SCRIPT_DIR/../.env.local"
56

6-
# Access the JSON object passed as the first argument
7-
contents="$3"
8-
9-
data=$(cat <<-END
10-
{
11-
"registration_ids":["${token}"],
12-
"data": $contents
13-
}
14-
END
7+
ACCESS_TOKEN=$(python3 << EOF
8+
from google.oauth2 import service_account
9+
from google.auth.transport.requests import Request
10+
11+
credentials = service_account.Credentials.from_service_account_file(
12+
"$SERVER_SERVICE_ACCOUNT_KEY_JSON",
13+
scopes=["https://www.googleapis.com/auth/firebase.messaging"]
1514
)
15+
credentials.refresh(Request())
16+
print(credentials.token)
17+
EOF
18+
)
19+
20+
21+
DEVICE_TOKEN=$1
22+
23+
# Access the JSON object passed as the first argument
24+
contents="$2"
1625

17-
echo $data
26+
JSON_PAYLOAD=$(jq -n \
27+
--arg token "$DEVICE_TOKEN" \
28+
--argjson data "$contents" \
29+
'{
30+
message: {
31+
token: $token,
32+
android: {
33+
priority: "high",
34+
data: $data
35+
}
36+
}
37+
}')
1838

19-
curl -i -H 'Content-type: application/json' \
20-
-H "Authorization: key=$key" \
21-
-XPOST https://fcm.googleapis.com/fcm/send \
22-
-d "$data"
39+
curl -X POST \
40+
"https://fcm.googleapis.com/v1/projects/${FCM_PROJECT_ID}/messages:send" \
41+
-H "Authorization: Bearer $ACCESS_TOKEN" \
42+
-H "Content-Type: application/json" \
43+
-d "$JSON_PAYLOAD"

0 commit comments

Comments
 (0)