Skip to content

Commit

Permalink
Feat/preview api (#496)
Browse files Browse the repository at this point in the history
* feat: Preview Api

* feat: Preview Api
  • Loading branch information
rrr523 authored Mar 12, 2024
1 parent 98ee8e5 commit 007a4ba
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-dragons-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: ed25519 support preview api
3 changes: 2 additions & 1 deletion examples/nextjs/src/components/object/info/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { client, selectSp } from '@/client';
import { ACCOUNT_PRIVATEKEY } from '@/config/env';
import { getOffchainAuthKeys } from '@/utils/offchainAuth';
import { useState } from 'react';
import { useAccount } from 'wagmi';
Expand Down Expand Up @@ -96,7 +97,7 @@ export const ObjectInfo = () => {
view: '1',
'X-Gnfd-User-Address': address,
'X-Gnfd-App-Domain': window.location.origin,
'X-Gnfd-Expiry-Timestamp': '2023-09-03T09%3A23%3A39Z',
'X-Gnfd-Expiry-Timestamp': '2024-03-12T09:39:22Z',
},
},
{
Expand Down
22 changes: 20 additions & 2 deletions packages/js-sdk/src/api/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
MsgUpdateObjectInfo,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/tx';
import { bytesFromBase64 } from '@bnb-chain/greenfield-cosmos-types/helpers';
import { hexlify } from '@ethersproject/bytes';
import { ed25519 } from '@noble/curves/ed25519';
import { Headers } from 'cross-fetch';
import { bytesToUtf8, hexToBytes } from 'ethereum-cryptography/utils';
import { container, delay, inject, injectable } from 'tsyringe';
Expand All @@ -37,7 +39,12 @@ import {
newObjectGRN,
} from '..';
import { RpcQueryClient } from '../clients/queryclient';
import { encodePath, getAuthorization, getSortQuery } from '../clients/spclient/auth';
import {
encodePath,
getAuthorization,
getSortQuery,
HTTPHeaderRegPubKey,
} from '../clients/spclient/auth';
import { getApprovalMetaInfo } from '../clients/spclient/spApis/approval';
import { getGetObjectMetaInfo } from '../clients/spclient/spApis/getObject';
import {
Expand Down Expand Up @@ -461,6 +468,9 @@ export class Objects implements IObject {

public async getObjectPreviewUrl(params: GetPrivewObject, authType: AuthType) {
assertAuthType(authType);
if (authType.type === 'ECDSA') {
throw new Error('Get object preview url only support EDDSA');
}
const { bucketName, objectName, queryMap } = params;
verifyBucketName(bucketName);
verifyObjectName(objectName);
Expand All @@ -472,7 +482,15 @@ export class Objects implements IObject {
const path = '/' + encodePath(objectName);
const url = generateUrlByBucketName(endpoint, bucketName) + path;

const queryRaw = getSortQuery(queryMap);
let pubKey = '';
if (authType.type === 'EDDSA') {
pubKey = hexlify(ed25519.getPublicKey(authType.seed.slice(2)));
}

const queryRaw = getSortQuery({
...queryMap,
[HTTPHeaderRegPubKey]: pubKey.slice(2),
});

const canonicalRequest = [
METHOD_GET,
Expand Down
8 changes: 4 additions & 4 deletions packages/js-sdk/src/api/offchainauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class OffChainAuth implements IOffChainAuth {
const signRes = await personalSign({ message: signMsg, address, provider });
const jsonSignMsg = JSON.stringify(signMsg).replace(/\"/g, '');
const authorization = `GNFD1-ETH-PERSONAL_SIGN,SignedMsg=${jsonSignMsg},Signature=${signRes}`;
// 4. upload signature and pubKey to server

const res = await updateSpsPubKey({
address,
sps,
Expand All @@ -70,9 +70,9 @@ export class OffChainAuth implements IOffChainAuth {
code: 0,
body: {
seedString: hexlify(privateKey),
keypars: {
privateKey: hexlify(privateKey),
publicKey: hexlify(publicKey),
keypairs: {
privateKey: hexlify(privateKey).slice(2),
publicKey: hexlify(publicKey).slice(2),
},
expirationTime,
spAddresses: successSps,
Expand Down
7 changes: 5 additions & 2 deletions packages/js-sdk/src/types/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export interface IGenOffChainAuthKeyPairAndUpload extends IBaseUser {

export interface IReturnOffChainAuthKeyPairAndUpload {
/**
* compatibility: private key
* compatibility for old version: private key
*/
seedString: string;
keypars: {
/**
* public key and private key without prefix `0x`
*/
keypairs: {
privateKey: string;
publicKey: string;
};
Expand Down

0 comments on commit 007a4ba

Please sign in to comment.