Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
v2s-datapod
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nodes
v2s-datapod
Commits
bd7c583c
Commit
bd7c583c
authored
1 year ago
by
poka
Browse files
Options
Downloads
Patches
Plain Diff
enh: improve signature errors
parent
0d2a6388
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/signature_verify.ts
+15
-14
15 additions, 14 deletions
lib/signature_verify.ts
lib/update_profile.ts
+8
-7
8 additions, 7 deletions
lib/update_profile.ts
with
23 additions
and
21 deletions
lib/signature_verify.ts
+
15
−
14
View file @
bd7c583c
import
{
signatureVerify
,
base64Decode
}
from
'
https://deno.land/x/polkadot@0.2.44/util-crypto/mod.ts
'
;
export
async
function
verifySignature
(
address
:
string
,
signatureBase64
:
string
,
hash
:
string
,
playload
:
string
):
Promise
<
boolean
>
{
try
{
const
messageUint8Array
=
new
TextEncoder
().
encode
(
h
ash
);
const
signature
=
base64Decode
(
signatureBase64
);
const
signedMessage
=
signatureVerify
(
messageUint8Array
,
signature
,
address
)
export
enum
SignatureResponse
{
valid
,
invalidH
ash
,
invalidSignature
}
export
async
function
verifySignature
(
address
:
string
,
signatureBase64
:
string
,
hash
:
string
,
playload
:
string
):
Promise
<
SignatureResponse
>
{
try
{
const
hashVerify
=
await
createHashedMessage
(
playload
);
// console.log(playload)
// console.log(hash)
// console.log(hashVerify)
if
(
hash
!=
hashVerify
)
{
console
.
log
(
'
hash documents is invalid
'
)
return
false
;
console
.
error
(
'
hash documents is invalid
'
)
return
SignatureResponse
.
invalidHash
;
}
return
signedMessage
.
isValid
;
const
messageUint8Array
=
new
TextEncoder
().
encode
(
hash
);
const
signature
=
base64Decode
(
signatureBase64
);
const
signedMessage
=
signatureVerify
(
messageUint8Array
,
signature
,
address
)
return
signedMessage
.
isValid
?
SignatureResponse
.
valid
:
SignatureResponse
.
invalidSignature
;
}
catch
(
error
)
{
console
.
error
(
'
Signature verification failed:
'
,
error
);
return
false
;
throw
new
Error
(
`Cannot verify signature`
)
;
}
}
...
...
This diff is collapsed.
Click to expand it.
lib/update_profile.ts
+
8
−
7
View file @
bd7c583c
import
{
Context
}
from
"
https://deno.land/x/oak@v12.6.1/context.ts
"
;
import
{
Client
}
from
"
https://deno.land/x/postgres@v0.17.0/client.ts
"
;
import
{
verifySignature
}
from
"
./signature_verify.ts
"
;
import
{
SignatureResponse
,
verifySignature
}
from
"
./signature_verify.ts
"
;
import
{
convertBase64ToBytea
}
from
"
./utils.ts
"
;
export
async
function
updateProfile
(
ctx
:
Context
,
client
:
Client
)
{
...
...
@@ -13,10 +13,11 @@ export async function updateProfile(ctx: Context, client: Client) {
// Verify signature
const
playload
=
JSON
.
stringify
({
description
,
avatarBase64
,
geoloc
,
title
,
city
,
socials
});
if
(
!
await
verifySignature
(
address
,
signature
,
hash
,
playload
))
{
const
signatureResult
=
await
verifySignature
(
address
,
signature
,
hash
,
playload
);
if
(
signatureResult
!=
SignatureResponse
.
valid
)
{
ctx
.
response
.
status
=
401
;
console
.
log
(
'
Invalid signature
'
)
ctx
.
response
.
body
=
{
success
:
false
,
message
:
'
Invalid signature
'
};
console
.
error
(
'
Invalid signature
:
'
+
SignatureResponse
[
signatureResult
]
)
ctx
.
response
.
body
=
{
success
:
false
,
message
:
'
Invalid signature
:
'
+
SignatureResponse
[
signatureResult
]
};
return
;
}
console
.
log
(
'
Signature is valid
'
)
...
...
@@ -43,7 +44,7 @@ export async function updateProfile(ctx: Context, client: Client) {
text
:
query
,
args
:
[
address
,
description
,
avatarBytea
,
geoloc
?
geoloc
[
"
latitude
"
]
:
null
,
geoloc
?
geoloc
[
"
longitude
"
]
:
null
,
title
,
city
,
socialJson
],
});
console
.
log
(
'
User updated successfully
'
);
console
.
log
(
`Profile
${
address
}
has been updated`
);
}
catch
(
error
)
{
throw
error
;
}
...
...
@@ -52,10 +53,10 @@ export async function updateProfile(ctx: Context, client: Client) {
ctx
.
response
.
status
=
200
;
ctx
.
response
.
body
=
{
success
:
true
,
message
:
"
Profile has been updated
"
message
:
`
Profile
${
address
}
has been updated
`
};
}
catch
(
error
)
{
console
.
error
(
'
Error updating
user
:
'
,
error
);
console
.
error
(
'
Error updating
profile
:
'
,
error
);
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
{
success
:
false
,
message
:
'
Error updating user
'
};
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment