Skip to content

Commit

Permalink
Added command for upload, update and download
Browse files Browse the repository at this point in the history
  • Loading branch information
AtlasPilotPuppy committed Mar 18, 2022
1 parent b638b99 commit 7fde21c
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ url = "https://anchor.projectserum.com"

[provider]
cluster = "localnet"
wallet = "/Users/anant/.config/solana/id.json"
wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
Expand Down
39 changes: 39 additions & 0 deletions app/get_track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env ts-node
import yargs = require('yargs/yargs');
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { utf8 } from "@project-serum/anchor/dist/cjs/utils/bytes";
import { TrackUpload } from "../target/types/track_upload";
import fs from 'fs';
import ipfsAPI from 'ipfs-api';

const argv = yargs(process.argv.slice(2))
.describe({key:"Get the track from Solana and download it."})
.options({
key: { type: 'string', demandOption: true, alias: 'k' },
download: {type: 'boolean', default: true, alias: 'd'}
}).argv;

const main = async() => {
const args = await argv;
anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.TrackUpload as Program<TrackUpload>;
const key = new anchor.web3.PublicKey(args.key);
let trackState = await program.account.track.fetch(key);
console.log(`TRACK: ${trackState.artist}, ${trackState.cid}, ${trackState.trackTitle}`);
if (args.download){
const ipfs = ipfsAPI('ipfs.infura.io', '5001', {protocol: 'https'})
ipfs.files.get(`${trackState.cid}`, function (err, files) {
files.forEach((file) => {
fs.writeFileSync(file.path, file.content);
})
})
}
}

main().then(() => {
console.log("Uploaded track successfully.")
});



77 changes: 77 additions & 0 deletions app/update_track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env ts-node
import yargs = require('yargs/yargs');
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { utf8 } from "@project-serum/anchor/dist/cjs/utils/bytes";
import { TrackUpload } from "../target/types/track_upload";
import fs from 'fs';
import { IPFS, create } from 'ipfs-core';
import type { CID } from 'ipfs-core';
import { string } from 'yargs';
import { isIPFS } from 'ipfs-core';

const argv = yargs(process.argv.slice(2))
.describe({key:"Update solana track entry."})
.options({
key: { type: 'string', demandOption: true, alias: 'k'},
cid: { type: 'string', default: null, alias: 'c' },
path: {type: 'string', default: null, alias: 'p'},
artist: { type: 'string', default: '', alias: 'a' },
title: { type: 'string', default: '', alias: 't' }
}).argv;

const main = async() => {
const args = await argv;
if (!args.cid && !args.path) {
console.error("Either path or cid need to be provided");
process.exit(1)
}
if (args.cid && !isIPFS.cid(args.cid)) {
console.error(`CID ${args.cid} is invalid`);
process.exit(1)
}
anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.TrackUpload as Program<TrackUpload>;
const creator = program.provider.wallet;
const track = anchor.web3.Keypair.generate();

let cid = args.cid? args.cid : "";
if (args.path) {
const node = await create();
const file = fs.readFileSync(args.path);
const file_upload = await node.add({
path: args.path,
content: file.buffer
})
cid = file_upload.cid.toString();
}

let trackState = await program.account.track.fetch(track.publicKey);
if (trackState.signer != creator.publicKey) {
console.error("Only the original creator of the track can update it.");
process.exit(1);
}
const tx = await program.rpc.update(
cid,
args.artist,
args.title,
{
accounts: {
signer: creator.publicKey,
track: track.publicKey,
},
signers: creator instanceof (anchor.Wallet as any) ? [] : [creator]
});
console.log("Your transaction signature", tx);
console.log("Track key", track.publicKey.toString())
let trackUpdated = await program.account.track.fetch(track.publicKey);
console.log(`Track artist: ${trackUpdated.artist}, cid: ${trackUpdated.cid}, title: ${trackUpdated.trackTitle} `)

}

main().then(() => {
console.log("Uploaded track successfully.")
});



69 changes: 69 additions & 0 deletions app/upload_track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env ts-node
import yargs = require('yargs/yargs');
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { utf8 } from "@project-serum/anchor/dist/cjs/utils/bytes";
import { TrackUpload } from "../target/types/track_upload";
import fs from 'fs';
import { IPFS, create, isIPFS } from 'ipfs-core';
import type { CID } from 'ipfs-core';

const argv = yargs(process.argv.slice(2))
.describe({key:"Upload a track to IPFS and persist its id to solana"})
.options({
cid: { type: 'string', default: null, alias: 'c' },
path: {type: 'string', default: null, alias: 'p'},
artist: { type: 'string', default: '', alias: 'a' },
title: { type: 'string', default: '', alias: 't' }
}).argv;

const main = async() => {
const args = await argv;
if (!args.cid && !args.path) {
console.error("Either path or cid need to be provided");
process.exit(1)
}

if (args.cid && !isIPFS.cid(args.cid)) {
console.error(`CID ${args.cid} is invalid`);
process.exit(1)
}
anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.TrackUpload as Program<TrackUpload>;
const creator = program.provider.wallet;
const track = anchor.web3.Keypair.generate();
let cid = args.cid? args.cid : "";
if (args.path) {
const node = await create();
const file = fs.readFileSync(args.path);
const file_upload = await node.add({
path: args.path,
content: file.buffer
})
cid = file_upload.cid.toString();
}
//
const tx = await program.rpc.initialize(
cid,
args.artist,
args.title,
{
accounts: {
creator: creator.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
track: track.publicKey,
},
signers:[track]
});
console.log("Your transaction signature", tx);
console.log("Track key", track.publicKey.toString())
let trackState = await program.account.track.fetch(track.publicKey);
console.log(`TRACK: ${trackState.artist}, ${trackState.cid}, ${trackState.trackTitle} `)
}

