-
Notifications
You must be signed in to change notification settings - Fork 7
/
contract.js
129 lines (120 loc) · 4.54 KB
/
contract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const icon_svg_base64 = 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA5IDkiPgogICAgPHJlY3QgeT0iMCIgd2lkdGg9IjkiIGhlaWdodD0iMyIgZmlsbD0iIzBiZiIvPgogICAgPHJlY3QgeT0iMyIgd2lkdGg9IjYiIGhlaWdodD0iMyIgZmlsbD0iI2Y4MiIvPgogICAgPHJlY3QgeD0iNiIgeT0iMyIgd2lkdGg9IjMiIGhlaWdodD0iMyIgZmlsbD0iIzMzMyIgLz4KICAgIDxyZWN0IHk9IjYiIHdpZHRoPSIzIiBoZWlnaHQ9IjMiIGZpbGw9IiMyYWEiLz4KICAgIDxyZWN0IHg9IjMiIHk9IjYiIHdpZHRoPSI2IiBoZWlnaHQ9IjMiIGZpbGw9IiM2NjYiIC8+Cjwvc3ZnPg==';
export function web4_get() {
const request = JSON.parse(env.input()).request;
let response;
if (request.path == '/serviceworker.js') {
response = {
contentType: "application/javascript; charset=UTF-8",
body: env.get_content_base64(request.path)
};
} else if (request.path.indexOf('/musicwasms/') == 0) {
response = {
contentType: "application/wasm",
body: env.get_content_base64(request.path)
};
} else if (request.path == '/webassemblymusicsources.zip') {
if (env.nft_supply_for_owner(request.query.account_id[0]) > 0) {
const validSignature = env.verify_signed_message(
request.query.message[0],
request.query.signature[0],
request.query.account_id[0]
);
if (validSignature) {
response = {
contentType: "application/zip",
body: env.get_content_base64(request.path)
};
} else {
response = {
contentType: "text/plain",
body: env.base64_encode("INVALID SIGNATURE"),
};
}
} else {
response = {
contentType: "text/plain",
body: env.base64_encode("NOT OWNER"),
};
}
} else if (request.path == '/icon.svg' || request.path == '/assets/icon.svg') {
response = {
contentType: "image/svg+xml",
body: icon_svg_base64
};
} else if (request.path == '/nftowners.json') {
const tokens = JSON.parse(env.nft_tokens(0, 100));
response = {
contentType: "application/json; charset=UTF-8",
body: env.base64_encode(JSON.stringify(tokens.map(t => ({ token_id: t.token_id, owner_id: t.owner_id }))))
};
} else if (request.path.endsWith('.html')) {
response = {
contentType: "text/html; charset=UTF-8",
body: env.get_content_base64(request.path)
};
} else {
response = {
contentType: "text/html; charset=UTF-8",
body: env.get_content_base64('/index.html')
};
}
env.value_return(JSON.stringify(response));
}
export function store_signing_key() {
if (env.nft_supply_for_owner(env.signer_account_id()) > 0) {
env.store_signing_key(env.block_timestamp_ms() + 24 * 60 * 60 * 1000);
}
}
export function nft_metadata() {
return {
name: "WebAssembly Music by Peter Salomonsen",
symbol: "PSMUSIC",
icon: `data:image/svg+xml;base64,${icon_svg_base64}`,
base_uri: null,
reference: null,
reference_hash: null,
};
}
export function nft_mint() {
if (env.signer_account_id() != env.current_account_id()) {
env.panic('only contract account can mint');
}
const args = JSON.parse(env.input());
const svgstring = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 9 9">
<rect y="0" width="9" height="3" fill="#0bf"/>
<rect y="3" width="6" height="3" fill="#f82"/>
<rect x="6" y="3" width="3" height="3" fill="#333" />
<rect y="6" width="3" height="3" fill="#2aa"/>
<rect x="3" y="6" width="6" height="3" fill="#666" />
<text x="4.5" y="5.5" text-anchor="middle" font-size="3"
font-family="system-ui" fill="white">
${args.token_id}
</text>
</svg>`;
return JSON.stringify({
title: `WebAssembly Music token number #${args.token_id}`,
description: `An album by Peter Salomonsen with the first generation of tunes made in the browser using the "WebAssembly Music" web application. webassemblymusic.near.page`,
media: `data:image/svg+xml;base64,${env.base64_encode(svgstring)}`,
media_hash: env.sha256_utf8_to_base64(svgstring)
});
}
/**
* @returns
*/
export function nft_payout() {
const args = JSON.parse(env.input());
const balance = BigInt(args.balance);
const payout = {};
const token_owner_id = JSON.parse(env.nft_token(args.token_id)).owner_id;
const contract_owner = env.contract_owner();
const addPayout = (account, amount) => {
if (!payout[account]) {
payout[account] = 0n;
}
payout[account] += amount;
};
addPayout(token_owner_id, balance * BigInt(80_00) / BigInt(100_00));
addPayout(contract_owner, balance * BigInt(20_00) / BigInt(100_00));
Object.keys(payout).forEach(k => payout[k] = payout[k].toString());
return JSON.stringify({ payout });
}