├── .gitignore ├── LICENSE ├── README.md ├── dist ├── format_tbl.js └── index.js ├── examples └── basic_usage.js ├── package.json └── src ├── format_tbl.js └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SDK for Aptos Passport (APTPP) 2 | 3 | ## Install 4 | ``` 5 | npm install aptpp-js 6 | ``` 7 | or 8 | ``` 9 | yarn add aptpp-js 10 | ``` 11 | 12 | ## Usage 13 | 14 | ``` 15 | const { AptDomain } = require('aptpp-js'); 16 | 17 | // opts format: 18 | // network : string // [optional] Set network 'mainnet' / 'testnet' / 'devnet', default: 'mainnet' 19 | // nodeUrl : string // [optional] Node Url, for example 'https://fullnode.mainnet.aptoslabs.com' 20 | // contractAddress : string // [optional] The Mainnet Contract address is '0x0000777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb' 21 | // The Testnet or Devnet Contract address is '0x7ce77452da809fbc4ef32596cf2be18ec6f252e1884b4eefa4d4349c2941923e' 22 | // aptosClientConfig : object // [optional] Aptos client config 23 | 24 | let opts={network:"mainnet"}; 25 | let aptdomain=new AptDomain(opts); 26 | ``` 27 | 28 | ### Basic 29 | 30 | You can using in Promise: 31 | ``` 32 | const test_domain = 'test007@apt'; 33 | const test_address = '0x5e0c91adbe365dca24d1565b434b04c79e17823a1c6db299ba291fc7e86325e6'; 34 | 35 | let { address } = await aptdomain.lookup( test_domain); 36 | console.log( `${test_domain} => ${address}` ); 37 | 38 | let { domain } = await aptdomain.reverse( test_address ); 39 | console.log( `${test_address} => ${domain}` ); 40 | 41 | let record = await aptdomain.getDomainRecord(test_domain); 42 | console.log( `${test_domain} => ${JSON.stringify(record,null,' ')}` ); 43 | 44 | ``` 45 | 46 | Or you can using at Callback: 47 | ``` 48 | aptdomain.lookup(test_domain,(status,address)=>{ 49 | console.log( `${test_domain} => ${address}` ); 50 | }); 51 | 52 | aptdomain.reverse(test_address,(status,domain)=>{ 53 | console.log( `${test_address} => ${domain}` ); 54 | }); 55 | 56 | aptdomain.getDomainRecord(test_domain,(status,record)=>{ 57 | console.log( `${test_domain} => ${JSON.stringify(record,null,' ')}` ); 58 | }); 59 | ``` 60 | 61 | ### Domain Object 62 | 63 | Create a object of domain, get more formated data. 64 | 65 | Here is the sample: 66 | ``` 67 | // request a domain object by addres or domain 68 | // if domain not exist , return null 69 | let domainObj = await aptdomain.getDomainObj('test007@apt'); 70 | 71 | if( domainObj ){ 72 | console.log("domainObj.address(): ", domainObj.address()); 73 | console.log("domainObj.avatar(): ", domainObj.avatar()); 74 | console.log("domainObj.url(): ", domainObj.url()); 75 | console.log("domainObj.email(): ", domainObj.email()); 76 | 77 | console.log("domainObj.discord(): ", domainObj.discord()); 78 | console.log("domainObj.github(): ", domainObj.github()); 79 | console.log("domainObj.reddit(): ", domainObj.reddit()); 80 | console.log("domainObj.twitter(): ", domainObj.twitter()); 81 | console.log("domainObj.telegram(): ", domainObj.telegram()); 82 | 83 | console.log("domainObj.record('APT'): ", domainObj.record('APT')); 84 | console.log("domainObj.record('ETH'): ", domainObj.record('ETH')); 85 | console.log("domainObj.record('BTC'): ", domainObj.record('BTC')); 86 | console.log("domainObj.record('Solana'): ", domainObj.record('Solana')); 87 | } 88 | ``` 89 | 90 | Also you can check the examples to quickstart. 91 | 92 | ## Requirement 93 | - [Aptos SDK](https://github.com/aptos-labs/aptos-core/tree/main/ecosystem/typescript/sdk) 94 | 95 | ## More infomation 96 | - [Aptos Passport](https://aptpp.com) 97 | 98 | -------------------------------------------------------------------------------- /dist/format_tbl.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const GroupList = ['address', 'dns', 'txt', 'sns']; 4 | 5 | const FieldFormatTable = { 6 | 7 | 1: { 8 | group: 'address', 9 | name: 'APT', 10 | type: 'string' 11 | }, 12 | 13 | ////////////////////////////////// 14 | 400: { 15 | group: 'dns', 16 | name: 'A Record', 17 | type: 'string' 18 | }, 19 | 401: { 20 | group: 'dns', 21 | name: 'CNAME Record', 22 | type: 'string' 23 | }, 24 | 402: { 25 | group: 'dns', 26 | name: 'TXT Record', 27 | type: 'string' 28 | }, 29 | 403: { 30 | group: 'dns', 31 | name: 'IPFS CID', 32 | type: 'string' 33 | }, 34 | ////////////////////////////////// 35 | 36 | 100: { 37 | group: 'address', 38 | name: 'ETH', 39 | type: 'string' 40 | }, 41 | 101: { 42 | group: 'address', 43 | name: 'BTC', 44 | type: 'string' 45 | }, 46 | 102: { 47 | group: 'address', 48 | name: 'BSC', 49 | type: 'string' 50 | }, 51 | 103: { 52 | group: 'address', 53 | name: 'Solana', 54 | type: 'string' 55 | }, 56 | /*104:{ 57 | group:'address', 58 | name:'Avalanche', 59 | type:'string' 60 | }, 61 | 105:{ 62 | group:'address', 63 | name:'TRON', 64 | type:'string' 65 | }, 66 | 106:{ 67 | group:'address', 68 | name:'Polygon', 69 | type:'string' 70 | },*/ 71 | 72 | ////////////////////////////////// 73 | 74 | 201: { 75 | group: 'txt', 76 | name: 'email', 77 | type: 'email' 78 | }, 79 | 202: { 80 | group: 'txt', 81 | name: 'url', 82 | type: 'url' 83 | }, 84 | 203: { 85 | group: 'txt', 86 | name: 'avatar', 87 | type: 'url' 88 | }, 89 | 204: { 90 | group: 'txt', 91 | name: 'description', 92 | type: 'string' 93 | }, 94 | /*205:{ 95 | group:'txt', 96 | name:'notice', 97 | type:'string' 98 | }, 99 | 206:{ 100 | group:'txt', 101 | name:'keywords', 102 | type:'string' 103 | },*/ 104 | 105 | ////////////////////////////////// 106 | 107 | 301: { 108 | group: 'sns', 109 | name: 'com.discord', 110 | type: 'string' 111 | }, 112 | 302: { 113 | group: 'sns', 114 | name: 'com.github', 115 | type: 'string' 116 | }, 117 | 303: { 118 | group: 'sns', 119 | name: 'com.reddit', 120 | type: 'string' 121 | }, 122 | 304: { 123 | group: 'sns', 124 | name: 'com.twitter', 125 | type: 'string' 126 | }, 127 | 305: { 128 | group: 'sns', 129 | name: 'com.telegram', 130 | type: 'string' 131 | 132 | ////////////////////////////////// 133 | } }; 134 | 135 | module.exports = { 136 | GroupList, 137 | FieldFormatTable 138 | }; -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | // External Deps 4 | 5 | const { AptosClient, TokenClient, HexString } = require("aptos"); 6 | const { GroupList, FieldFormatTable } = require("./format_tbl"); 7 | 8 | // Const 9 | const BASE_SUFFIX = '@apt'; 10 | 11 | const Okay = 200; 12 | const ErrorNotFound = 404; 13 | const ErrorUnknow = 500; 14 | 15 | class AptDomainObject { 16 | constructor(records) { 17 | this.records = records ? records : {}; 18 | this.fields = {}; 19 | 20 | if (this.records && this.records.data && this.records.data.data) { 21 | for (let cur of this.records.data.data) { 22 | this.fields[cur.name] = cur.value; 23 | } 24 | } 25 | } 26 | 27 | address() { 28 | return this.record('APT'); 29 | } 30 | avatar() { 31 | return this.record('avatar'); 32 | } 33 | url() { 34 | return this.record('url'); 35 | } 36 | email() { 37 | return this.record('email'); 38 | } 39 | 40 | discord() { 41 | return this.record('com.discord'); 42 | } 43 | github() { 44 | return this.record('com.github'); 45 | } 46 | reddit() { 47 | return this.record('com.reddit'); 48 | } 49 | twitter() { 50 | return this.record('com.twitter'); 51 | } 52 | telegram() { 53 | return this.record('com.telegram'); 54 | } 55 | 56 | record(name) { 57 | return this.fields[name] ? this.fields[name] : ''; 58 | } 59 | 60 | } 61 | 62 | class AptDomain { 63 | 64 | constructor(opts = {}) { 65 | 66 | //Init params 67 | this._network = opts && opts.network ? opts.network : 'mainnet'; 68 | 69 | switch (this._network) { 70 | case 'mainnet': 71 | this._nodeUrl = 'https://fullnode.mainnet.aptoslabs.com'; 72 | this._contractAddress = '0x777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb'; 73 | break; 74 | case 'testnet': 75 | this._nodeUrl = 'https://fullnode.testnet.aptoslabs.com'; 76 | this._contractAddress = '0x7ce77452da809fbc4ef32596cf2be18ec6f252e1884b4eefa4d4349c2941923e'; 77 | break; 78 | case 'devnet': 79 | this._nodeUrl = 'https://fullnode.devnet.aptoslabs.com'; 80 | this._contractAddress = '0x7ce77452da809fbc4ef32596cf2be18ec6f252e1884b4eefa4d4349c2941923e'; 81 | break; 82 | default: 83 | console.warn(`AptDomain: Unknow Network setting on ${this._network} `); 84 | break; 85 | } 86 | 87 | if (opts && opts.nodeUrl) { 88 | this._nodeUrl = opts.nodeUrl; 89 | } 90 | 91 | if (opts && opts.contractAddress) { 92 | this._contractAddress = opts.contractAddress; 93 | } 94 | 95 | //Init handles 96 | this._client = new AptosClient(this._nodeUrl, opts.aptosClientConfig); 97 | this._tokenClient = new TokenClient(this._client); 98 | this._globalMap = null; 99 | } 100 | 101 | _process_domain(domain) { 102 | return domain ? domain.toLowerCase().replace(BASE_SUFFIX, '') : ''; 103 | } 104 | 105 | _isAddress(val) { 106 | return val ? val.startsWith('0x') && val.length >= 20 : false; 107 | } 108 | 109 | _isAptDomain(val) { 110 | return val ? val.toLowerCase().endsWith(BASE_SUFFIX) : false; 111 | } 112 | 113 | format_domain_data(cur) { 114 | 115 | if (!cur || !cur.data || !cur.data.data) return cur; 116 | 117 | cur.addr = "0x0"; 118 | for (let dt of cur.data.data) { 119 | let format = FieldFormatTable[dt.key]; 120 | if (format) { 121 | dt.name = format.name; 122 | dt.type = format.type; 123 | dt.group = format.group; 124 | dt.value = Buffer.from(new HexString(dt.value).toUint8Array()).toString(); 125 | 126 | switch (dt.type) { 127 | case 'string': 128 | break; 129 | case 'unixtime': 130 | dt.value = new Date(parseInt(dt.value) * 1000).format('Y-M-d h:m:s');break; 131 | } 132 | } else { 133 | dt.name = 'Unknow'; 134 | dt.type = 'Unknow'; 135 | } 136 | 137 | //this is APT address on key1 138 | if (dt.key == 1) { 139 | cur.addr = dt.value; 140 | } 141 | } 142 | 143 | if (cur.nft_meta) { 144 | if (typeof cur.nft_meta.default_properties.map != "undefined") { 145 | for (let dt of cur.nft_meta.default_properties.map.data) { 146 | if (dt.key == "creator") { 147 | cur.creator = Buffer.from(new HexString(dt.value.value).toUint8Array()).toString(); 148 | } 149 | } 150 | } else { 151 | for (let key in cur.nft_meta.default_properties.data) { 152 | if (key == "creator") { 153 | let value = cur.nft_meta.default_properties.data[key]; 154 | cur.creator = Buffer.from(new HexString(value.value).toUint8Array()).toString(); 155 | } 156 | } 157 | } 158 | } 159 | 160 | return cur; 161 | } 162 | 163 | async _get_globalmap() { 164 | try { 165 | //If init 166 | if (this._globalMap) return this._globalMap; 167 | 168 | const res = await this._client.getAccountResources(this._contractAddress); 169 | for (let cur of res) { 170 | if (cur.type == `${this._contractAddress}::APDomain::GlobalMap`) { 171 | this._globalMap = cur.data; 172 | break; 173 | } 174 | } 175 | return this._globalMap; 176 | } catch (e) { 177 | //Failed to get global map 178 | } 179 | return null; 180 | } 181 | 182 | //domain => address 183 | async lookup(domain, cb) { 184 | 185 | if (this._isAddress(domain)) { 186 | let ret = { status: 200, address: domain }; 187 | if (cb) cb(ret); 188 | return ret; 189 | } 190 | 191 | let ret = { status: ErrorUnknow, address: null }; 192 | 193 | try { 194 | let globalmap = await this._get_globalmap(); 195 | 196 | domain = this._process_domain(domain); 197 | 198 | const addr = await this._client.getTableItem(globalmap.domain2addr.handle, { 199 | key: Buffer.from(domain).toString('hex'), 200 | key_type: "vector", 201 | value_type: "address" 202 | }); 203 | 204 | if (!addr) { 205 | //not found 206 | ret = { status: ErrorNotFound, address: null }; 207 | } else { 208 | //found 209 | ret = { status: Okay, address: addr }; 210 | } 211 | } catch (e) { 212 | //not found 213 | if (e.status == ErrorNotFound) ret = { status: ErrorNotFound, address: null }; 214 | } 215 | if (cb) cb(ret.status, ret.address); 216 | return ret; 217 | } 218 | 219 | //address => domain 220 | async reverse(address, cb) { 221 | 222 | if (this._isAptDomain(address)) { 223 | let ret = { status: Okay, domain: address }; 224 | if (cb) cb(ret); 225 | return ret; 226 | } 227 | 228 | let ret = { status: ErrorUnknow, domain: null }; 229 | 230 | try { 231 | let globalmap = await this._get_globalmap(); 232 | 233 | const name = await this._client.getTableItem(globalmap.addr2domain.handle, { 234 | key: address, 235 | key_type: "address", 236 | value_type: "vector" 237 | }); 238 | 239 | if (!name) { 240 | //not found 241 | ret = { status: ErrorNotFound, name: null }; 242 | } else { 243 | let namt_str = Buffer.from(new HexString(name).toUint8Array()).toString(); 244 | if (namt_str == "") ret = { status: 502, domain: null };else ret = { status: Okay, domain: namt_str + BASE_SUFFIX }; 245 | } 246 | } catch (e) { 247 | //not found 248 | if (e.status == ErrorNotFound) ret = { status: ErrorNotFound, domain: null }; 249 | } 250 | if (cb) cb(ret.status, ret.domain); 251 | return ret; 252 | } 253 | 254 | async getDomainObj(domain_or_address, cb) { 255 | 256 | let ret = await this.reverse(domain_or_address); 257 | 258 | if (!ret || ret.status != Okay) return null; 259 | 260 | let domain = ret.domain; 261 | 262 | if (cb) { 263 | await this.getDomainRecord(domain, function (ret) { 264 | if (ret.status == Okay && ret.record) cb(new AptDomainObject(ret.record));else cb(null); 265 | }); 266 | } else { 267 | let ret = await this.getDomainRecord(domain); 268 | if (ret.status == Okay && ret.record) return new AptDomainObject(ret.record); 269 | return null; 270 | } 271 | } 272 | 273 | //get all records from domain 274 | async getDomainRecord(domain, cb) { 275 | try { 276 | let globalmap = await this._get_globalmap(); 277 | 278 | domain = this._process_domain(domain); 279 | 280 | let domainObject = await this._client.getTableItem(globalmap.domains.handle, { 281 | key: Buffer.from(domain).toString('hex'), 282 | key_type: "vector", 283 | value_type: `${this._contractAddress}::APDomain::Domain` 284 | }); 285 | 286 | if (!domainObject) { 287 | if (cb) cb(ErrorNotFound, null); 288 | return { status: ErrorNotFound, record: null }; 289 | } 290 | 291 | try { 292 | domainObject.nft_meta = await this._tokenClient.getTokenData(domainObject.tokenid.token_data_id.creator, domainObject.tokenid.token_data_id.collection, domainObject.tokenid.token_data_id.name); 293 | } catch (e) { 294 | if (e.status == ErrorNotFound) { 295 | if (cb) cb(ErrorNotFound, null); 296 | return { status: ErrorNotFound, record: null }; 297 | } 298 | 299 | console.error(e); 300 | } 301 | 302 | domainObject = this.format_domain_data(domainObject); 303 | 304 | if (cb) cb(Okay, domainObject); 305 | return { status: Okay, record: domainObject }; 306 | } catch (e) { 307 | if (e.status == ErrorNotFound) { 308 | if (cb) cb(ErrorNotFound, null); 309 | return { status: ErrorNotFound, record: null }; 310 | } 311 | console.error(e); 312 | } 313 | return { status: ErrorUnknow, record: null }; 314 | } 315 | 316 | } 317 | 318 | module.exports = { 319 | AptDomain, 320 | AptDomainObject 321 | }; -------------------------------------------------------------------------------- /examples/basic_usage.js: -------------------------------------------------------------------------------- 1 | const { AptDomain } = require('../src/index'); 2 | 3 | (async()=>{ 4 | 5 | let opt={ 6 | // 7 | // set network 'mainnet' / 'testnet' / 'devnet', default: 'mainnet' 8 | // 9 | network: 'testnet' 10 | 11 | // 12 | // Or you can set the node url and contract address 13 | // 14 | // for mainnet 15 | //nodeUrl:'https://fullnode.mainnet.aptoslabs.com', 16 | //contractAddress:'0x777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb', 17 | 18 | // for testnet 19 | //nodeUrl:'https://fullnode.testnet.aptoslabs.com', 20 | //contractAddress:'0x7ce77452da809fbc4ef32596cf2be18ec6f252e1884b4eefa4d4349c2941923e', 21 | 22 | // for devtest 23 | //nodeUrl:'https://fullnode.devnet.aptoslabs.com', 24 | //contractAddress:'0x7ce77452da809fbc4ef32596cf2be18ec6f252e1884b4eefa4d4349c2941923e', 25 | 26 | // 27 | // aptos client config 28 | // 29 | //aptosClientConfig:{}, 30 | }; 31 | 32 | let aptdomain=new AptDomain(opt); 33 | 34 | const test_domain = 'test007@apt'; 35 | const test_address = '0x5e0c91adbe365dca24d1565b434b04c79e17823a1c6db299ba291fc7e86325e6'; 36 | 37 | /////////////////////////////////////////// 38 | // basic using on promise 39 | 40 | let { address } = await aptdomain.lookup( test_domain); 41 | console.log( `[promise] ${test_domain} => ${address}` ); 42 | 43 | let { domain } = await aptdomain.reverse( test_address ); 44 | console.log( `[promise] ${test_address} => ${domain}` ); 45 | 46 | let data = await aptdomain.getDomainRecord(test_domain); 47 | console.log( `[promise] ${test_domain} => ${JSON.stringify(data,null,' ')}` ); 48 | 49 | /////////////////////////////////////////// 50 | // basic using on callback 51 | 52 | aptdomain.lookup(test_domain,(status,address)=>{ 53 | console.log( `[callback] ${test_domain} => ${address}` ); 54 | }); 55 | 56 | aptdomain.reverse(test_address,(status,domain)=>{ 57 | console.log( `[callback] ${test_address} => ${domain}` ); 58 | }); 59 | 60 | aptdomain.getDomainRecord(test_domain,(status,data)=>{ 61 | console.log( `[callback] ${test_domain} => ${JSON.stringify(data,null,' ')}` ); 62 | }); 63 | 64 | /////////////////////////////////////////// 65 | // domain object 66 | 67 | // request a domain object by addres or domain 68 | // if domain not exist , return null 69 | 70 | let domainObj = await aptdomain.getDomainObj(test_address); 71 | 72 | if( domainObj ){ 73 | console.log("domainObj.address(): ", domainObj.address()); 74 | console.log("domainObj.avatar(): ", domainObj.avatar()); 75 | console.log("domainObj.url(): ", domainObj.url()); 76 | console.log("domainObj.email(): ", domainObj.email()); 77 | 78 | console.log("domainObj.discord(): ", domainObj.discord()); 79 | console.log("domainObj.github(): ", domainObj.github()); 80 | console.log("domainObj.reddit(): ", domainObj.reddit()); 81 | console.log("domainObj.twitter(): ", domainObj.twitter()); 82 | console.log("domainObj.telegram(): ", domainObj.telegram()); 83 | 84 | console.log("domainObj.record('APT'): ", domainObj.record('APT')); 85 | console.log("domainObj.record('ETH'): ", domainObj.record('ETH')); 86 | console.log("domainObj.record('BTC'): ", domainObj.record('BTC')); 87 | console.log("domainObj.record('Solana'): ", domainObj.record('Solana')); 88 | } 89 | 90 | })(); 91 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aptpp-js", 3 | "version": "1.0.3", 4 | "description": "aptos passport js sdk", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "build": "babel src --out-dir dist", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "homepage": "https://aptpp.com", 11 | "author": "AptosPassport [https://aptpp.com]", 12 | "keywords": [ 13 | "Aptos", 14 | "Aptos Labs", 15 | "AptPP", 16 | "Passport", 17 | "Domain", 18 | "Move" 19 | ], 20 | "license": "Apache-2.0", 21 | "repository": { 22 | "type": "git", 23 | "url": "https://github.com/aptospassport/aptpp-js.git" 24 | }, 25 | "dependencies": { 26 | "aptos": "^1.3.15" 27 | }, 28 | "devDependencies": { 29 | "babel-cli": "^6.26.0", 30 | "babel-preset-env": "^1.7.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/format_tbl.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const GroupList = ['address', 'dns', 'txt', 'sns']; 4 | 5 | const FieldFormatTable = { 6 | 7 | 1: { 8 | group: 'address', 9 | name: 'APT', 10 | type: 'string' 11 | }, 12 | 13 | ////////////////////////////////// 14 | 400: { 15 | group: 'dns', 16 | name: 'A Record', 17 | type: 'string' 18 | }, 19 | 401: { 20 | group: 'dns', 21 | name: 'CNAME Record', 22 | type: 'string' 23 | }, 24 | 402: { 25 | group: 'dns', 26 | name: 'TXT Record', 27 | type: 'string' 28 | }, 29 | 403: { 30 | group: 'dns', 31 | name: 'IPFS CID', 32 | type: 'string' 33 | }, 34 | ////////////////////////////////// 35 | 36 | 100: { 37 | group: 'address', 38 | name: 'ETH', 39 | type: 'string' 40 | }, 41 | 101: { 42 | group: 'address', 43 | name: 'BTC', 44 | type: 'string' 45 | }, 46 | 102: { 47 | group: 'address', 48 | name: 'BSC', 49 | type: 'string' 50 | }, 51 | 103: { 52 | group: 'address', 53 | name: 'Solana', 54 | type: 'string' 55 | }, 56 | /*104:{ 57 | group:'address', 58 | name:'Avalanche', 59 | type:'string' 60 | }, 61 | 105:{ 62 | group:'address', 63 | name:'TRON', 64 | type:'string' 65 | }, 66 | 106:{ 67 | group:'address', 68 | name:'Polygon', 69 | type:'string' 70 | },*/ 71 | 72 | ////////////////////////////////// 73 | 74 | 201: { 75 | group: 'txt', 76 | name: 'email', 77 | type: 'email' 78 | }, 79 | 202: { 80 | group: 'txt', 81 | name: 'url', 82 | type: 'url' 83 | }, 84 | 203: { 85 | group: 'txt', 86 | name: 'avatar', 87 | type: 'url' 88 | }, 89 | 204: { 90 | group: 'txt', 91 | name: 'description', 92 | type: 'string' 93 | }, 94 | /*205:{ 95 | group:'txt', 96 | name:'notice', 97 | type:'string' 98 | }, 99 | 206:{ 100 | group:'txt', 101 | name:'keywords', 102 | type:'string' 103 | },*/ 104 | 105 | ////////////////////////////////// 106 | 107 | 301: { 108 | group: 'sns', 109 | name: 'com.discord', 110 | type: 'string' 111 | }, 112 | 302: { 113 | group: 'sns', 114 | name: 'com.github', 115 | type: 'string' 116 | }, 117 | 303: { 118 | group: 'sns', 119 | name: 'com.reddit', 120 | type: 'string' 121 | }, 122 | 304: { 123 | group: 'sns', 124 | name: 'com.twitter', 125 | type: 'string' 126 | }, 127 | 305: { 128 | group: 'sns', 129 | name: 'com.telegram', 130 | type: 'string' 131 | } 132 | 133 | ////////////////////////////////// 134 | }; 135 | 136 | module.exports = { 137 | GroupList, 138 | FieldFormatTable 139 | }; -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | // External Deps 4 | const { AptosClient, TokenClient, HexString } = require("aptos"); 5 | const { GroupList, FieldFormatTable } = require("./format_tbl"); 6 | 7 | // Const 8 | const BASE_SUFFIX = '@apt'; 9 | 10 | const Okay = 200; 11 | const ErrorNotFound = 404; 12 | const ErrorUnknow = 500; 13 | 14 | class AptDomainObject { 15 | constructor(records) { 16 | this.records=records?records:{}; 17 | this.fields={}; 18 | 19 | if( this.records && this.records.data && this.records.data.data ){ 20 | for( let cur of this.records.data.data ){ 21 | this.fields[cur.name]=cur.value; 22 | } 23 | } 24 | } 25 | 26 | address() {return this.record('APT');} 27 | avatar() {return this.record('avatar')} 28 | url() {return this.record('url')} 29 | email() {return this.record('email')} 30 | 31 | discord() {return this.record('com.discord')} 32 | github() {return this.record('com.github')} 33 | reddit() {return this.record('com.reddit')} 34 | twitter() {return this.record('com.twitter')} 35 | telegram() {return this.record('com.telegram')} 36 | 37 | record(name){ 38 | return this.fields[name]?this.fields[name]:''; 39 | } 40 | 41 | } 42 | 43 | class AptDomain { 44 | 45 | constructor(opts = {}) { 46 | 47 | //Init params 48 | this._network = opts && opts.network ? opts.network : 'mainnet'; 49 | 50 | switch( this._network ){ 51 | case 'mainnet': 52 | this._nodeUrl='https://fullnode.mainnet.aptoslabs.com'; 53 | this._contractAddress='0x777821c78442e17d82c3d7a371f42de7189e4248e529fe6eee6bca40ddbb'; 54 | break; 55 | case 'testnet': 56 | this._nodeUrl='https://fullnode.testnet.aptoslabs.com'; 57 | this._contractAddress='0x7ce77452da809fbc4ef32596cf2be18ec6f252e1884b4eefa4d4349c2941923e'; 58 | break; 59 | case 'devnet': 60 | this._nodeUrl='https://fullnode.devnet.aptoslabs.com'; 61 | this._contractAddress='0x7ce77452da809fbc4ef32596cf2be18ec6f252e1884b4eefa4d4349c2941923e'; 62 | break; 63 | default: 64 | console.warn(`AptDomain: Unknow Network setting on ${this._network} `); 65 | break; 66 | } 67 | 68 | if( opts && opts.nodeUrl ){ 69 | this._nodeUrl = opts.nodeUrl; 70 | } 71 | 72 | if( opts && opts.contractAddress ){ 73 | this._contractAddress = opts.contractAddress; 74 | } 75 | 76 | //Init handles 77 | this._client = new AptosClient(this._nodeUrl, opts.aptosClientConfig); 78 | this._tokenClient = new TokenClient(this._client); 79 | this._globalMap = null; 80 | } 81 | 82 | _process_domain(domain) { 83 | return domain ? domain.toLowerCase().replace(BASE_SUFFIX, '') : ''; 84 | } 85 | 86 | _isAddress(val) { 87 | return val?(val.startsWith('0x') && val.length>=20):false; 88 | } 89 | 90 | _isAptDomain(val) { 91 | return val?val.toLowerCase().endsWith(BASE_SUFFIX):false; 92 | } 93 | 94 | format_domain_data(cur) { 95 | 96 | if (!cur || !cur.data || !cur.data.data) return cur; 97 | 98 | cur.addr = "0x0"; 99 | for (let dt of cur.data.data) { 100 | let format = FieldFormatTable[dt.key]; 101 | if (format) { 102 | dt.name = format.name; 103 | dt.type = format.type; 104 | dt.group = format.group; 105 | dt.value = Buffer.from(new HexString(dt.value).toUint8Array()).toString(); 106 | 107 | switch (dt.type) { 108 | case 'string': 109 | break; 110 | case 'unixtime': 111 | dt.value = new Date(parseInt(dt.value) * 1000).format('Y-M-d h:m:s');break; 112 | } 113 | } else { 114 | dt.name = 'Unknow'; 115 | dt.type = 'Unknow'; 116 | } 117 | 118 | //this is APT address on key1 119 | if (dt.key == 1) { 120 | cur.addr = dt.value; 121 | } 122 | } 123 | 124 | if (cur.nft_meta) { 125 | if( typeof cur.nft_meta.default_properties.map!="undefined" ){ 126 | for (let dt of cur.nft_meta.default_properties.map.data) { 127 | if (dt.key == "creator") { 128 | cur.creator = Buffer.from(new HexString(dt.value.value).toUint8Array()).toString(); 129 | } 130 | } 131 | } 132 | else{ 133 | for (let key in cur.nft_meta.default_properties.data) { 134 | if (key == "creator") { 135 | let value=cur.nft_meta.default_properties.data[key]; 136 | cur.creator = Buffer.from(new HexString(value.value).toUint8Array()).toString(); 137 | } 138 | } 139 | } 140 | } 141 | 142 | return cur; 143 | } 144 | 145 | async _get_globalmap() { 146 | try { 147 | //If init 148 | if (this._globalMap) return this._globalMap; 149 | 150 | const res = await this._client.getAccountResources(this._contractAddress); 151 | for (let cur of res) { 152 | if (cur.type == `${this._contractAddress}::APDomain::GlobalMap`) { 153 | this._globalMap = cur.data; 154 | break; 155 | } 156 | } 157 | return this._globalMap; 158 | } catch (e) { 159 | //Failed to get global map 160 | } 161 | return null; 162 | } 163 | 164 | //domain => address 165 | async lookup(domain, cb) { 166 | 167 | if( this._isAddress(domain) ){ 168 | let ret = { status: 200, address: domain }; 169 | if(cb) cb(ret); 170 | return ret; 171 | } 172 | 173 | let ret = { status: ErrorUnknow, address: null }; 174 | 175 | try { 176 | let globalmap = await this._get_globalmap(); 177 | 178 | domain = this._process_domain(domain); 179 | 180 | const addr = await this._client.getTableItem(globalmap.domain2addr.handle, { 181 | key: Buffer.from(domain).toString('hex'), 182 | key_type: "vector", 183 | value_type: "address" 184 | }); 185 | 186 | if (!addr) { 187 | //not found 188 | ret = { status: ErrorNotFound, address: null }; 189 | } else { 190 | //found 191 | ret = { status: Okay, address: addr }; 192 | } 193 | } catch (e) { 194 | //not found 195 | if (e.status == ErrorNotFound) ret = { status: ErrorNotFound, address: null }; 196 | } 197 | if (cb) cb(ret.status, ret.address); 198 | return ret; 199 | } 200 | 201 | //address => domain 202 | async reverse(address, cb) { 203 | 204 | if( this._isAptDomain(address) ){ 205 | let ret = { status: Okay, domain: address }; 206 | if(cb) cb(ret); 207 | return ret; 208 | } 209 | 210 | let ret = { status: ErrorUnknow, domain: null }; 211 | 212 | try { 213 | let globalmap = await this._get_globalmap(); 214 | 215 | const name = await this._client.getTableItem(globalmap.addr2domain.handle, { 216 | key: address, 217 | key_type: "address", 218 | value_type: "vector" 219 | }); 220 | 221 | if (!name) { 222 | //not found 223 | ret = { status: ErrorNotFound, name: null }; 224 | } else { 225 | let namt_str = Buffer.from(new HexString(name).toUint8Array()).toString(); 226 | if (namt_str == "") ret = { status: 502, domain: null };else ret = { status: Okay, domain: namt_str + BASE_SUFFIX }; 227 | } 228 | } catch (e) { 229 | //not found 230 | if (e.status == ErrorNotFound) ret = { status: ErrorNotFound, domain: null }; 231 | } 232 | if (cb) cb(ret.status, ret.domain); 233 | return ret; 234 | } 235 | 236 | async getDomainObj(domain_or_address, cb) { 237 | 238 | let ret = await this.reverse(domain_or_address); 239 | 240 | if( !ret || ret.status!=Okay ) 241 | return null; 242 | 243 | let domain=ret.domain; 244 | 245 | if( cb ){ 246 | await this.getDomainRecord(domain,function(ret){ 247 | if( ret.status==Okay && ret.record ) 248 | cb( new AptDomainObject(ret.record) ); 249 | else 250 | cb( null ); 251 | }); 252 | } 253 | else{ 254 | let ret=await this.getDomainRecord(domain); 255 | if( ret.status==Okay && ret.record ) 256 | return new AptDomainObject(ret.record); 257 | return null; 258 | } 259 | } 260 | 261 | //get all records from domain 262 | async getDomainRecord(domain, cb) { 263 | try { 264 | let globalmap = await this._get_globalmap(); 265 | 266 | domain = this._process_domain(domain); 267 | 268 | let domainObject = await this._client.getTableItem(globalmap.domains.handle, { 269 | key: Buffer.from(domain).toString('hex'), 270 | key_type: "vector", 271 | value_type: `${this._contractAddress}::APDomain::Domain` 272 | }); 273 | 274 | if (!domainObject) { 275 | if (cb) cb(ErrorNotFound, null); 276 | return { status: ErrorNotFound, record: null }; 277 | } 278 | 279 | try { 280 | domainObject.nft_meta=await this._tokenClient.getTokenData( 281 | domainObject.tokenid.token_data_id.creator, 282 | domainObject.tokenid.token_data_id.collection, 283 | domainObject.tokenid.token_data_id.name, 284 | ); 285 | } catch (e) { 286 | if (e.status == ErrorNotFound) { 287 | if (cb) cb(ErrorNotFound, null); 288 | return { status: ErrorNotFound, record: null }; 289 | } 290 | 291 | console.error(e); 292 | } 293 | 294 | domainObject = this.format_domain_data(domainObject); 295 | 296 | if (cb) cb(Okay, domainObject); 297 | return { status: Okay, record: domainObject }; 298 | } catch (e) { 299 | if (e.status == ErrorNotFound) { 300 | if (cb) cb(ErrorNotFound, null); 301 | return { status: ErrorNotFound, record: null }; 302 | } 303 | console.error(e); 304 | } 305 | return { status: ErrorUnknow, record: null }; 306 | } 307 | 308 | } 309 | 310 | module.exports = { 311 | AptDomain, 312 | AptDomainObject 313 | }; --------------------------------------------------------------------------------