|
@@ -3,43 +3,62 @@ import { u8aToHex } from '@polkadot/util'
|
|
import { signatureVerify } from '@polkadot/util-crypto'
|
|
import { signatureVerify } from '@polkadot/util-crypto'
|
|
import base64url from 'base64url'
|
|
import base64url from 'base64url'
|
|
|
|
|
|
-export interface TokenRequest {
|
|
|
|
- dataObjectId: number
|
|
|
|
- storageBucketId: number
|
|
|
|
- bagId: string
|
|
|
|
|
|
+export interface UploadTokenRequest {
|
|
|
|
+ data: UploadTokenRequestBody
|
|
|
|
+ signature: string
|
|
}
|
|
}
|
|
|
|
|
|
-export interface TokenBody {
|
|
|
|
|
|
+export interface UploadTokenRequestBody extends RequestData {
|
|
|
|
+ memberId: number
|
|
|
|
+ accountId: string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export interface RequestData {
|
|
dataObjectId: number
|
|
dataObjectId: number
|
|
storageBucketId: number
|
|
storageBucketId: number
|
|
bagId: string
|
|
bagId: string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export interface UploadTokenBody extends RequestData {
|
|
timestamp: number
|
|
timestamp: number
|
|
}
|
|
}
|
|
|
|
|
|
-export interface Token {
|
|
|
|
- data: TokenBody
|
|
|
|
|
|
+export interface UploadToken {
|
|
|
|
+ data: UploadTokenBody
|
|
signature: string
|
|
signature: string
|
|
}
|
|
}
|
|
|
|
|
|
-export function parseToken(tokenString: string): Token {
|
|
|
|
|
|
+export function parseUploadToken(tokenString: string): UploadToken {
|
|
return JSON.parse(base64url.decode(tokenString))
|
|
return JSON.parse(base64url.decode(tokenString))
|
|
}
|
|
}
|
|
|
|
|
|
export function verifyTokenSignature(
|
|
export function verifyTokenSignature(
|
|
- token: Token,
|
|
|
|
- account: KeyringPair
|
|
|
|
|
|
+ token: UploadToken | UploadTokenRequest,
|
|
|
|
+ address: string
|
|
): boolean {
|
|
): boolean {
|
|
const message = JSON.stringify(token.data)
|
|
const message = JSON.stringify(token.data)
|
|
- const { isValid } = signatureVerify(message, token.signature, account.address)
|
|
|
|
|
|
+ const { isValid } = signatureVerify(message, token.signature, address)
|
|
|
|
|
|
return isValid
|
|
return isValid
|
|
}
|
|
}
|
|
|
|
|
|
-export function signToken(tokenBody: TokenBody, account: KeyringPair): string {
|
|
|
|
|
|
+export function signTokenBody(
|
|
|
|
+ tokenBody: UploadTokenBody | UploadTokenRequestBody,
|
|
|
|
+ account: KeyringPair
|
|
|
|
+): string {
|
|
const message = JSON.stringify(tokenBody)
|
|
const message = JSON.stringify(tokenBody)
|
|
const signature = u8aToHex(account.sign(message))
|
|
const signature = u8aToHex(account.sign(message))
|
|
|
|
|
|
- const token: Token = {
|
|
|
|
|
|
+ return signature
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function createUploadToken(
|
|
|
|
+ tokenBody: UploadTokenBody,
|
|
|
|
+ account: KeyringPair
|
|
|
|
+): string {
|
|
|
|
+ const signature = signTokenBody(tokenBody, account)
|
|
|
|
+
|
|
|
|
+ const token = {
|
|
data: tokenBody,
|
|
data: tokenBody,
|
|
signature,
|
|
signature,
|
|
}
|
|
}
|
|
@@ -48,16 +67,19 @@ export function signToken(tokenBody: TokenBody, account: KeyringPair): string {
|
|
}
|
|
}
|
|
|
|
|
|
// Throws exceptions on errors.
|
|
// Throws exceptions on errors.
|
|
-export function verifyTokenData(token: Token, data: TokenRequest): void {
|
|
|
|
- if (token.data.dataObjectId !== data.dataObjectId) {
|
|
|
|
|
|
+export function verifyUploadTokenData(
|
|
|
|
+ token: UploadToken,
|
|
|
|
+ request: RequestData
|
|
|
|
+): void {
|
|
|
|
+ if (token.data.dataObjectId !== request.dataObjectId) {
|
|
throw new Error('Unexpected dataObjectId')
|
|
throw new Error('Unexpected dataObjectId')
|
|
}
|
|
}
|
|
|
|
|
|
- if (token.data.storageBucketId !== data.storageBucketId) {
|
|
|
|
|
|
+ if (token.data.storageBucketId !== request.storageBucketId) {
|
|
throw new Error('Unexpected storageBucketId')
|
|
throw new Error('Unexpected storageBucketId')
|
|
}
|
|
}
|
|
|
|
|
|
- if (token.data.bagId !== data.bagId) {
|
|
|
|
|
|
+ if (token.data.bagId !== request.bagId) {
|
|
throw new Error('Unexpected bagId')
|
|
throw new Error('Unexpected bagId')
|
|
}
|
|
}
|
|
}
|
|
}
|