Skip to content

Commit

Permalink
feat(kyve): allow kyve token type for uploads and top ups PE-6447
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Aug 21, 2024
1 parent f46daa0 commit 861d542
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type Currency =
| 'brl';
export type Country = 'United States' | 'United Kingdom' | 'Canada'; // TODO: add full list

export const tokenTypes = ['arweave', 'solana', 'ethereum'] as const;
export const tokenTypes = ['arweave', 'solana', 'ethereum', 'kyve'] as const;
export type TokenType = (typeof tokenTypes)[number];

export type Adjustment = {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Secp256k1HdWallet, makeCosmoshubPath } from '@cosmjs/amino';
import { Slip10, Slip10Curve } from '@cosmjs/crypto';
import { toHex } from '@cosmjs/encoding';
import { ArweaveSigner, EthereumSigner, HexSolanaSigner } from 'arbundles';

import {
Expand Down Expand Up @@ -66,3 +69,21 @@ export function createTurboSigner({
return new ArweaveSigner(clientProvidedPrivateKey);
}
}

export async function signerFromKyveMnemonic(
mnemonic: string,
): Promise<TurboSigner> {
const kyveWallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: 'kyve',
});

const privateKey = toHex(
Slip10.derivePath(
Slip10Curve.Secp256k1,
kyveWallet['seed'],
makeCosmoshubPath(0),
).privkey,
);

return new EthereumSigner(privateKey);
}
59 changes: 59 additions & 0 deletions tests/turbo.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import KyveSDK from '@kyvejs/sdk';
import {
ArweaveSigner,
EthereumSigner,
Expand Down Expand Up @@ -28,6 +29,8 @@ import {
} from '../src/common/turbo.js';
import { TurboFactory } from '../src/node/factory.js';
import { TurboNodeSigner } from '../src/node/signer.js';
import { TurboSigner } from '../src/types.js';
import { signerFromKyveMnemonic } from '../src/utils/common.js';
import { FailedRequestError } from '../src/utils/errors.js';
import {
delayedBlockMining,
Expand Down Expand Up @@ -921,4 +924,60 @@ describe('Node environment', () => {
});
});
});

describe('TurboAuthenticatedNodeClient with KyveSigner', () => {
let turbo: TurboAuthenticatedClient;

// TODO: KYVE Gateway
// const tokenTools = new KyveToken({
// gatewayUrl: kyveUrlString,
// pollingOptions: {
// maxAttempts: 3,
// pollingIntervalMs: 10,
// initialBackoffMs: 0,
// },
// });

let signer: TurboSigner; // KyveSigner
before(async () => {
signer = await signerFromKyveMnemonic(await KyveSDK.generateMnemonic());

Check failure on line 943 in tests/turbo.node.test.ts

View workflow job for this annotation

GitHub Actions / test

Property 'generateMnemonic' does not exist on type 'typeof import("/home/runner/work/turbo-sdk/turbo-sdk/node_modules/@kyvejs/sdk/dist/index")'.
turbo = TurboFactory.authenticated({
signer,
...turboDevelopmentConfigurations,
// tokenTools,
});
});

it('should properly upload a Readable to turbo', async () => {
const filePath = new URL('files/1KB_file', import.meta.url).pathname;
const fileSize = fs.statSync(filePath).size;
const response = await turbo.uploadFile({
fileStreamFactory: () => fs.createReadStream(filePath),
fileSizeFactory: () => fileSize,
});
expect(response).to.not.be.undefined;
expect(response).to.not.be.undefined;
expect(response).to.have.property('fastFinalityIndexes');
expect(response).to.have.property('dataCaches');
expect(response).to.have.property('owner');
expect(response['owner']).to.equal(testSolAddressBase64);
});

it('should properly upload a Buffer to turbo', async () => {
const signedDataItem = createData('signed data item', signer, {});
await signedDataItem.sign(signer);

const response = await turbo.uploadSignedDataItem({
dataItemStreamFactory: () => signedDataItem.getRaw(),
dataItemSizeFactory: () => signedDataItem.getRaw().length,
});

expect(response).to.not.be.undefined;
expect(response).to.not.be.undefined;
expect(response).to.have.property('fastFinalityIndexes');
expect(response).to.have.property('dataCaches');
expect(response).to.have.property('owner');
expect(response['owner']).to.equal(testSolAddressBase64);
});
});
});

0 comments on commit 861d542

Please sign in to comment.