main().then(() => {
console.log("Uploaded track successfully.")
});



12 changes: 12 additions & 0 deletions migrations/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Migrations are an early feature. Currently, they're nothing more than this
// single deploy script that's invoked from the CLI, injecting a provider
// configured from the workspace's Anchor.toml.

const anchor = require("@project-serum/anchor");

module.exports = async function (provider) {
// Configure client to use the provider.
anchor.setProvider(provider);

// Add your deploy script here.
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"scripts": {
"upload": "node app/upload_file.ts"
},
"type": "module",
"moduleResolution": "Node",
"dependencies": {
"@project-serum/anchor": "^0.22.1",
"@types/node": "^17.0.21",
"@types/yargs": "^17.0.9",
"ipfs-api": "^26.1.2",
"ipfs-core": "^0.14.1",
"ts-node": "^10.7.0",
"yargs": "^17.3.1"
Expand Down
14 changes: 10 additions & 4 deletions programs/track_upload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
pub mod track_upload {
use super::*;

pub fn initialize(ctx: Context<Initialize>, cid: String, artist: String, title: String) -> Result<()> {
pub fn initialize(ctx: Context<Initialize>, cid: String,
artist: String, title: String) -> Result<()> {
let track = &mut ctx.accounts.track;
track.signer = ctx.accounts.creator.key();
track.artist = artist.as_bytes().to_vec();
track.cid = cid.as_bytes().to_vec();
track.track_title = "TITLE".as_bytes().to_vec();
track.track_title = title.as_bytes().to_vec();
msg!(&title);
Ok(())
}

pub fn update(ctx: Context<UpdateTrack>, cid: String) -> Result<()> {
// Would be prefered to take Option<String> as args but
// running into issues with serialization
pub fn update(ctx: Context<UpdateTrack>, cid: String,
artist: String, title: String) -> Result<()> {
let track = &mut ctx.accounts.track;
require!(ctx.accounts.signer.key() == track.signer, TrackError::UnauthorizedUser);
track.cid = cid.as_bytes().to_vec();
if cid.chars().count() > 0 {track.cid = cid.as_bytes().to_vec()};
if artist.chars().count() > 0 {track.artist = artist.as_bytes().to_vec()};
if title.chars().count() > 0 {track.track_title = title.as_bytes().to_vec()};
Ok(())
}

Expand Down
39 changes: 22 additions & 17 deletions tests/track_upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TrackUpload } from "../target/types/track_upload";
import fs from 'fs';
import { IPFS, create } from 'ipfs-core';
import type { CID } from 'ipfs-core';
import * as assert from "assert";

const program = anchor.workspace.TrackUpload as Program<TrackUpload>;
const creator = program.provider.wallet;
Expand All @@ -15,21 +16,14 @@ describe("track_upload", () => {
anchor.setProvider(anchor.Provider.env());



const cid = "QmPvAuVdiqteJF82w13sjhjqb4YNSBKohmpiv3G9FoBz22";
const artist = "Beatles";
const track_title = "Yellow Submarine";
it("Is initialized!", async () => {
// Add your test here.
const node = await create();
const file = fs.readFileSync("tst.png");
const file_upload = await node.add({
path:"test",
content: file.buffer
})

console.log(`UPLOADED CID: ${file_upload.cid.toString()}`)
const tx = await program.rpc.initialize(
file_upload.cid.toString(),
"BeetLes",
"Yellow Submarinexx",
cid,
artist,
track_title,
{
accounts: {
creator: creator.publicKey,
Expand All @@ -41,20 +35,28 @@ describe("track_upload", () => {
console.log("Your transaction signature", tx);
console.log("Track pub key", track.publicKey)
let trackState = await program.account.track.fetch(track.publicKey);
console.log(`TRACK:", ${String.fromCharCode(...trackState.artist)}, ${String.fromCharCode(...trackState.cid)}, ${trackState.signer} ,_ ${trackState.trackTitle} `)
console.log(`SIGNER: ${track.publicKey}`);
assert.equal(`${trackState.artist}`, artist);
assert.equal(`${trackState.cid}`, cid);
assert.equal(`${trackState.trackTitle}`, track_title);
console.log(`TRACK:", ${trackState.artist}, ${trackState.cid}, ${trackState.trackTitle} `)
console.log(`Track Key: ${track.publicKey}`);
console.log(`SIGNER: ${creator.publicKey}`);
});



});

const new_cid = "QmPvAuVdiqteJF82w13sjhjqb4YNSBKohmpiv3G9FoBz22";
const new_artist = "BEATLES";
const new_title = "Yellow Sub"
describe("track_update", () => {
it("Updates", async () => {
let trackState = await program.account.track.fetch(track.publicKey);
const tx = await program.rpc.update(
"QmPvAuVdiqteJF82w13sjhjqb4YNSBKohmpiv3G9FoBz22",
new_cid,
new_artist,
new_title,
{
accounts: {
track: track.publicKey,
Expand All @@ -64,7 +66,10 @@ describe("track_update", () => {
}
);
let trackState2 = await program.account.track.fetch(track.publicKey);
console.log(`TRACK:", ${String.fromCharCode(...trackState2.artist)}, ${String.fromCharCode(...trackState2.cid)}, ${trackState.signer} ,_ ${trackState2.trackTitle} `)
assert.equal(`${trackState2.artist}`, new_artist);
assert.equal(`${trackState2.cid}`, new_cid);
assert.equal(`${trackState2.trackTitle}`, new_title);
console.log(`TRACK:", ${trackState2.artist}, ${trackState2.cid}, ${trackState2.trackTitle} `)
});

})
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"strict": false,
"esModuleInterop": true
}
}

0 comments on commit 7fde21c

Please sign in to comment.