|
|
@ -1,4 +1,3 @@ |
|
|
|
|
|
|
|
function bufferFromBase64(base64) { |
|
|
|
const binaryString = atob(base64); |
|
|
|
const length = binaryString.length; |
|
|
@ -119,7 +118,6 @@ async function accessToken(identifier) { |
|
|
|
issuer: did, |
|
|
|
signer, |
|
|
|
}); |
|
|
|
console.error(jwt); |
|
|
|
return jwt; |
|
|
|
} |
|
|
|
|
|
|
@ -284,7 +282,6 @@ function ES256KSigner(privateKey, recoverable = false) { |
|
|
|
|
|
|
|
return async function (data) { |
|
|
|
const signature = nobleCurves.secp256k1.sign(sha256(data), privateKeyBytes); |
|
|
|
console.error(signature); |
|
|
|
return toJose( |
|
|
|
{ |
|
|
|
r: leftpad(signature.r.toString(16)), |
|
|
@ -387,68 +384,57 @@ async function getSettingById(id) { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
async function setMostRecentNotified(id) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
|
|
const dbName = "TimeSafari"; |
|
|
|
const storeName = "settings"; |
|
|
|
const key = 1; |
|
|
|
const propertyToUpdate = "lastNotifiedClaimId"; |
|
|
|
const newValue = id; |
|
|
|
|
|
|
|
let request = indexedDB.open(dbName); |
|
|
|
|
|
|
|
request.onerror = function(event) { |
|
|
|
console.error("Database error: " + event.target.errorCode); |
|
|
|
}; |
|
|
|
|
|
|
|
request.onsuccess = function(event) { |
|
|
|
let db = event.target.result; |
|
|
|
let transaction = db.transaction(storeName, "readwrite"); |
|
|
|
let store = transaction.objectStore(storeName); |
|
|
|
|
|
|
|
let getRequest = store.get(key); |
|
|
|
|
|
|
|
getRequest.onsuccess = function(event) { |
|
|
|
let data = event.target.result; |
|
|
|
async function setMostRecentNotified(id) { |
|
|
|
try { |
|
|
|
const db = await openIndexedDB("TimeSafari"); |
|
|
|
const transaction = db.transaction("settings", "readwrite"); |
|
|
|
const store = transaction.objectStore("settings"); |
|
|
|
|
|
|
|
// Only update if the object exists
|
|
|
|
const data = await getRecord(store, 1); |
|
|
|
if (data) { |
|
|
|
// Update the specific property
|
|
|
|
data[propertyToUpdate] = newValue; |
|
|
|
|
|
|
|
// Put the updated object back into the database
|
|
|
|
let putRequest = store.put(data); |
|
|
|
|
|
|
|
putRequest.onsuccess = function() { |
|
|
|
console.error("Record updated successfully"); |
|
|
|
}; |
|
|
|
|
|
|
|
putRequest.onerror = function(event) { |
|
|
|
console.error("Error updating record: " + event.target.errorCode); |
|
|
|
}; |
|
|
|
data["lastNotifiedClaimId"] = id; |
|
|
|
await updateRecord(store, data); |
|
|
|
} else { |
|
|
|
console.error("Record not found"); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
getRequest.onerror = function(event) { |
|
|
|
console.error("Error fetching record: " + event.target.errorCode); |
|
|
|
}; |
|
|
|
transaction.oncomplete = () => db.close(); |
|
|
|
} catch (error) { |
|
|
|
console.error("Database error: " + error.message); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
transaction.oncomplete = function() { |
|
|
|
db.close(); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
request.onupgradeneeded = function(event) { |
|
|
|
let db = event.target.result; |
|
|
|
if (!db.objectStoreNames.contains(storeName)) { |
|
|
|
db.createObjectStore(storeName); |
|
|
|
function openIndexedDB(dbName) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
const request = indexedDB.open(dbName); |
|
|
|
request.onerror = () => reject(request.error); |
|
|
|
request.onsuccess = () => resolve(request.result); |
|
|
|
request.onupgradeneeded = (event) => { |
|
|
|
const db = event.target.result; |
|
|
|
if (!db.objectStoreNames.contains("settings")) { |
|
|
|
db.createObjectStore("settings"); |
|
|
|
} |
|
|
|
}; |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getRecord(store, key) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
const request = store.get(key); |
|
|
|
request.onsuccess = () => resolve(request.result); |
|
|
|
request.onerror = () => reject(request.error); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function updateRecord(store, data) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
const request = store.put(data); |
|
|
|
request.onsuccess = () => resolve(request.result); |
|
|
|
request.onerror = () => reject(request.error); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
@ -492,6 +478,10 @@ async function getNotificationCount() { |
|
|
|
secret = self.secret; |
|
|
|
const secretUint8Array = self.decodeBase64(secret); |
|
|
|
const settings = await getSettingById(1); |
|
|
|
let lastNotifiedClaimId = null; |
|
|
|
if ("lastNotifiedClaimId" in settings) { |
|
|
|
lastNotifiedClaimId = settings["lastNotifiedClaimId"]; |
|
|
|
} |
|
|
|
const activeDid = settings["activeDid"]; |
|
|
|
accounts = await fetchAllAccounts(); |
|
|
|
let did = null; |
|
|
@ -510,8 +500,6 @@ async function getNotificationCount() { |
|
|
|
const msg = decoder.decode(decrypted); |
|
|
|
const identifier = JSON.parse(JSON.parse(msg)); |
|
|
|
|
|
|
|
console.log(identifier); |
|
|
|
|
|
|
|
const headers = { |
|
|
|
"Content-Type": "application/json", |
|
|
|
}; |
|
|
@ -525,34 +513,32 @@ async function getNotificationCount() { |
|
|
|
headers: headers, |
|
|
|
}, |
|
|
|
); |
|
|
|
console.error(did, response.status); |
|
|
|
const claims = await response.json(); |
|
|
|
// formulate a message back for notifications
|
|
|
|
let lastNotifiedClaimId = null; |
|
|
|
if ('lastNotifiedClaimId' in settings) { |
|
|
|
lastNotifiedClaimId = settings['lastNotifiedClaimId']; |
|
|
|
} |
|
|
|
if (response.status == 200) { |
|
|
|
let json = await response.json(); |
|
|
|
let claims = json["data"]; |
|
|
|
let newClaims = 0; |
|
|
|
// search for id recent notified -- if it exists if not return everything count
|
|
|
|
for (var i=0; i< response.json()['data'].length; i++) { |
|
|
|
let claim = response.json()['data']; |
|
|
|
if (claim['id'] == lastNotifiedClaimId) { |
|
|
|
for (var i = 0; i < claims.length; i++) { |
|
|
|
let claim = claims[i]; |
|
|
|
if (claim["id"] === lastNotifiedClaimId) { |
|
|
|
break; |
|
|
|
} |
|
|
|
newClaims++; |
|
|
|
} |
|
|
|
// make the notification message here
|
|
|
|
|
|
|
|
// first claim is the most recent (?)
|
|
|
|
const most_recent_notified = claims[0]['id']; |
|
|
|
if (newClaims === 0) { |
|
|
|
result = "You have no new claims today."; |
|
|
|
} else { |
|
|
|
result = `${newClaims} have been shared with you`; |
|
|
|
} |
|
|
|
const most_recent_notified = claims[0]["id"]; |
|
|
|
await setMostRecentNotified(most_recent_notified); |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
return "TEST"; |
|
|
|
} else { |
|
|
|
console.error(response.status); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
self.getNotificationCount = getNotificationCount; |
|
|
|