-
Notifications
You must be signed in to change notification settings - Fork 15
Added address Verification #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
cf5561b
8fe3bf5
6173f04
053964d
072ae8f
33303ca
b457038
d209c8c
6e686e6
f78a184
83282d4
19bb462
da1dc9b
8d3f6be
ed9b2a1
271a125
2c9bb35
a619850
f6e276c
2cc72b2
f39848c
daebc6a
3e68824
f613fcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Ignore artifacts: | ||
dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/*! | ||
* The buffer module from node.js, for the browser. | ||
* | ||
* @author Feross Aboukhadijeh <https://feross.org> | ||
* @license MIT | ||
*/ | ||
|
||
/*! https://mths.be/base64 v1.0.0 by @mathias | MIT license */ | ||
|
||
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
type COSESign1 = { | ||
signature: string; | ||
key: string; | ||
} | ||
|
||
type Signer = (msg: string) => PromiseLike<COSESign1>; | ||
|
||
export function sign( | ||
signer: Signer, | ||
expires_in?: string | number, | ||
body?: Object | ||
): PromiseLike<string>; | ||
|
||
export function verify(token: string): { | ||
address: string; | ||
body: Object; | ||
}; | ||
|
||
declare const Web3Token: { | ||
sign: typeof sign; | ||
verify: typeof verify; | ||
}; | ||
|
||
export default Web3Token; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Types ❤️ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
type Signer = (msg: string) => PromiseLike<string> | ||
type COSESign1 = { | ||
signature: string; | ||
key: string; | ||
} | ||
|
||
type Signer = (msg: string) => PromiseLike<COSESign1>; | ||
|
||
export function sign( | ||
signer: Signer, | ||
expires_in?: string | number, | ||
body?: Object): PromiseLike<string> | ||
signer: Signer, | ||
expires_in?: string | number, | ||
body?: Object | ||
): PromiseLike<string>; | ||
|
||
export function verify( | ||
token: string | ||
): { | ||
address: string, | ||
body: Object | ||
} | ||
export function verify(token: string): { | ||
address: string; | ||
body: Object; | ||
}; | ||
|
||
declare const Web3Token: { | ||
sign: typeof sign, | ||
verify: typeof verify | ||
} | ||
sign: typeof sign; | ||
verify: typeof verify; | ||
}; | ||
|
||
export default Web3Token | ||
export default Web3Token; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,80 @@ | ||
import Base64 from 'base-64'; | ||
import { timeSpan } from './timespan'; | ||
import Base64 from "base-64"; | ||
import { timeSpan } from "./timespan"; | ||
|
||
/** | ||
* | ||
* | ||
* @param {function} signer - The signer function, must return Promise<string> | ||
* @param {any} body - Body to add to the sign | ||
*/ | ||
export const sign = async (signer, expires_in = '1d', body = {}) => { | ||
|
||
export const sign = async (signer, expires_in = "1d", body = {}) => { | ||
const expires_in_date = timeSpan(expires_in); | ||
|
||
validateInput(body); | ||
|
||
const data = { | ||
'Web3-Token-Version': 1, | ||
'Expire-Date': expires_in_date, | ||
"Web3-Token-Version": 1, | ||
"Expire-Date": expires_in_date, | ||
...body, | ||
}; | ||
|
||
const msg = buildMessage(data); | ||
if(typeof signer === 'function') { | ||
var signature = await signer(msg); | ||
|
||
if (typeof signer === "function") { | ||
var COSESign1Message = await signer(msg); | ||
} else { | ||
throw new Error('"signer" argument should be a function that returns a signature eg: "msg => web3.eth.personal.sign(msg, <YOUR_ADDRESS>)"') | ||
throw new Error( | ||
'"signer" argument should be a function that returns a signature eg: "msg => web3.eth.personal.sign(msg, <YOUR_ADDRESS>)"' | ||
); | ||
} | ||
|
||
if (typeof(signature) === "object") { | ||
signature = signature.signature | ||
|
||
let signature, key; | ||
|
||
if (typeof COSESign1Message === "string") { | ||
signature = COSESign1Message; | ||
} else { | ||
signature = COSESign1Message.signature; | ||
key = COSESign1Message.key; | ||
} | ||
|
||
if(typeof signature !== 'string') { | ||
throw new Error('"signer" argument should be a function that returns a signature string (Promise<string>)') | ||
if (typeof signature !== "string") { | ||
throw new Error( | ||
'"signature" argument should be a function that returns a signature string (Promise<string>)' | ||
); | ||
} | ||
|
||
const token = Base64.encode(JSON.stringify({ | ||
signature, | ||
body: msg, | ||
})) | ||
const token = Base64.encode( | ||
JSON.stringify({ | ||
signature, | ||
key, | ||
body: msg, | ||
}) | ||
); | ||
|
||
return token; | ||
} | ||
|
||
}; | ||
|
||
const validateInput = body => { | ||
const validateInput = (body) => { | ||
for (const key in body) { | ||
const field = body[key]; | ||
|
||
const field = body[key] | ||
|
||
if(key === 'Expire-Date') { | ||
if (key === "Expire-Date") { | ||
throw new Error('Please do not rewrite "Expire-Date" field'); | ||
} | ||
|
||
if(key === 'Web3-Token-Version') { | ||
if (key === "Web3-Token-Version") { | ||
throw new Error('Please do not rewrite "Web3-Token-Version" field'); | ||
} | ||
|
||
if(typeof field !== 'string') { | ||
throw new Error('Body can only contain string values'); | ||
if (typeof field !== "string") { | ||
throw new Error("Body can only contain string values"); | ||
} | ||
} | ||
}; | ||
|
||
const buildMessage = data => { | ||
const buildMessage = (data) => { | ||
const message = []; | ||
for (const key in data) { | ||
message.push(`${key}: ${data[key]}`) | ||
message.push(`${key}: ${data[key]}`); | ||
} | ||
return message.join('\n'); | ||
return message.join("\n"); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we stick with arrow function types? Also we should make first letter of the function uppercase ⏫