-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added command for upload, update and download
- Loading branch information
1 parent
b638b99
commit 7fde21c
Showing
9 changed files
with
233 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
}); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
}); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
}); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"lib": ["es2015"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"strict": false, | ||
"esModuleInterop": true | ||
} | ||
} |