@quantumcoin/base64 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +33 -0
  2. package/base64.sol +64 -0
  3. package/package.json +37 -0
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # @quantumcoin/base64
2
+
3
+ Base64 encoding library for Solidity 0.7.6 on the QuantumCoin blockchain.
4
+
5
+ # Local Development
6
+
7
+ Requires `node@>=18`. The library is compiled with the
8
+ [`@quantumcoin/solc`](https://www.npmjs.com/package/@quantumcoin/solc) npm package
9
+ (QuantumCoin's Solidity 0.7.6 with 32-byte address support).
10
+
11
+ ## Install Dependencies
12
+
13
+ `npm install`
14
+
15
+ ## Compile
16
+
17
+ `npm run compile`
18
+
19
+ ## Run Tests
20
+
21
+ `npm test`
22
+
23
+ Tests run against a local QuantumCoin devnet using the `quantumcoin` SDK. The devnet is
24
+ downloaded, installed, and started automatically by `scripts/devnet.js` (Windows, macOS, and
25
+ Ubuntu). Overrides: `QC_RPC_URL`, `QC_DEVNET_DIR`, `QC_KEYSTORE`, `QC_KEY_PASSWORD`.
26
+
27
+ ## Usage
28
+
29
+ Install via `npm install @quantumcoin/base64`, then:
30
+
31
+ ```solidity
32
+ import '@quantumcoin/base64/base64.sol';
33
+ ```
package/base64.sol ADDED
@@ -0,0 +1,64 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0;
4
+
5
+ /// @title Base64
6
+ /// @author Brecht Devos - <brecht@loopring.org>
7
+ /// @notice Provides a function for encoding some bytes in base64
8
+ library Base64 {
9
+ string internal constant TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
10
+
11
+ function encode(bytes memory data) internal pure returns (string memory) {
12
+ if (data.length == 0) return '';
13
+
14
+ // load the table into memory
15
+ string memory table = TABLE;
16
+
17
+ // multiply by 4/3 rounded up
18
+ uint256 encodedLen = 4 * ((data.length + 2) / 3);
19
+
20
+ // add some extra buffer at the end required for the writing
21
+ string memory result = new string(encodedLen + 32);
22
+
23
+ assembly {
24
+ // set the actual output length
25
+ mstore(result, encodedLen)
26
+
27
+ // prepare the lookup table
28
+ let tablePtr := add(table, 1)
29
+
30
+ // input ptr
31
+ let dataPtr := data
32
+ let endPtr := add(dataPtr, mload(data))
33
+
34
+ // result ptr, jump over length
35
+ let resultPtr := add(result, 32)
36
+
37
+ // run over the input, 3 bytes at a time
38
+ for {} lt(dataPtr, endPtr) {}
39
+ {
40
+ dataPtr := add(dataPtr, 3)
41
+
42
+ // read 3 bytes
43
+ let input := mload(dataPtr)
44
+
45
+ // write 4 characters
46
+ mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr(18, input), 0x3F)))))
47
+ resultPtr := add(resultPtr, 1)
48
+ mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr(12, input), 0x3F)))))
49
+ resultPtr := add(resultPtr, 1)
50
+ mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr( 6, input), 0x3F)))))
51
+ resultPtr := add(resultPtr, 1)
52
+ mstore(resultPtr, shl(248, mload(add(tablePtr, and( input, 0x3F)))))
53
+ resultPtr := add(resultPtr, 1)
54
+ }
55
+
56
+ // padding with '='
57
+ switch mod(mload(data), 3)
58
+ case 1 { mstore(sub(resultPtr, 2), shl(240, 0x3d3d)) }
59
+ case 2 { mstore(sub(resultPtr, 1), shl(248, 0x3d)) }
60
+ }
61
+
62
+ return result;
63
+ }
64
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@quantumcoin/base64",
3
+ "version": "1.0.0",
4
+ "description": "Base64 encoding library for Solidity contracts on the QuantumCoin blockchain",
5
+ "license": "MIT",
6
+ "files": [
7
+ "base64.sol"
8
+ ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/quantumcoinproject/base64.git"
12
+ },
13
+ "homepage": "https://github.com/quantumcoinproject/base64#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/quantumcoinproject/base64/issues"
16
+ },
17
+ "keywords": [
18
+ "quantumcoin",
19
+ "solidity",
20
+ "base64"
21
+ ],
22
+ "engines": {
23
+ "node": ">=18"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "scripts": {
29
+ "compile": "node scripts/compile.js",
30
+ "test": "npm run compile && node scripts/devnet.js ensure && node --test --test-concurrency=1",
31
+ "prepublishOnly": "npm run compile"
32
+ },
33
+ "devDependencies": {
34
+ "@quantumcoin/solc": "^0.7.6",
35
+ "quantumcoin": "^8.0.2"
36
+ }
37
+ }